QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#419229#8720. BFS 序 0tzxydbyWA 194ms56148kbC++202.0kb2024-05-23 19:14:342024-05-23 19:14:36

Judging History

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

  • [2024-05-23 19:14:36]
  • 评测
  • 测评结果:WA
  • 用时:194ms
  • 内存:56148kb
  • [2024-05-23 19:14:34]
  • 提交

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],ok,vis[N],c[N];
vector<int>e[N],g[N],p;
queue<int>qu;
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;
}
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);
				c[v]++;
			}
			sort(p.begin(),p.end());
			p.erase(unique(p.begin(),p.end()),p.end());
			for(auto u:p)
				if(!c[u])
					qu.push(u);
			while(qu.size())
			{
				int u=qu.front();
				qu.pop();
				for(auto v:g[u])
				{
					c[v]--;
					if(!c[v])
						qu.push(v);
				}
			}
			for(auto u:p)
				if(c[u])
					ok=0;
			while(qu.size())
				qu.pop();
			for(auto u:p)
			{
				g[u].clear();
				c[u]=0;
			}
			p.clear();
			i=j;
			if(!ok)
				break;
		}
		puts(ok?"Yes":"No");
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11872kb

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: 194ms
memory: 56148kb

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]