QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#86700#4000. Dynamic ReachabilityfansizheTL 1ms10424kbC++233.1kb2023-03-10 16:31:182023-03-10 16:31:19

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-10 16:31:19]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:10424kb
  • [2023-03-10 16:31:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int B=32;
int n,m,q;
vector<pair<int,int> > edge[50005];
int ex[100005],ey[100005];
struct Query{
	int opt,x,y;
}Q[100005];
vector<int> vec;
int pos[50005];
int del[100005],vis[100005];
ull to[50005],to1[64];
int dfn[50005],low[50005],tot,ins[50005],st[50005],top,scc[50005],col;
vector<int> e[50005];
int cnt,po[50005],deg[50005];
queue<int> que;
void Tarjan(int x){
	dfn[x]=low[x]=++tot;
	st[++top]=x,ins[x]=1;
	for(auto e:edge[x])if(!vis[e.first]&&!del[e.first]){
		int y=e.second;
		if(!dfn[y])Tarjan(y),low[x]=min(low[x],low[y]);
		else if(ins[y])low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]){
		col++;int y;
		do{
			y=st[top--];
			scc[y]=col,ins[y]=0;
		}while(y!=x);
	}
}
int main(){
	scanf("%d%d%d",&n,&m,&q);
	for(int i=1;i<=m;i++){
		int x,y;scanf("%d%d",&x,&y);
		edge[x].push_back(make_pair(i,y));
		ex[i]=x,ey[i]=y;
	}
	for(int i=1;i<=q;i++){
		scanf("%d",&Q[i].opt);
		if(Q[i].opt==1)scanf("%d",&Q[i].x);
		else scanf("%d%d",&Q[i].x,&Q[i].y);
	}
	for(int ql=1,qr=B;ql<=q;ql+=B,qr+=B){
		qr=min(qr,q);vec.clear();
		for(int i=ql;i<=qr;i++)
			if(Q[i].opt==1)vec.push_back(ex[Q[i].x]),vec.push_back(ey[Q[i].x]),vis[Q[i].x]=1;
			else vec.push_back(Q[i].x),vec.push_back(Q[i].y);
		sort(vec.begin(),vec.end());
		vec.erase(unique(vec.begin(),vec.end()),vec.end());
		for(int i=0;i<vec.size();i++)pos[vec[i]]=i+1;
		for(int i=1;i<=n;i++)dfn[i]=0;col=tot=0;
		for(int i=1;i<=n;i++)if(!dfn[i])Tarjan(i);
		while(!que.empty())que.pop();
		for(int i=1;i<=col;i++)e[i].clear();
		for(int i=1;i<=m;i++)if(!del[i]&&!vis[i])
			if(scc[ex[i]]!=scc[ey[i]])e[scc[ex[i]]].push_back(scc[ey[i]]),deg[scc[ey[i]]]++;
		for(int i=1;i<=col;i++)if(!deg[i])que.push(i);
		cnt=0;
		while(!que.empty()){
			int x=que.front();que.pop();
			po[++cnt]=x;
			for(int y:e[x])if(!--deg[y])que.push(y);
		}
		assert(cnt==col);
		for(int i=1;i<=col;i++)to[i]=0;
		for(int i=0;i<vec.size();i++)to[scc[vec[i]]]|=1ull<<i;
		for(int i=col;i>=1;i--)
			for(int y:e[po[i]])to[po[i]]|=to[y];
		for(int i=0;i<vec.size();i++){
			to1[i]=0;
			for(int j=0;j<vec.size();j++)
				if(to[scc[vec[i]]]&(1ull<<j))to1[i]|=1ull<<j;
		}
		for(int i=ql;i<=qr;i++)if(Q[i].opt==1)if(vis[Q[i].x]){
			vis[Q[i].x]=0;
			if(!del[Q[i].x])
				if(!(to[scc[ex[Q[i].x]]]&(1ull<<pos[ey[Q[i].x]]-1)))
					to1[pos[ex[Q[i].x]]-1]|=1ull<<pos[ey[Q[i].x]]-1;
		}
		for(int i=ql;i<=qr;i++)
			if(Q[i].opt==1){
				del[Q[i].x]^=1;
				if(!(to[scc[ex[Q[i].x]]]&(1ull<<pos[ey[Q[i].x]]-1)))
					to1[pos[ex[Q[i].x]]-1]^=1ull<<pos[ey[Q[i].x]]-1;
			}else{
				while(!que.empty())que.pop();
				int S=pos[Q[i].x]-1,T=pos[Q[i].y]-1;
				que.push(S);
				ull now=1ull<<S;
				while(!que.empty()){
					int x=que.front();que.pop();
					ull sta=to1[x]^(to1[x]&now);
					while(sta){
						int y=__builtin_ctzll(sta);
						sta^=1ull<<y,now|=1ull<<y;
						que.push(y);
						if(y==T)break;
					}
					if(now&(1ull<<T))break;
				}
				if(now&(1ull<<T))puts("YES");
				else puts("NO");
			}
		for(int x:vec)pos[x]=0;
	}
	return 0;
}

详细

Test #1:

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

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
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
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
...

result: