forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaintings.cpp
87 lines (72 loc) · 1.46 KB
/
paintings.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int ways = 0;
vector<int> best;
vector<bool> used;
vector<int> here;
vector<vector<bool>> good;
void recurse() {
if(here.size() == n) {
if(best.size() == 0) {
for(auto i : here) {
best.push_back(i);
}
}
ways++;
return;
}
for(int i = 0; i < n; i++) {
if(!used[i]) {
if(here.size() > 0 && !good[here.back()][i]) {
continue;
}
used[i] = true;
here.push_back(i);
recurse();
here.pop_back();
used[i] = false;
}
}
}
void solve() {
cin >> n;
used.clear();
used.resize(n,false);
map<string,int> color;
vector<string> colors;
for(int i = 0; i < n; i++) {
string s;
cin >> s;
color[s] = i;
colors.push_back(s);
}
good.clear();
good.resize(n,vector<bool>(n,true));
int t;
cin >> t;
for(int i = 0; i < t; i++) {
string s1, s2;
cin >> s1 >> s2;
int n1 = color[s1];
int n2 = color[s2];
good[n1][n2] = false;
good[n2][n1] = false;
}
recurse();
cout << ways << endl;
for(auto i : best) {
cout << colors[i] << " ";
}
cout << endl;
ways = 0;
best.clear();
}
int main() {
int cases;
cin >> cases;
while(cases--) {
solve();
}
}