考虑时光倒流。
由于 A 操作只会给两个活跃的点连边,所以可以忽略,倒过来相当于没有删边操作。
然后只剩下加边,加活跃点,两种操作。每次暴力 DFS 即可。
时间复杂度 \(\mathcal O(n+Q)\)。
Code:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = 100005, M = 200005;
int n, Q;
struct node {
int ty, u, v;
node (){}
node (int _ty, int _u, int _v) {
ty = _ty, u = _u, v = _v;
}
} E[M], Que[M];
int ty[N];
int cnt, tot;
int ans[N];
vector <int> G[N];
void add(int u, int v) {
G[u].pb(v), G[v].pb(u);
}
void dfs(int u, int tim) {
if (ans[u]) return; ans[u] = tim;
for (int v : G[u]) dfs(v, tim);
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n >> Q;
for (int i = 1; i <= Q; ++i) {
char ch; int u, v; cin >> ch;
if (ch == 'D') { cin >> u, ty[u] = 1, Que[++tot] = node(1, u, 0); }
if (ch == 'A') { cin >> u >> v, E[++cnt] = node(0, u, v), Que[++tot] = node(0, 0, 0); }
if (ch == 'R') { cin >> u, Que[++tot] = node(-1, E[u].u, E[u].v), E[u].ty = 1; }
}
for (int i = 1; i <= cnt; ++i) if (!E[i].ty) add(E[i].u, E[i].v);
for (int i = 1; i <= n; ++i) if (!ty[i]) dfs(i, Q);
for (int i = Q; i; --i) {
if (Que[i].ty == 1) dfs(Que[i].u, i - 1);
else {
if (ans[Que[i].u]) dfs(Que[i].v, i - 1); if (ans[Que[i].v]) dfs(Que[i].u, i - 1);
add(Que[i].u, Que[i].v);
}
}
for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
return 0;
}
标签:node,ch,洛谷,ty,int,cin,tot,P8097
From: https://www.cnblogs.com/Kobe303/p/16893694.html