/
hw5.cpp
151 lines (124 loc) · 2.39 KB
/
hw5.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
#include <iomanip>
#include <math.h>
#include <string>
#include <unordered_map>
#include <typeinfo>
#include <queue>
using namespace std;
// g++ -std=c++11 -Wall hw5.cpp
// a < hw5_test.txt
string name1, name2;
unordered_map <string, vector<string>> graph;
unordered_map <string, string> color;
unordered_map <string, int> d;
void BFS(string s)
{
vector<string> Q {s};
vector<string> babyfaces;
vector<string> heels;
while(!Q.empty())
{
string u = Q.front();
Q.erase(Q.begin());
for(auto v: graph[u])
{
if(color[v] == "WHITE")
{
color[v] = "GRAY";
d[v] = d[u] + 1;
Q.push_back(v);
}
}
color[u] = "BLACK";
}
for(auto x:graph)
{
for(int i =0; i < (int)x.second.size(); ++i)
{
if(d[x.first] % 2 == d[x.second.at(i)] % 2)
{
cout << "IMPOSSIBLE" << endl;
return;
}
}
}
//check if babyface or heel
for(auto v: graph)
{
if(d[v.first] % 2 == 0)
{
babyfaces.push_back(v.first);
}
else
{
heels.push_back(v.first);
}
}
sort(babyfaces.begin(), babyfaces.end());
sort(heels.begin(), heels.end());
cout << "BABYFACES" << endl;
for(auto a: babyfaces)
{
cout << a << endl;
}
cout << "HEELS" << endl;
for(auto a: heels)
{
cout << a << endl;
}
}
int main()
{
vector<string> x;
string start;
while(cin >> name1 >> name2)
{
if(graph.empty())
{
start = name1;
}
if(graph.count(name1) == 0)
{
if(graph.count(name2) == 0)
{ //neither are in
graph.emplace(name1, x);
color.emplace(name1, "WHITE");
d.emplace(name1, 0);
graph.emplace(name2, x);
color.emplace(name2, "WHITE");
d.emplace(name2, 0);
graph[name1].push_back(name2);
graph[name2].push_back(name1);
}
else
{ //name1 is not in, but name2 is
graph.emplace(name1, x);
color.emplace(name1, "WHITE");
d.emplace(name1, 0);
graph[name1].push_back(name2);
graph[name2].push_back(name1);
}
}
else if(graph.count(name1) > 0)
{
if(graph.count(name2) == 0)
{ //name1 is in, but not name2
graph.emplace(name2, x);
color.emplace(name2, "WHITE");
d.emplace(name2, 0);
graph[name1].push_back(name2);
graph[name2].push_back(name1);
}
else
{ //both are in
graph[name1].push_back(name2);
graph[name2].push_back(name1);
}
}
}
BFS(start);
}