QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#392366#4000. Dynamic ReachabilityoscaryangTL 4ms18952kbC++173.4kb2024-04-17 15:10:252024-04-17 15:10:26

Judging History

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

  • [2024-04-17 15:10:26]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:18952kb
  • [2024-04-17 15:10:25]
  • 提交

answer

#include<bits/stdc++.h>

#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define tup tuple <int, int, int>
#define mkt make_tuple
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define lep(i, a, b) for(int i = (a); i >= (b); --i)

using namespace std;
bool st;

mt19937 gen(time(0));

inline int read() {
	int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x; 
}

const int N = 1e5 + 5, B = 1, SZ = 256;

int n, m, q, col[N], key[N];
int DFN, idx, cnt, scc, topf, deg[N], id[N], ID[N], dfn[N], low[N], stk[N], ins[N];
pii e[N];
tup o[N];
bitset < SZ > to[N];
vc <int> G[N], Gra[N];
vc <pii> K[N];

inline void tarjan (int x) {
	dfn[x] = low[x] = ++ DFN; stk[++ topf] = x; ins[x] = 1;
	for (auto y : G[x]) 
		if (!dfn[y]) tarjan (y), low[x] = min (low[x], low[y]);
		else if (ins[y]) low[x] = min (low[x], dfn[y]);
	if (dfn[x] == low[x]) {
		int z; ++ scc;
		do { z = stk[topf --]; ins[z] = 0; id[z] = scc; } while (x != z);
	}
}

inline void toposort () {
	queue <int> Q; 
	rep (i, 0, idx) {
		to[i].reset ();
		if (!deg[i]) Q.push (i);
	} 
	while (!Q.empty ()) {
		int x = Q.front (); Q.pop ();
		if (x < cnt) to[x][x] = 1;
		for (auto y : Gra[x]) if (!(--deg[y])) to[y] |= to[x], Q.push (y);
	}
}

inline void init (int L, int R) {
	// clear 
	DFN = scc = cnt = topf = 0;
	rep (i, 0, n) key[i] = -1, dfn[i] = id[i] = deg[i] = 0;
	rep (i, 0, n) G[i].clear (), Gra[i].clear (), K[i].clear ();
	
	// add edge
	vc <int> ban (m + 1, 0), edge;
	rep (i, L, R) {
		auto [u, v, w] = o[i];
		if (u == 1) ban[v] = 1, edge.pb (v);
	}
	rep (i, 1, m) if (col[i] && !ban[i]) {
		auto [x, y] = e[i];
		G[x].pb (y);
	}
	
	// tarjan
	rep (i, 1, n) if (!dfn[i]) tarjan (i);
	
	// remark
	auto mark = [&] (int x) -> void {
		x = id[x];
		if (key[x] == -1) key[x] = cnt ++;
	} ;
	rep (i, L, R) {
		auto [u, v, w] = o[i];
		if (u == 1) mark (e[v].first);
		else mark (v), mark (w);
	}
	idx = cnt - 1;
	rep (i, 1, scc) ID[i] = key[i] == -1 ? ++ idx : key[i];
	rep (i, 1, n) id[i] = ID[id[i]];
	rep (x, 1, n) for (auto y : G[x]) 
		if (id[x] != id[y]) Gra[id[y]].pb (id[x]), ++ deg[id[x]];
	for (auto u : edge) {
		auto [x, y] = e[u]; x = id[x]; y = id[y];
		K[x].pb (mkp (y, u));
	}
	
	// toposort
	toposort ();
}

inline int ask (int s, int t) {
	bitset < SZ > vis, nxt; 
	queue <int> Q; 
	Q.push (s); vis[s] = 1;
	
	auto add = [&] (int x) -> void {
		while (( nxt = (to[x] & (~vis)) ).any ()) {
			int y = nxt._Find_first ();
			Q.push (y); vis[y] = 1;
		}
	};
	
	while (!Q.empty ()) {
		int x = Q.front (); Q.pop ();
		if (x == t) return 1;
		add (x);
		for (auto [y, u] : K[x]) if (col[u]) add (y);
	}
	return 0;
}

bool ed;
signed main() {
	cerr << (&ed - &st) / 1024 / 1024 << "MB ---------------------------" << endl;
	
	n = read (); m = read (); q = read ();
	for (int i = 1, u, v; i <= m; i++) {
		u = read (); v = read ();
		e[i] = mkp (u, v); col[i] = 1;
	}
	for (int i = 1, u, v, w; i <= q; i++) {
		u = read (), v = read ();
		if (u == 2) w = read (); 
		o[i] = mkt (u, v, w);
	}
	
	rep (t, 1, q) {
		if ((t - 1) % B == 0) init (t, min (t + B - 1, q));
		auto [u, v, w] = o[t];
		if (u == 1) col[v] ^= 1;
		else puts (ask (id[v], id[w]) ? "YES" : "NO");
	}
	
	return 0;
}


详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 18952kb

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:


result: