-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountfriends.cpp
More file actions
executable file
·71 lines (56 loc) · 814 Bytes
/
countfriends.cpp
File metadata and controls
executable file
·71 lines (56 loc) · 814 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define SIZE 1000000
ll mod = 1000000007;
ll root[SIZE], size[SIZE];
map<ll, ll> mp;
ll n;
ll getroot(ll u)
{
while(root[u]!=u)
{
root[u]=root[root[u]];
u = root[u];
}
return u;
}
void unionNodes(ll u, ll v)
{
ll r_u=getroot(u);
ll r_v=getroot(v);
if(size[r_u]>size[r_v])
{
root[r_v]=root[r_u];
size[r_u]+=size[r_v];
size[r_v]=0;
}
else
{
root[r_u]=root[r_v];
size[r_v]+=size[r_u];
size[r_u]=0;
}
}
int main(void)
{
ll m;
cin>>n>>m;
for(int i=0; i<n; i++)
{
root[i]=i;
size[i]=1;
}
for(ll i=0; i<m; i++)
{
int x, y;
cin>>x>>y;
unionNodes(x-1, y-1);
}
for(int i=0; i<n; i++)
mp[getroot(i)]++;
for(int i=0; i<n; i++)
cout<<mp[getroot(i)]-1<<" ";
cout<<"\n";
return 0;
}