-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16402.cpp
76 lines (65 loc) · 1.68 KB
/
16402.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 500;
int parent[MAX];
map<string, int> dict;
vector<string> split(string input, char delimiter) {
vector<string> strs;
stringstream ss(input);
string temp;
while (getline(ss, temp, delimiter)) {
strs.push_back(temp);
}
return strs;
}
int find(int x) {
if (parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void merge(string k1, string k2) {
int x = dict[k1];
int y = dict[k2];
int px = find(x);
int py = find(y);
if (px == py) {
parent[x] = parent[px] = parent[py] = x;
} else parent[find(y)] = parent[find(x)];
}
void solve(){
string str;
getline(cin, str);
auto data = split(str, ' ');
int N = stoi(data[0]);
int M = stoi(data[1]);
for (int i = 0; i < N; i++) {
parent[i] = i;
string str; getline(cin, str);
string kingdom = split(str, ' ').back();
dict[kingdom] = i;
}
for (int i = 0; i < M; i++) {
string str; getline(cin, str);
auto data = split(str, ',');
string kingdom1 = split(data[0], ' ').back();
string kingdom2 = split(data[1], ' ').back();
if (data.back() == "2") {
merge(kingdom2, kingdom1);
} else {
merge(kingdom1, kingdom2);
}
}
set<string> result;
for (auto [kingdom, x] : dict) {
if (parent[x] == x) result.insert(kingdom);
}
cout << result.size() << '\n';
for (auto kingdom : result) {
cout << "Kingdom of " << kingdom << '\n';
}
}
int main()
{
solve();
return 0;
}