-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakingbad.cpp
More file actions
81 lines (76 loc) · 1.88 KB
/
breakingbad.cpp
File metadata and controls
81 lines (76 loc) · 1.88 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ar array
#define ll long long
void solve() {
int n,m;
cin>>n;
unordered_map<string,vector<string> > adj_list;
unordered_map<string,char> color;
for(int i=0;i<n;i++){
string w;
cin>>w;
adj_list[w]=vector<string>();
color[w]='W';
}
cin>>m;
while(m--){
string u,v;
cin>>u>>v;
adj_list[u].push_back(v);
adj_list[v].push_back(u);
}
for(auto it=color.begin();it!=color.end();it++){
if(it->second=='W'){
// run bfs
queue<string> q;
q.push(it->first);
it->second='R';
while(!q.empty()){
string u=q.front();
q.pop();
char uc=color.find(u)->second;
for(string v:adj_list.find(u)->second){
char &vc=color.find(v)->second;
if(vc==uc){
cout<<"impossible"<<endl;
return;
}
if(vc=='W'){
vc=uc=='R'?'B':'R';
q.push(v);
}
}
}
}
}
int idx=0;
for(auto it=color.begin();it!=color.end();it++){
if(it->second=='R'){
if(idx>0)cout<<" ";
cout<<it->first;
idx++;
}
}
cout<<endl;
idx=0;
for(auto it=color.begin();it!=color.end();it++){
if(it->second=='B'){
if(idx>0)cout<<" ";
cout<<it->first;
idx++;
}
}
cout<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}