-
Notifications
You must be signed in to change notification settings - Fork 0
/
farmupdates.cpp
77 lines (64 loc) · 1.35 KB
/
farmupdates.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
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
using namespace std;
int N, Q;
bool active[100005], rmv[200005];
int res[100005];
vector<int> adj[100005];
vector<pii> edges;
vector<vector<int>> queries;
void dfs(int i, int val) {
if(res[i] != -1) return;
res[i] = val;
for(int to : adj[i]) {
dfs(to, val);
}
}
int main() {
cin >> N >> Q;
for(int i = 0; i < N; i++) {
res[i] = -1;
active[i] = true;
}
for(int q = 0; q < Q; q++) {
char t; cin >> t;
if(t == 'A') {
int x, y;
cin >> x >> y;
x--; y--;
edges.pb({x, y});
} else if(t == 'D') {
int x;
cin >> x; x--;
active[x] = false;
queries.pb({q, x});
} else if(t == 'R') {
int e; cin >> e; e--;
rmv[e] = true;
queries.pb({q, edges[e].f, edges[e].s});
}
}
for(int i = 0; i < edges.size(); i++) {
if(!rmv[i]) {
adj[edges[i].f].pb(edges[i].s);
adj[edges[i].s].pb(edges[i].f);
}
}
for(int i = 0; i < N; i++) {
if(active[i]) dfs(i, Q, -1);
}
for(int z = queries.size()-1; z >= 0; z--) {
vector<int> q = queries[z];
if(q.size() == 2) dfs(q[1], q[0]);
if(q.size() == 3) {
adj[q[1]].push_back(q[2]);
adj[q[2]].push_back(q[1]);
if(res[q[2]] != -1) dfs(q[1], q[0]);
if(res[q[1]] != -1) dfs(q[2], q[0]);
}
}
for(int i = 0; i < N; i++) cout << res[i] << endl;
}