-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakingbad.cpp
86 lines (77 loc) · 1.65 KB
/
breakingbad.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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <climits>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
vector<string> V;
unordered_map<string, int> M;
unordered_map<string, vector<string>> AL;
bool impossible = false;
void bfs(string x) {
queue<string> q;
q.push(x);
M[x] = 1;
while (!q.empty()) {
string u = q.front(); q.pop();
int ui = M[u];
int vi = 2;
if (ui == 2) vi = 1;
for (auto& v:AL[u]) {
if (M[v]) {
if (M[v] == ui) {impossible = true; return; }
continue;
}
M[v] = vi;
q.push(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("1.in","r",stdin);
int n, m; string s,a,b;
cin >> n;
for (int i=0;i<n;i++) {
cin >> s; V.emplace_back(s);
}
cin >> m;
for (int i=0;i<m;i++) {
cin >> a >> b;
AL[a].push_back(b);
AL[b].push_back(a);
}
for (auto& x:V) {
if (M[x] != 0) continue;
bfs(x);
if (impossible) {
cout << "impossible"; return 0;
}
}
vector<string> v1, v2;
for (auto& x:V) {
if (M[x] == 1) v1.push_back(x);
else v2.push_back(x);
}
for (auto& x:v1) {
cout << x << ' ';
}
cout << endl;
for (auto& x:v2) {
cout << x << ' ';
}
}