-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRoad Construction.cpp
68 lines (57 loc) · 1.11 KB
/
Road Construction.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
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define F first
#define S second
#define ll long long
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
const int N = 100005;
int parent[N];
int rnk[N];
int sz[N];
void Dsu(int n){
for(int i=0;i<=n;i++) {
parent[i] = i;
rnk[i] = 0;
sz[i] = 1;
}
}
int Find(int v) {
if (v == parent[v])
return v;
return parent[v] = Find(parent[v]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a != b) {
if (rnk[a] < rnk[b])
swap(a, b);
parent[b] = a;
sz[a]+=sz[b];
if (rnk[a] == rnk[b])
rnk[a]++;
}
}
void solve(){
int n,m;
cin>>n>>m;
Dsu(n+1);
int mxSize = 1;
int forest = n;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
if( Find(u)!=Find(v) ){
Union(u,v);
mxSize = max({mxSize,sz[Find(u)],sz[Find(v)]});
forest--;
}
cout<<forest<<" "<<mxSize<<endl;
}
}
int main(){
FASTIO;
solve();
}