QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#602925#8720. BFS 序 0UESTC_xxx#AC ✓118ms82788kbC++201.6kb2024-10-01 13:32:562024-10-01 13:33:05

Judging History

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

  • [2024-10-01 13:33:05]
  • 评测
  • 测评结果:AC
  • 用时:118ms
  • 内存:82788kb
  • [2024-10-01 13:32:56]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
int n,fa[30][500005],d[500005],q,p0,m,c[500005],flag,t[500005],tot,b[800005],a[500005];
vector<int>e[500005],g[500005];
bool ins[500005],vis[500005];
void dfs(int u){
	for(int i=0;i<e[u].size();++i){
		int v=e[u][i];
		d[v]=d[u]+1;
		dfs(v);
	}
}
int lca(int x,int y){
	if(d[x]<d[y]) swap(x,y);
	for(int i=20;i>=0;--i) if(d[fa[i][x]]>=d[y]) x=fa[i][x];
	if(x==y) return x;
	for(int i=20;i>=0;--i) if(fa[i][x]!=fa[i][y]) x=fa[i][x],y=fa[i][y];
	return fa[0][x];
}
void dfs0(int u){
	ins[u]=vis[u]=1;
	for(int i=0;i<g[u].size();++i){
		int v=g[u][i];
		if(ins[v]) flag=1;
		if(!vis[v]) dfs0(v);
	}
	ins[u]=0;
}
int main(){
	scanf("%d",&n);
	for(int i=2;i<=n;++i){
		scanf("%d",&fa[0][i]);
		e[fa[0][i]].push_back(i);
	}
	scanf("%d",&q);
	d[1]=1;
	dfs(1);
	for(int i=1;i<=22;++i) for(int j=1;j<=n;++j) fa[i][j]=fa[i-1][fa[i-1][j]];
	while(q--){
		scanf("%d",&m);
		flag=p0=tot=0;
		for(int j=1;j<=m;++j){
			scanf("%d",&a[j]);
			if(d[a[j]]<d[a[j-1]]||a[j]==a[j-1]) flag=1;
		}
		if(m==1){
			printf("Yes\n");
			continue;
		}
		for(int j=1;j<m;++j){
			int x=a[j],y=a[j+1];
			if(d[x]!=d[y]) continue;
			for(int k=20;k>=0;--k) if(fa[k][x]!=fa[k][y]) x=fa[k][x],y=fa[k][y];
			tot++,b[tot]=x,tot++,b[tot]=y;
			g[x].push_back(y);
		}
		for(int j=1;j<=tot;++j){
			if(vis[b[j]]) continue;
			dfs0(b[j]);
		}
		if(flag) printf("No\n");
		else printf("Yes\n");
		for(int j=1;j<=tot;++j){
			g[b[j]].clear();
			vis[b[j]]=0;
		}
	}
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 117ms
memory: 82080kb

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:

ok 254 token(s): yes count is 89, no count is 165

Test #3:

score: 0
Accepted
time: 118ms
memory: 80196kb

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:

Yes
No
No
Yes
No
No
Yes
Yes
No
Yes
No
Yes
No
No
No
Yes
No
No
No
No
Yes
No
No
No
No
No
Yes
Yes
No
No
No
Yes
No
No
No
No
No
Yes
Yes
Yes
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
Yes
No
No
No
Yes
No
No
Yes
No
Yes
Yes
No
No
No
Yes
No
Yes
Yes
Yes
No
Yes
No
Yes
Yes
Yes
Yes
Yes
No
Yes
No
No
No
No
No
Yes
No
...

result:

ok 266 token(s): yes count is 102, no count is 164

Test #4:

score: 0
Accepted
time: 116ms
memory: 82788kb

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
No
Yes
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
No
No
No
No
No
Yes
No
No
No
Yes
Yes
No
No
No
Yes
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
Yes
No
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
No
No
No
Yes
Yes
No
Yes
Yes
No
No
No...

result:

ok 252 token(s): yes count is 87, no count is 165

Test #5:

score: 0
Accepted
time: 3ms
memory: 59108kb

input:

10
1 1 3 3 1 3 1 3 9
10000
4 3 5 6 9
5 3 1 4 9 6
5 6 4 3 5 7
28 8 3 4 3 1 6 3 5 8 7 2 4 4 4 2 7 5 9 2 4 10 8 4 10 3 4 5 7
3 5 2 9
6 4 7 2 3 9 5
5 9 2 5 6 1
12 3 5 8 8 1 3 5 8 4 5 9 1
29 3 9 9 1 5 8 5 5 6 10 8 8 4 8 4 2 8 10 5 7 3 7 1 8 4 7 5 1 6
7 5 9 8 3 2 1 6
4 4 1 9 5
22 10 9 5 4 5 8 10 3 2 1 10 ...

output:

No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
No
No
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
No
No
No
Yes
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
No
No
No
...

result:

ok 10000 token(s): yes count is 1058, no count is 8942

Test #6:

score: 0
Accepted
time: 4ms
memory: 57016kb

input:

9
1 2 1 2 2 4 2 6
10000
6 5 1 4 3 7 8
14 6 8 7 9 7 5 1 8 3 6 3 8 5 2
2 6 9
17 7 8 6 5 2 1 5 5 9 3 4 1 5 2 7 2 5
3 8 6 5
5 3 9 1 6 8
8 3 5 8 7 9 4 6 2
4 8 9 6 5
7 4 7 2 9 3 8 1
6 1 4 9 5 6 2
4 7 5 4 3
5 9 5 2 1 3
4 4 5 7 3
3 9 1 8
4 4 2 8 6
26 7 4 7 6 1 7 9 4 5 5 8 9 8 2 4 8 9 6 2 8 6 4 5 5 4 4
45 3 ...

output:

No
No
Yes
No
Yes
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
Yes
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
Yes
No
No
No
No
No
No
No
N...

result:

ok 10000 token(s): yes count is 1492, no count is 8508

Test #7:

score: 0
Accepted
time: 8ms
memory: 57080kb

input:

8
1 1 2 4 2 4 6
10000
5 2 6 6 2 1
3 7 3 8
2 8 1
5 7 5 1 2 7
3 2 3 5
3 8 4 3
5 8 2 4 5 3
4 3 2 1 8
6 6 8 4 5 3 1
6 3 1 2 7 4 6
4 8 6 4 3
5 4 7 6 8 5
11 2 1 6 5 2 7 1 3 3 5 4
4 6 1 7 2
7 6 2 7 8 4 1 3
3 4 6 7
3 7 1 6
38 3 4 6 6 3 4 8 1 5 3 1 8 8 6 2 3 6 7 8 8 4 2 3 7 3 3 6 7 7 5 7 5 8 8 5 4 5 7
1 8
5 ...

output:

No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
Yes
No
No
Yes
No
No
No
No
No
Yes
No
Yes
No
No
No
Yes
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
...

result:

ok 10000 token(s): yes count is 1568, no count is 8432

Test #8:

score: 0
Accepted
time: 4ms
memory: 59060kb

input:

7
1 2 3 1 2 5
10000
5 1 7 3 5 6
5 5 3 2 7 4
4 5 2 6 4
12 3 1 5 5 6 6 3 5 3 5 3 3
11 2 3 2 6 7 3 6 6 3 3 3
2 3 2
6 1 3 4 2 6 7
3 2 7 1
6 5 2 7 3 4 1
27 2 5 6 1 7 1 7 1 4 6 7 1 6 2 1 7 1 3 4 4 7 7 2 6 4 4 3
2 5 6
1 7
3 3 1 4
4 1 7 3 4
3 4 2 1
4 4 1 6 7
4 6 2 7 4
4 5 1 6 4
1 3
5 1 2 3 5 7
5 2 3 7 6 5
1...

output:

No
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
Yes
No
No
No
No
Yes
No
No
No
No
Yes
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
Yes
No
Yes
Yes
No
Yes
Yes
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Ye...

result:

ok 10000 token(s): yes count is 2205, no count is 7795

Extra Test:

score: 0
Extra Test Passed