QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#602862#8720. BFS 序 0liguo#WA 54ms44308kbC++203.0kb2024-10-01 13:13:332024-10-01 13:13:34

Judging History

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

  • [2024-10-01 13:13:34]
  • 评测
  • 测评结果:WA
  • 用时:54ms
  • 内存:44308kb
  • [2024-10-01 13:13:33]
  • 提交

answer

#include<cstdio>
#include<iostream>
using namespace std;
int n,m,t;
const int maxn=2e6+10;
struct node{
	int to,next;
}e[maxn];
int head[maxn],k=0;
void add(int a,int b){
	e[++k].next=head[a];
	e[k].to=b;
	head[a]=k;
}
struct nod{
	int l,r;
}bl[maxn];
int deep[maxn],h[maxn],zhan[maxn],cnt=0,fr[maxn];
void dfs(int id){
	zhan[++cnt]=id;
	fr[id]=cnt;
	bl[cnt].l=cnt;
	int tmp=cnt;
	for(int i=head[id];i;i=e[i].next){
		int tp=e[i].to;
		deep[tp]=deep[id]+1;
		dfs(tp);
	}
	bl[tmp].r=cnt;
}
struct ond{
	int fl,mx;
}tree1[maxn*4],tree2[maxn*4];
void down(int id){
	tree1[id*2].mx=tree1[id].fl;
	tree1[id*2].fl=tree1[id].fl;
	tree1[id*2+1].mx=tree1[id].fl;
	tree1[id*2+1].fl=tree1[id].fl;
	tree1[id].fl=-1;
}
void add(int id,int l,int r,int pl,int pr,int nm){
	if(l==pl&&r==pr){
		tree1[id].mx=nm;
		tree1[id].fl=nm;
		return;
	}
	if(tree1[id].fl>=0) down(id);
	int mid=(l+r)/2;
	if(pr<=mid) add(id*2,l,mid,pl,pr,nm);
	else if(pl>mid) add(id*2+1,mid+1,r,pl,pr,nm);
	else{
		add(id*2,l,mid,pl,mid,nm);
		add(id*2+1,mid+1,r,mid+1,pr,nm);
	}
	tree1[id].mx=max(tree1[id*2].mx,tree1[id*2+1].mx);
}
int find(int id,int l,int r,int pl,int pr){
	if(l==pl&&r==pr) return tree1[id].mx;
	if(tree1[id].fl>=0) down(id);
	int mid=(l+r)/2;
	if(pr<=mid) return find(id*2,l,mid,pl,pr);
	if(pl>mid) return find(id*2+1,mid+1,r,pl,pr);
	int a=find(id*2,l,mid,pl,mid);
	int b=find(id*2+1,mid+1,r,mid+1,pr);
	return max(a,b);
}
void downn(int id){
	tree2[id*2].mx=tree2[id].fl;
	tree2[id*2].fl=tree2[id].fl;
	tree2[id*2+1].mx=tree2[id].fl;
	tree2[id*2+1].fl=tree2[id].fl;
	tree2[id].fl=-1;
}
void ad(int id,int l,int r,int pl){
	tree2[id].mx++;
	if(l==r) return;
	if(tree2[id].fl==0) downn(id);
	int mid=(l+r)/2;
	if(pl<=mid) ad(id*2,l,mid,pl);
	else ad(id*2+1,mid+1,r,pl);
}
int findsm(int id,int l,int r,int pl,int pr){
	if(l==pl&&r==pr) return tree2[id].mx;
	if(tree2[id].fl==0) downn(id);
	int mid=(l+r)/2;
	if(pr<=mid) return findsm(id*2,l,mid,pl,pr);
	if(pl>mid) return findsm(id*2+1,mid+1,r,pl,pr);
	int a=findsm(id*2,l,mid,pl,mid);
	int b=findsm(id*2+1,mid+1,r,mid+1,pr);
	return a+b;
}
int bk[maxn];
int main(){
	scanf("%d",&n);
	int a;
	for(int i=2;i<=n;i++){
		scanf("%d",&a);
		add(a,i);
	}
	scanf("%d",&t);
	deep[1]=1;dfs(1);
	//for(int i=1;i<=n;i++) printf("%d ",deep[i]);
	while(t--){
		scanf("%d",&a);
		for(int i=1;i<=a;i++) scanf("%d",&h[i]);
		int fl=0;
		for(int i=1;i<a;i++){
			if(deep[h[i]]>deep[h[i+1]]){
				fl=1;
				break;
			}
		}
		if(fl){
			printf("No\n");
			continue;
		}
		int kt=n;bk[n]=1;
		tree1[1].mx=n;tree1[1].fl=n;
		tree2[1].mx=0;tree2[1].fl=0;
		for(int i=1;i<=a;i++){
			int l=bl[fr[h[i]]].l,r=bl[fr[h[i]]].r;
			int tmp=find(1,1,n,l,r);
			
			int tl=bl[bk[tmp]].l,tr=bl[bk[tmp]].r;
			int tsm=findsm(1,1,n,tl,tr);
			
			//printf("%d %d %d\n",kt,tmp,tsm);
			if(tmp-tsm==kt){
				kt--;
				bk[kt]=fr[h[i]];
				ad(1,1,n,l);
				add(1,1,n,l,r,kt);
			}
			else{
				fl=1;
				break;
			}
		}
		if(fl) printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 54ms
memory: 44308kb

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

result:

wrong answer expected YES, found NO [2nd token]