forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasspicture.cpp
71 lines (58 loc) · 1.27 KB
/
classpicture.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
#include <bits/stdc++.h>
using namespace std;
bool works(vector<int>& perm, vector<vector<bool>>& edges) {
for(int i = 1; i < perm.size(); i++) {
if(edges[perm[i-1]][perm[i]]) {
return false;
}
}
return true;
}
void print(vector<int>& v, vector<string>& s) {
for(auto i : v) {
cout << s[i] << " ";
}
cout << endl;
}
void solve(int n) {
vector<string> v;
map<string,int> conv;
for(int i = 0; i < n; i++) {
string s;
cin >> s;
v.push_back(s);
}
sort(v.begin(), v.end());
for(int i = 0; i < n; i++) {
conv[v[i]] = i;
}
vector<vector<bool>> edges;
edges.resize(n, vector<bool>(n, false));
int m;
cin >> m;
while(m--) {
string s1, s2;
cin >> s1 >> s2;
int n1 = conv[s1];
int n2 = conv[s2];
edges[n1][n2] = true;
edges[n2][n1] = true;
}
vector<int> perm;
for(int i = 0; i < n; i++) {
perm.push_back(i);
}
do {
if(works(perm, edges)) {
print(perm, v);
return;
}
} while(next_permutation(perm.begin(), perm.end()));
cout << "You all need therapy." << endl;
}
int main() {
int n;
while(cin >> n) {
solve(n);
}
}