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
|
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <set>
#include <vector>
using namespace std;
vector<int> readints(string &s) {
vector<int> v;
stringstream sin(s);
int x;
while (sin >> x)
v.push_back(x);
return v;
}
bool valid(vector<int> &v, vector<pair<int, int>> &rules) {
for (unsigned i = 0; i < v.size(); i++)
for (unsigned j = i+1; j < v.size(); j++)
for (auto r : rules)
if (v[i] == r.second && v[j] == r.first)
return false;
return true;
}
bool canbenext(int x, vector<int> &v, bool picked[], vector<pair<int, int>> &rules) {
for (unsigned i = 0; i < v.size(); i++) {
if (picked[i])
continue;
for (auto r : rules)
if (r.first == v[i] && r.second == x)
return false;
}
return true;
}
vector<int> sort(vector<int> &v, vector<pair<int, int>> &rules) {
vector<int> w;
bool picked[v.size()] = { 0 };
while (w.size() < v.size()) {
for (unsigned i = 0; i < v.size(); i++) {
if (!picked[i] && canbenext(v[i], v, picked, rules)) {
picked[i] = true;
w.push_back(v[i]);
}
}
}
return w;
}
int main() {
string line;
vector<pair<int, int>> rules;
while (getline(cin, line)) {
if (line == "")
break;
replace(line.begin(), line.end(), '|', ' ');
auto v = readints(line);
int x = v[0];
int y = v[1];
rules.push_back(make_pair(x, y));
}
int tot = 0;
while (getline(cin, line)) {
replace(line.begin(), line.end(), ',', ' ');
auto v = readints(line);
if (!valid(v, rules)) {
auto w = sort(v, rules);
tot += w[w.size()/2];
}
}
cout << tot << endl;
return 0;
}
|