检测到时间不够了就 putsNO,其他时候暴力,洛谷数据 100,ccf 80。
#include <bits/stdc++.h>
using namespace std;
#define TM 1.1
inline int read() {
char c; bool f = true;
while (!isdigit(c = getchar())) f = c != '-';
int x = c ^ 48;
while (isdigit(c = getchar())) x = x * 10 + (c ^ 48);
return f ? x : -x;
}
const int N = 500010;
int n, m, q, opt, x, y, cnt;
unordered_set<int> in[N], out[N], del[N];
int main() {
n = read(); m = read();
for (int i = 1; i <= m; i++) {
x = read(); y = read();
out[x].insert(y);
in[y].insert(x);
}
for (int i = 1; i <= n; i++) {
cnt += (out[i].size() == 1);
}
unsigned clk = clock();
q = read();
for (int i = 1; i <= q; i++) {
opt = read();
if (opt == 1) {
x = read(); y = read();
if (clock() - clk >= TM * CLOCKS_PER_SEC) {
puts("NO");
continue;
}
cnt -= (out[x].size() == 1);
out[x].erase(y);
cnt += (out[x].size() == 1);
in[y].erase(x);
del[y].insert(x);
} else if (opt == 2) {
x = read();
if (clock() - clk >= TM * CLOCKS_PER_SEC) {
puts("NO");
continue;
}
for (auto y : in[x]) {
cnt -= (out[y].size() == 1);
out[y].erase(x);
cnt += (out[y].size() == 1);
del[x].insert(y);
}
in[x].clear();
} else if (opt == 3) {
x = read(); y = read();
if (clock() - clk >= TM * CLOCKS_PER_SEC) {
puts("NO");
continue;
}
cnt -= (out[x].size() == 1);
out[x].insert(y);
cnt += (out[x].size() == 1);
in[y].insert(x);
del[y].erase(x);
} else if (opt == 4) {
x = read();
if (clock() - clk >= TM * CLOCKS_PER_SEC) {
puts("NO");
continue;
}
for (auto y : del[x]) {
cnt -= (out[y].size() == 1);
out[y].insert(x);
cnt += (out[y].size() == 1);
in[x].insert(y);
}
del[x].clear();
}
puts(cnt == n ? "YES" : "NO");
// cout << "? " << cnt << endl;
}
return 0;
}