-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA_1107.cpp
73 lines (71 loc) · 1.17 KB
/
A_1107.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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int n, father[maxn], isRoot[maxn] = {0}, k;
void init()
{
for(int i = 0; i < maxn; i++)
father[i] = i;
}
//°üº¬ÁËѹËõ·¾¶µÄ²Ù×÷
int findFather(int x)
{
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a, int b)
{
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB)
father[faA] = faB;
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int t, cnt = 0, course[maxn] = {0};
scanf("%d", &n);
init();
for(int i = 1; i <= n; i++)
{
scanf("%d:", &k);
for(int j = 0; j < k; j++)
{
scanf("%d", &t);
if(course[t] == 0)
course[t] = i;
Union(i, findFather(course[t]));
}
}
for(int i = 1; i <= n; i++)
{
isRoot[findFather(i)]++;
}
for(int i = 1; i <= n; i++)
{
if(isRoot[i] != 0)
cnt++;
}
sort(isRoot, isRoot + n + 1, cmp);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
{
printf("%d", isRoot[i]);
if(i + 1 != cnt)
printf(" ");
}
return 0;
}