QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416220#8720. BFS 序 0zjx666WA 247ms117400kbC++141.4kb2024-05-21 17:54:132024-05-21 17:54:14

Judging History

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

  • [2024-05-21 17:54:14]
  • 评测
  • 测评结果:WA
  • 用时:247ms
  • 内存:117400kb
  • [2024-05-21 17:54:13]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=500010;
int d[maxn];
vector<int>e[maxn];
vector<int>s[maxn];
int a[maxn];
int f[maxn][30];
int vi[maxn];
void bfs(int x,int deep)
{
	d[x]=deep+1;
	for(int i=1;i<=25;i++)
	{
		f[x][i]=f[f[x][i-1]][i-1];
	}
	for(int i=0;i<e[x].size();i++)
	{
		f[e[x][i]][0]=x;
		bfs(e[x][i],d[x]);
	}
}
bool lca(int x,int y)
{
	for(int i=25;i>=0;i--)
	{
		if(f[x][i]!=f[y][i])
		{
			x=f[x][i];
			y=f[y][i];
		}
	}
	for(int i=0;i<s[y].size();i++)
	{
		if(s[y][i]==x)return 0;
	}
	e[x].push_back(y);
	return 1;
	
}
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	for(int i=2;i<=n;i++)
	{
		int x;
		cin>>x;
	    e[x].push_back(i);
	}
	f[1][0]=1;
	bfs(1,0); 
	int q;
    cin>>q;
    while(q--)
    {
    	for(int i=1;i<=n;i++)
		{
			s[i].clear();
			vi[i]=0;
		}
    	int m,v=1;
    	cin>>m;
    	for(int i=1;i<=m&&v;i++)
    	{
    		cin>>a[i];
    		if(vi[a[i]])v=0;
    		vi[a[i]]=1;
		}
		for(int i=2;i<=m&&v;i++)
		{
			if(d[a[i]]==d[a[i-1]])
			{
				if(!lca(a[i-1],a[i]))v=0;
			}
			else if(d[a[i]]>d[a[i-1]])continue;
			else 
			{
				v=0;
				break;
			}
		}
		if(!v)cout<<"NO\n";
		else cout<<"YES\n";
	}
	return 0;
}
/*
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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 247ms
memory: 117400kb

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

result:

wrong answer expected NO, found YES [87th token]