-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTR12.cpp
68 lines (63 loc) · 1014 Bytes
/
TR12.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
#include<bits/stdc++.h>
using namespace std;
int parent[100010];
int sum[100010];
multiset<int>ss;
int maximum(int a,int b);
int find(int x)
{
if(parent[x]==x)
return x;
return find(parent[x]);
}
int merge(int x,int y)
{
int xp,yp;
xp=find(x);
yp=find(y);
if(xp!=yp)
{
if(sum[xp]>sum[yp])
{
ss.erase(ss.find(sum[xp]));
ss.erase(ss.find(sum[yp]));
sum[xp]+=sum[yp];
parent[yp]=xp;
sum[yp]=0;
ss.insert(sum[xp]);
return sum[xp];
}
else
{
ss.erase(ss.find(sum[xp]));
ss.erase(ss.find(sum[yp]));
sum[yp]+=sum[xp];
parent[xp]=yp;
sum[xp]=0;
ss.insert(sum[yp]);
return sum[yp];
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
sum[i]=1;
parent[i]=i;
ss.insert(1);
}
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
int xp=find(x);
int yp=find(y);
int ans=merge(x,y);
multiset<int>::iterator it=ss.end();
it--;
cout<<*it -*(ss.begin())<<endl;
}
}