forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcantinaofbabel.cpp
86 lines (66 loc) · 1.72 KB
/
cantinaofbabel.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 <bits/stdc++.h>
using namespace std;
void dfs1(unordered_map<string, pair<int, vector<string>>>& m, unordered_set<string>& vis, stack<string>& r, string curr) {
if(vis.count(curr) > 0) {
return;
}
vis.insert(curr);
for(auto next : m[curr].second) {
dfs1(m, vis, r, next);
}
r.push(curr);
}
void dfs2(unordered_map<string, pair<int, vector<string>>>& m, unordered_set<string>& vis, vector<string>& comp, string curr) {
if(vis.count(curr) > 0) {
return;
}
vis.insert(curr);
comp.push_back(curr);
for(auto next : m[curr].second) {
dfs2(m, vis, comp, next);
}
}
int main() {
int n;
cin >> n;
unordered_map<string, pair<int, vector<string>>> m1;
unordered_map<string, pair<int, vector<string>>> m2;
unordered_set<string> names;
for(int i = 0; i < n; i++) {
string name, lang, s;
cin >> name >> lang;
names.insert(lang);
m1[lang].first++;
m2[lang].first++;
while(cin.peek() != '\n') {
cin >> s;
m1[lang].second.push_back(s);
m2[s].second.push_back(lang);
}
}
stack<string> r;
unordered_set<string> vis;
for(auto i : names) {
if(vis.count(i) > 0) {
continue;
}
dfs1(m1, vis, r, i);
}
vis.clear();
int best = 0;
while(!r.empty()) {
string curr = r.top();
r.pop();
if(vis.count(curr) > 0) {
continue;
}
vector<string> comp;
dfs2(m2, vis, comp, curr);
int here = 0;
for(auto i : comp) {
here += m1[i].first;
}
best = max(here, best);
}
cout << n - best << endl;
}