QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#131582#4000. Dynamic ReachabilitykkioRE 0ms7928kbC++143.0kb2023-07-27 17:57:342023-07-27 17:57:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-27 17:57:34]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:7928kb
  • [2023-07-27 17:57:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn=5005+10,maxm=1e5+10,K=500;
int n,m,q,qid,pid;
struct edg{int u,v,c;}e[maxm];
struct qryy{int t,op,u,v;}opt[maxm];
struct edge{int to,next;}E[maxm];
int head[maxn],tot;
inline void add(int u,int v)
{
	E[++tot]={v,head[u]};
	head[u]=tot;
}
int dfn[maxn],times,low[maxn],stk[maxn],top;
bool ins[maxn];
int scc[maxn],nid;
void tarjan(int u)
{
	dfn[u]=low[u]=++times;
	ins[u]=1;stk[++top]=u;
	for(int i=head[u];i;i=E[i].next)
	{
		int v=E[i].to;
		if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}
		else if(ins[v]){low[u]=min(low[u],dfn[v]);}
	}
	if(low[u]==dfn[u])
	{
		++nid;int x=0;
		while(x!=u)
		{
			x=stk[top];top--;
			ins[x]=0;scc[x]=nid;
		}
	}
}
vector<int> Ip,Ie;
bool tag[maxm];
int deg[maxn];
bitset<1005> f[maxn];
vector<int> G[maxn],F[maxn],N[maxn];
void work()
{
	times=0;for(int i=1;i<=n;i++)dfn[i]=low[i]=scc[i]=0;nid=0;times=0;
	for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
	for(int i=1;i<=nid;i++)f[i].reset(),deg[i]=0,G[i].clear();
	for(int i=1;i<=n;i++)
	for(int j=head[i];j;j=E[j].next)
	{
		int to=E[j].to;
		if(scc[i]==scc[to])continue;
		G[scc[to]].push_back(scc[i]);deg[scc[i]]++;
	}
	for(int i=0;i<Ip.size();i++)f[scc[Ip[i]]].set(i);
	queue<int> q;
	for(int i=1;i<=nid;i++)if(!deg[i])q.push(i);
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int v:G[u])
		{
			f[v]|=f[u];
			deg[v]--;
			if(!deg[v])q.push(v);
		}
	}
}

bool vis[maxn];
bool check(int s,int t)
{
	queue<int> q;
	q.push(s);
	for(int v:Ip)vis[v]=0;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		if(u==t)return 1;
		if(vis[u])continue;
		vis[u]=1;
		for(int v:F[u])
			q.push(v);
		for(int v:N[u])
			q.push(v);
	}
	return 0;
}

void solve(int L,int R)
{
	Ip.clear();Ie.clear();
	for(int i=L;i<=R;i++)
	{
		if(opt[i].op==2)
		{
			Ip.push_back(opt[i].u);
			Ip.push_back(opt[i].v);
		}
		int eg=opt[i].u;
		tag[eg]=1;
		Ip.push_back(e[eg].u);
		Ip.push_back(e[eg].v);
		Ie.push_back(eg);
	}
	sort(Ip.begin(),Ip.end());Ip.resize(unique(Ip.begin(),Ip.end())-Ip.begin());
	sort(Ie.begin(),Ie.end());Ie.resize(unique(Ie.begin(),Ie.end())-Ie.begin());
	for(int i=1;i<=n;i++)head[i]=0;tot=0;
	for(int i=1;i<=m;i++)if(!tag[i]&&e[i].c)add(e[i].u,e[i].v);
	work();
	for(int v:Ip)F[v].clear();
	for(int i=0;i<Ip.size();i++)
	{
		int sc=scc[Ip[i]];
		for(int j=f[sc]._Find_first();j!=1005;j=f[sc]._Find_next(j))
			if(i!=j)F[Ip[i]].push_back(Ip[j]);
	}
	for(int i=L;i<=R;i++)
	{
		if(opt[i].op==1)
		{
			int eg=opt[i].u;
			tag[eg]=0;
			e[eg].c^=1;
		}
		else 
		{
			for(int v:Ip)N[v].clear();
			for(int ep:Ie)
				if(e[ep].c)
					N[e[ep].u].push_back(e[ep].v);
			if(check(opt[i].u,opt[i].v))puts("YES");
			else puts("NO");
		}
	}
}
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>q;
	for(int i=1;i<=m;i++)
		cin>>e[i].u>>e[i].v,e[i].c=1;
	for(int i=1;i<=q;i++)
	{
		cin>>opt[i].op>>opt[i].u;
		if(opt[i].op==2)cin>>opt[i].v;
	}
	for(int i=1;i<=q;i+=K)solve(i,min(q,i+K-1));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7928kb

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
Runtime Error

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: