-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
73 lines (65 loc) · 1.25 KB
/
A.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
ll n,m;
ll id[105];
ll sz[105];
void initialise(){
for(ll i=0;i<=m;i++){
id[i] = i;
sz[i] = 1;
}
}
ll root(ll i){
while(i!=id[i]){
id[i] = id[id[i]];
i = id[i];
}
return i;
}
bool connected(ll p , ll q){
return(root(p)==root(q));
}
void uni(ll p, ll q){
ll proot = root(p);
ll qroot = root(q);
if(sz[proot]>sz[qroot]){
id[qroot] = id[proot];
sz[proot] += sz[qroot];
}
else{
id[proot] = id[qroot];
sz[qroot]+=sz[proot];
}
}
int main(){
fast;
set <ll> mp;
cin>>n>>m;
initialise();
ll ans=0,k,first,temp;
for(ll i=0;i<n;i++){
cin>>k;
if(k==0)
ans++;
else{
cin>>first;
mp.insert(first);
for(ll j=1;j<k;j++){
cin>>temp;
mp.insert(temp);
uni(first,temp);
}
}
}
if(ans==n)
cout<<n<<"\n";
else{
for(ll i=1;i<=m;i++)
if(id[i]==i && mp.find(i)!=mp.end())
ans++;
cout<<ans-1<<"\n";
}
return 0;
}