-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy path1677 - Network Breakdown.cpp
78 lines (75 loc) · 1.65 KB
/
1677 - Network Breakdown.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
/*
Problem Name: Network Breakdown
Problem Link: https://cses.fi/problemset/task/1677
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 200005;
int len[mxN], par[mxN];
void init() {
for (int i = 1; i < mxN; i++)
len[i] = 1, par[i] = i;
}
int find(int x) {
int s = x;
while(par[s] != s)
s = par[s];
while(par[x] != x) {
int k = x;
x = par[x];
par[k] = s;
}
return s;
}
void join(int x, int y) {
int s1 = find(x), s2 = find(y);
if (s1 == s2)
return;
if (len[s1] > len[s2])
swap(s1, s2);
par[s1] = s2;
len[s2] += len[s1];
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, k; cin>>n>>m>>k;
set<pair<int, int>> e;
init();
for (int i = 0; i < m; i++) {
int x, y; cin>>x>>y;
if (x > y) swap(x, y);
e.insert({x, y});
}
vector<pair<int,int>> q;
for (int i = 0; i < m; i++) {
int x,y; cin>>x>>y;
if (x > y) swap(x, y);
e.erase({x, y});
q.push_back({x, y});
}
int comp = n;
for (auto [x, y]: e) {
if (find(x) != find(y)) {
comp--;
join(x, y);
}
}
int ans[k] = {0};
for (int i = k-1; i >= 0; i--) {
ans[i] = comp;
auto [x, y] = q[i];
if (find(x) != find(y)) {
comp--;
join(x, y);
}
}
for (int i = 0; i < k; i++)
cout<<ans[i]<<' ';
}