-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1671.cpp
More file actions
75 lines (64 loc) · 1.16 KB
/
1671.cpp
File metadata and controls
75 lines (64 loc) · 1.16 KB
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
#include <iostream>
using namespace std;
const int N = 100007;
int uf[N];
int wh[N];
int edge[N][2];
bool use[N];
int r[N];
int rs;
int res;
int n, m, a, b, q;
int qs[N];
int find(int i) {
if (uf[i] == i)
return i;
return (uf[i] = find(uf[i]));
}
void join(int i, int j) {
i = find(i);
j = find(j);
if (i == j)
return;
res--;
if (wh[i] < wh[j])
swap(i, j);
wh[j] += wh[i];
uf[i] = j;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
uf[i] = i;
wh[i] = 1;
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--; b--;
edge[i][0] = a;
edge[i][1] = b;
}
cin >> q;
for (int i = 0; i < q; i++) {
cin >> a;
a--;
use[a] = 1;
qs[i] = a;
}
res = n;
for (int i = 0; i < m; i++) {
if (use[i])
continue;
join(edge[i][0], edge[i][1]);
}
for (int i = q - 1; i >= 0; i--) {
r[i] = res;
join(edge[qs[i]][0], edge[qs[i]][1]);
}
for (int i = 0; i < q; i++) {
if (i)
cout << " ";
cout << r[i];
}
cout << endl;
}