forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork Breakdown.cpp
56 lines (48 loc) · 1.2 KB
/
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
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+1;
const int maxM = 2e5;
typedef pair<int,int> pii;
int N, M, K, a, b, cnt, ds[maxN], ans[maxM];
pii edges[maxM], queries[maxM];
set<pii> S;
int find(int u){
if(ds[u] < 0) return u;
ds[u] = find(ds[u]);
return ds[u];
}
bool merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return false;
if(ds[u] < ds[v]) swap(u, v);
ds[v] += ds[u];
ds[u] = v;
return true;
}
int main(){
scanf("%d %d %d", &N, &M, &K);
fill(ds+1, ds+N+1, -1);
for(int i = 0; i < M; i++){
scanf("%d %d", &a, &b);
if(b > a) swap(a, b);
edges[i] = {a, b};
}
for(int i = 0; i < K; i++){
scanf("%d %d", &a, &b);
if(b > a) swap(a, b);
queries[i] = {a, b};
S.insert({a, b});
}
cnt = N;
for(int i = 0; i < M; i++)
if(S.find(edges[i]) == S.end())
if(merge(edges[i].first, edges[i].second))
cnt--;
for(int i = K-1; i >= 0; i--){
ans[i] = cnt;
if(merge(queries[i].first, queries[i].second))
cnt--;
}
for(int i = 0; i < K; i++)
printf("%d%c", ans[i], (" \n")[i==K-1]);
}