-
Notifications
You must be signed in to change notification settings - Fork 0
/
socialCluster.cpp
97 lines (84 loc) · 1.61 KB
/
socialCluster.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<vector>
int visit[1005];
int hobby[1005];
int people[1006];
int hcnt[1005];
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int myfind(int val){
int x = val;
while(val != hobby[val]){
val = hobby[val];
}
int t;
while(x!=val){
t=hobby[x];
hobby[x]=val;
x=t;
}
return val;
}
void myunion(int a, int b){
int fa = myfind(a);
int fb = myfind(b);
if(fa!=fb){
hobby[fb]=fa;
}
}
int main(){
int n;
cin>>n;
memset(visit, 0, sizeof(visit));
memset(hcnt, 0, sizeof(hcnt));
for(int i=0;i<1005;i++){
hobby[i]=i;
}
for(int i=0;i<n;i++){
int hc;
scanf("%d:", &hc);
int ga;
for(int j=0;j<hc;j++){
int h;
scanf("%d", &h);
if(j==0){
ga = myfind(h);
}
myunion(ga, h);
visit[h]=1;
}
people[i]=myfind(ga);
}
set<int> hset;
for(int i=0;i<1005;i++){
if(visit[i]==1){
hset.insert(myfind(i));
}
}
cout<<hset.size()<<endl;
for(int i=0;i<n;i++){
hcnt[myfind(people[i])]+=1;
}
vector<int> retv;
for(int i=0;i<1005;i++){
if(visit[i]==1 && hcnt[i]!=0){
retv.push_back(hcnt[i]);
}
}
sort(retv.begin(),retv.end(), cmp);
for(int i=0;i<hset.size();i++){
if(i==0){
cout<<retv[0];
continue;
}
cout<<" "<<retv[i];
}
cout<<endl;
}