-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnansiCobweb.cpp
117 lines (99 loc) · 2 KB
/
AnansiCobweb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <bits/stdc++.h>
#define input freopen("in.txt", "r", stdin)
#define output freopen("out.txt", "w", stdout)
using namespace std;
int parent[100000];
int rango[100000];
int n, m, grupos;
void init()
{
for (int i = 0; i <= n; i++)
{
parent[i] = i;
rango[i] = 0;
}
}
int find(int x)
{
if (x == parent[x])
{
return x;
}
else
{
parent[x] = find(parent[x]);
return parent[x];
}
}
void unionRango(int x, int y)
{
int xRaiz = find(x);
int yRaiz = find(y);
if (xRaiz == yRaiz)
{
return;
}
if (rango[xRaiz] > rango[yRaiz])
{
parent[yRaiz] = xRaiz;
}
else
{
parent[xRaiz] = yRaiz;
if (rango[xRaiz] == rango[yRaiz])
{
rango[yRaiz]++;
}
}
grupos--;
}
int main()
{
input;
cin >> n >> m;
grupos = n;
init();
vector<pair<int, int>> hilos(m);
vector<bool> hilosAEliminarB(m);
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
hilos[i] = make_pair(x, y);
}
int NhilosAEliminar;
cin >> NhilosAEliminar;
vector<int> hilosAEliminar(NhilosAEliminar);
stack<int> answer;
for (int i = 0; i < NhilosAEliminar; i++)
{
cin >> hilosAEliminar[i];
hilosAEliminar[i]--;
hilosAEliminarB[hilosAEliminar[i]] = 1;
}
for (int i = 0; i < m; i++)
{
if (!hilosAEliminarB[i])
{
int ini = hilos[hilosAEliminar[i]].first;
int fin = hilos[hilosAEliminar[i]].second;
unionRango(ini, fin);
}
}
answer.push(grupos);
for (int i = NhilosAEliminar; i > 1; i--)
{
int ini = hilos[hilosAEliminar[i-1]].first;
int fin = hilos[hilosAEliminar[i-1]].second;
unionRango(ini, fin);
answer.push(grupos);
}
cout<<answer.top();
answer.pop();
while (!answer.empty())
{
cout<<" "<<answer.top();
answer.pop();
}
return 0;
}