-
Notifications
You must be signed in to change notification settings - Fork 0
/
lostnfound.cpp
71 lines (64 loc) · 1.19 KB
/
lostnfound.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
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
using namespace std;
int parent[100005], wt[100005];
multiset <int> s;
multiset <int> :: iterator it;
void initialise(int len) {
parent[0] = 0;
wt[0] = 1;
for (int i = 1; i <= len; i++) {
parent[i] = i;
wt[i] = 1;
s.insert(wt[i]);
}
}
int root(int i) {
while (i != parent[i]) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}
void merge (int a, int b) {
if (root(a) != root(b)) {
if (wt[root(a)] > wt[root(b)]) {
wt[root(b)] += wt[root(a)];
parent[root(a)] = root(b);
}
else {
wt[root(a)] += wt[root(b)];
parent[root(b)] = root(a);
}
}
}
void display(int len) {
for (int i = 1; i <= len; ++i) {
cout << parent[i] << " ";
}
cout << endl;
for (int i = 1; i <= len; ++i) {
cout << wt[i] << " ";
}
cout << endl;
}
int main() {
freopen("input.txt", "r", stdin);
int n, q, a, b;
scanf("%d %d", &n, &q);
initialise(n);
while (q--) {
scanf("%d %d", &a, &b);
if (root(a) != root(b)) {
s.erase(s.find(wt[root(a)]));
s.erase(s.find(wt[root(b)]));
merge(a, b);
s.insert(wt[root(a)]);
}
// display(n);
cout << (*(s.rbegin()) - *(s.begin())) << endl;
}
return 0;
}