QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#131603#4000. Dynamic ReachabilitykkioWA 0ms14188kbC++143.2kb2023-07-27 18:56:382023-07-27 18:56:41

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 18:56:41]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:14188kb
  • [2023-07-27 18:56:38]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optmize(2)
using namespace std;
const int maxn=50005+10,maxm=1e5+10,K=200;
int n,m,q;
struct edg{int u,v,c;}e[maxm];
struct qryy{int 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<K<<1> 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;
	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;}
		for(int v:F[u])
			if(!vis[v])q.push(v),vis[v]=1;
		for(int v:N[u])
			if(!vis[v])q.push(v),vis[v]=1;
	}
	return 0;
}

void solve(int L,int R)
{
	printf("%d %d\n",L,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);
		}
		else
		{
			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!=K<<1;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()
{
//	freopen("data.in","r",stdin);
//	freopen("4.out","w",stdout);
	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: 0
Wrong Answer
time: 0ms
memory: 14188kb

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:

1 7
YES
NO
NO
YES

result:

wrong answer 1st lines differ - expected: 'YES', found: '1 7'