-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunningMom.cpp
More file actions
65 lines (61 loc) · 2.14 KB
/
Copy pathrunningMom.cpp
File metadata and controls
65 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <unordered_map>
#include <iterator>
#include <vector>
#include <string>
//using namespace std;
class City{
public:
int outFlightCount;
int isSafe; // -1 unknown; 0 unsafe; 1 safe
std::vector<std::string> destinations;
City(){
isSafe = 1;
outFlightCount = 0;
}
};
int main(void){
int flights;
std::string city1Name, city2Name;
std::cin >> flights;
std::unordered_map<std::string, City*> cities;
for(int i = 0; i < flights; i ++){
std::cin >> city1Name >> city2Name;
std::pair<std::unordered_map<std::string, City*>::iterator,bool> ret;
ret = cities.insert( std::pair<std::string, City*>(city1Name, new City()));
cities.insert( std::pair<std::string, City*>(city2Name, new City()));
ret.first->second->outFlightCount ++;
ret.first->second->destinations.push_back(city2Name);
}
int noOfCities = cities.size();
for(int i = 0; i < noOfCities; i++){
for(std::unordered_map<std::string, City*>::iterator it = cities.begin(); it != cities.end(); ++it){
if(it->second->isSafe == 0)
continue;
if(it->second->outFlightCount == 0){
it->second->isSafe = 0;
continue;
}
int isUnknown = 0;
for(std::vector<std::string>::iterator strIt = it->second->destinations.begin(); strIt != it->second->destinations.end(); strIt++){
std::unordered_map<std::string, City*>::iterator destCity = cities.find(*strIt);
if(destCity->second->isSafe != 0){
isUnknown = 1;
break;
}
}
if(isUnknown == 0){
it->second->isSafe = 0;
}
}
}
while(std::cin >> city1Name){
if(std::cin.eof())
break;
std::unordered_map<std::string, City*>::iterator testingCity = cities.find(city1Name);
if(testingCity->second->isSafe == 1)
std::cout << city1Name << " safe" << std::endl;
else
std::cout << city1Name << " trapped" << std::endl;
}
}