forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.cpp
59 lines (47 loc) · 1022 Bytes
/
control.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
#include <bits/stdc++.h>
using namespace std;
int find(vector<int>& d, int a) {
if(d[a] < 0) return a;
return d[a] = find(d, d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) return;
d[a] += d[b];
d[b] = a;
}
int size(vector<int>& d, int a) {
a = find(d, a);
return -d[a];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
vector<int> d(500001,-1);
int n;
cin >> n;
int ans = 0;
while(n--) {
int t;
cin >> t;
set<int> par;
vector<int> all(t);
for(auto& i : all) {
cin >> i;
i = find(d, i);
par.insert(i);
}
int total = 0;
for(auto i : par) {
total += size(d, i);
}
if(total == t) {
ans++;
for(int i = 1; i < all.size(); i++) {
join(d, all[0], all[i]);
}
}
}
cout << ans << endl;
}