-
Notifications
You must be signed in to change notification settings - Fork 0
/
01197.cpp
58 lines (41 loc) · 1.16 KB
/
01197.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
class UnionFind{
private:
vector<ll> p, setSize;
public:
UnionFind(ll n){
p.assign(n, 0); setSize.assign(n,1);
for(ll i = 0; i < n; i++) p[i] = i;
}
ll findSet(ll k){return (p[k]==k)?k:(p[k]=findSet(p[k]));}
ll findSize(ll k){return setSize[findSet(k)];}
void Union(ll x, ll y){
if(findSet(x)==findSet(y)) return;
x = findSet(x); y = findSet(y);
if(setSize[x] > setSize[y]) swap(x, y);
p[x] = y;
setSize[y] += setSize[x];
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ll n,m;
while(cin >> n >> m && n){
UnionFind UF(n);
for(ll i = 0; i < m; i++){
ll k; cin >> k;
ll h; cin >> h;
for(ll j = 1; j < k; j++){
ll temp; cin >> temp;
UF.Union(h, temp);
}
}
cout << UF.findSize(0) << "\n";
}
return 0;
}