-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva1197The Suspects.cpp
More file actions
98 lines (92 loc) · 2.22 KB
/
uva1197The Suspects.cpp
File metadata and controls
98 lines (92 loc) · 2.22 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
#include<bits/stdc++.h>
#define pb(n) push_back(n)
#define bug cout<<"bug";
#define all(c) c.begin(),c.end()
#define fr(i,n) for(i=0;i<n;i++)
#define sf(n) scanf("%d",&n)
#define mst(n) memset(n,0,sizeof(n))
#define nl printf("\n")
#define lli long long int
#define tr(container,it) \
for (auto it = container.begin(); it != container.end() ; ++it)
#define INF INT_MAX
#define mp(i,j) make_pair(i,j)
#define mstn(n) memset(n,-1,sizeof(n))
#define infile freopen("in.txt","r",stdin)
#define outfile freopen("out.txt","w",stdout)
#define ull unsigned long long
#define V 1000001
#define sz 1005
using namespace std;
int mapping(int n,int &cnt,map<int,int> &imap)
{
if(imap.find(n)==imap.end())
{
imap[n] = cnt++;
}
return imap[n];
}
struct node
{
int indx,rnk,parent,connctd;
node(int n)
{
indx = n, rnk = 0, parent = n, connctd = 1;
}
node() {}
};
node ara[30000];
int findParent(node a)
{
return (a.parent==a.indx? a.indx: ara[a.indx].parent = findParent(ara[a.parent]));
}
void make_union(int u,int v)
{
int p1 = findParent(ara[u]);
int p2 = findParent(ara[v]);
if(p1==p2) return;
if(ara[p1].rnk>ara[p2].rnk)
{
ara[p2].parent = ara[p1].indx;
ara[p1].connctd+= ara[p2].connctd;
}
else if(ara[p1].rnk<ara[p2].rnk)
{
ara[p1].parent = ara[p2].indx;
ara[p2].connctd+= ara[p1].connctd;
}
else
{
ara[p2].parent = ara[p1].indx;
ara[p1].connctd+= ara[p2].connctd;
ara[p1].rnk++;
}
}
void clr(int n)
{
for(int i=0; i<n; i++)
ara[i].indx=-1;
}
int main()
{
int num,grp;
while(scanf("%d %d",&num,&grp) && (num || grp))
{
clr(num);
ara[0] = node(0);
for(int i=0; i<grp; i++)
{
int how,frst,x;
sf(how);
sf(frst);
for(int j=1; j<how; j++)
{
sf(x);
if(ara[frst].indx!=frst) ara[frst] = node(frst);
if(ara[x].indx!=x) ara[x] = node(x);
make_union(frst,x);
}
}
cout<<ara[findParent(ara[0])].connctd<<endl;
}
}