QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#392337#4000. Dynamic Reachability251SecTL 1ms8172kbC++143.0kb2024-04-17 14:44:212024-04-17 14:44:21

Judging History

你现在查看的是最新测评结果

  • [2024-04-17 14:44:21]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:8172kb
  • [2024-04-17 14:44:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int B = 32;
struct Oper {
	int op, x, y;
} ope[100005];
struct Edge {
	int u, v, nxt;
} e[100005];
int head[50005], len;
void Insert(int u, int v) {
	e[++len] = { u, v, head[u] };
	head[u] = len;
}
bool del[100005];
int n, m, q;
int dfn[50005], low[50005], bel[50005], cnt, cti, st[50005], top;
bool vis[50005], col[50005];
void Tarjan(int u) {
	dfn[u] = low[u] = ++cti;
	st[++top] = u, vis[u] = true;
	for (int i = head[u]; i; i = e[i].nxt) {
		if (del[i]) continue;
		int v = e[i].v;
		if (!dfn[v]) {
			Tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if (vis[v]) low[u] = min(low[u], dfn[v]);
	}
	if (dfn[u] == low[u]) {
		cnt++;
		while (st[top + 1] != u) {
			int v = st[top--];
			vis[v] = false, bel[v] = cnt;
		}
	}
}
vector<int> eN[50005];
int id[50005], idR[50005], idC;
ull tar[50005], cur[50005];
void InsP(int u) {
	u = bel[u];
	if (id[u] == -1) {
		id[u] = idC;
		idR[idC++] = u;
		tar[u] |= 1ull << id[u];
	}
}
int main() {
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 1, u, v; i <= m; i++) {
		scanf("%d%d", &u, &v);
		Insert(u, v), col[i] = true;
	}
	for (int i = 1; i <= q; i++) {
		scanf("%d", &ope[i].op);
		if (ope[i].op == 1) scanf("%d", &ope[i].x);
		else scanf("%d%d", &ope[i].x, &ope[i].y);
	}
	for (int l = 1, r; l <= q; l += B) {
		r = min(l + B - 1, q);
		for (int i = l; i <= r; i++) if (ope[i].op == 1) del[ope[i].x] = true;
		memset(dfn, 0, sizeof(dfn)), memset(low, 0, sizeof(low)), memset(bel, 0, sizeof(bel)), memset(st, 0, sizeof(st)), memset(vis, 0, sizeof(vis));
		cnt = cti = top = 0;
		for (int i = 1; i <= n; i++) if (!dfn[i]) Tarjan(i);
		for (int u = 1; u <= n; u++) {
			for (int i = head[u]; i; i = e[i].nxt) {
				int v = e[i].v;
				if (!del[i] && col[i] && bel[u] != bel[v]) {
					eN[bel[u]].push_back(bel[v]);
				}
			}
		}
		memset(id, -1, sizeof(id)), idC = 0;
		memset(tar, 0, sizeof(tar));
		for (int i = l; i <= r; i++) {
			if (ope[i].op == 1) {
				auto &x = e[ope[i].x];
				InsP(x.u), InsP(x.v);
			}
			else InsP(ope[i].x), InsP(ope[i].y);
		}
		for (int u = 1; u <= cnt; u++) {
			for (int v : eN[u]) tar[u] |= tar[v];
		}
		for (int i = l; i <= r; i++) {
			if (ope[i].op == 1) col[ope[i].x] ^= 1, del[ope[i].x] = false;
			else {
				for (int i = 0; i < idC; i++) cur[i] = tar[idR[i]];
				for (int i = l; i <= r; i++) {
					if (ope[i].op == 1) {
						if (col[ope[i].x]) {
							auto &x = e[ope[i].x];
							cur[id[bel[x.u]]] |= 1ull << id[bel[x.v]];
						}
					}
				}
				ull nvis = -1;
				queue<int> q;
				q.push(id[bel[ope[i].x]]), nvis ^= 1ull << id[bel[ope[i].x]];
				while (!q.empty()) {
					int u = q.front(); q.pop();
					ull t = cur[u] & nvis;
					while (t) {
						int v = __lg(t & -t);
						q.push(v), nvis ^= 1ull << v;
						t ^= 1ull << v;
					}
				}
				puts(nvis >> id[bel[ope[i].y]] & 1 ? "NO" : "YES");
			}
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 8172kb

input:

5 6 7
1 2
1 3
2 4
3 4
3 5
4 5
2 1 5
2 2 3
1 3
1 4
2 1 4
1 3
2 1 5

output:

YES
NO
NO
YES

result:

ok 4 lines

Test #2:

score: -100
Time Limit Exceeded

input:

50000 100000 100000
36671 44121
25592 44321
13226 46463
13060 25694
14021 20087
22881 38333
34655 47774
22868 26462
31154 48710
27491 32365
5874 47497
17622 28600
1886 14193
22315 23656
14973 22704
1335 25384
22612 34915
2852 48213
23334 25519
24342 28784
6238 36125
14598 39494
33069 34250
2123 3059...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
YES
YES
NO
NO
NO
NO
YES
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
YES
...

result: