QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#419222#8720. BFS 序 0tzxydbyWA 186ms61252kbC++202.1kb2024-05-23 19:04:352024-05-23 19:07:57

Judging History

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

  • [2024-05-23 19:07:57]
  • 评测
  • 测评结果:WA
  • 用时:186ms
  • 内存:61252kb
  • [2024-05-23 19:04:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=500005;
int n,m,q,fa[N][20],dep[N],a[N],d[N],dfn[N],low[N],tim,in[N],st[N],tp,ok,vis[N];
vector<int>e[N],g[N],p;
void dfs(int u,int f)
{
	fa[u][0]=f;
	dep[u]=dep[f]+1;
	for(auto v:e[u])
		if(v!=f)
			dfs(v,u);
}
int lca(int u,int v)
{
	if(dep[u]<dep[v])
		swap(u,v);
	for(int i=18;i>=0;i--)
		if(dep[fa[u][i]]>=dep[v])
			u=fa[u][i];
	if(u==v)
		return u;
	for(int i=18;i>=0;i--)
		if(fa[u][i]!=fa[v][i])
			u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}
int cal(int u,int v)
{
	for(int i=18;i>=0;i--)
		if(dep[fa[u][i]]>dep[v])
			u=fa[u][i];
	return u;
}
void tarjan(int u)
{
	dfn[u]=low[u]=++tim;
	st[++tp]=u,in[u]=1;
	for(auto v:g[u])
	{
		if(!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(in[v])
			low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u])
	{
		int v;
		do
		{
			v=st[tp--];
			in[v]=0;
			if(v!=u)
				ok=0;
		}while(v!=u);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		e[x].push_back(i);
	}
	dfs(1,0);
	for(int j=1;j<=18;j++)
		for(int i=1;i<=n;i++)
			fa[i][j]=fa[fa[i][j-1]][j-1];
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&m);
		for(int i=1;i<=m;i++)
			scanf("%d",&a[i]);
		ok=1;
		for(int i=1;i<=m;i++)
		{
			if(vis[a[i]])
				ok=0;
			vis[a[i]]=1;
		}
		for(int i=1;i<=m;i++)
			vis[a[i]]=0;
		for(int i=1;i<=m;i++)
			d[i]=dep[a[i]];
		for(int i=1;i<m;i++)
			if(d[i]>d[i+1])
				ok=0;
		if(!ok)
		{
			puts("No");
			continue;
		}
		for(int i=1;i<=m;i++)
		{
			int j=i;
			while(j<m&&d[j+1]==d[i])
				j++;
			for(int k=i;k<j;k++)
			{
				int l=lca(a[k],a[k+1]);
				int u=cal(a[k],l),v=cal(a[k+1],l);
				p.push_back(u);
				p.push_back(v);
				g[u].push_back(v);
			}
			sort(p.begin(),p.end());
			p.erase(unique(p.begin(),p.end()),p.end());
			for(auto u:p)
				if(!dfn[u])
					tarjan(u);
			for(auto u:p)
			{
				g[u].clear();
				dfn[u]=low[u]=in[u]=0;
			}
			tim=tp=0;
			p.clear();
			i=j;
			if(!ok)
				break;
		}
		puts(ok?"Yes":"No");
	}
	return 0;
}

详细

Test #1:

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

input:

6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6

output:

No
Yes
Yes
No
No
No
No
No
No
Yes

result:

ok 10 token(s): yes count is 3, no count is 7

Test #2:

score: -100
Wrong Answer
time: 186ms
memory: 61252kb

input:

300000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

No
Yes
Yes
Yes
No
No
No
No
No
No
Yes
Yes
No
Yes
No
Yes
Yes
Yes
No
No
No
No
Yes
No
No
Yes
No
No
No
No
Yes
Yes
Yes
No
No
Yes
No
No
No
No
No
No
No
Yes
No
Yes
No
Yes
No
No
No
No
Yes
Yes
Yes
No
No
Yes
No
No
No
No
No
Yes
Yes
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
Yes
No
No
Yes
No
No
Yes
Yes
Yes
No
No
No
Y...

result:

wrong answer expected NO, found YES [181st token]