QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#176929#5455. TreeScriptCidoai#WA 2ms3292kbC++141.3kb2023-09-12 10:49:372023-09-12 10:49:37

Judging History

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

  • [2023-09-12 10:49:37]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3292kb
  • [2023-09-12 10:49:37]
  • 提交

answer

#include<cstdio>
inline int read(){
	int x=0;
	int f=0,ch=0;
	while(ch<48||ch>57) f=(ch=='-'),ch=getchar();
	while(ch>47&&ch<58) x=(x<<3)+(x<<1)+(ch&15),ch=getchar();
	return f?-x:x;
}
inline void write(int x,char end=' '){
	if(x==0){
		putchar('0');
		putchar(end);
		return;
	}
	if(x<0) putchar('-'),x=-x;
	int ch[70]={0},cnt=0;
	while(x){
		ch[cnt++]=(int)(x%10);
		x/=10;
	}
	while(cnt--) putchar(ch[cnt]+48);
	putchar(end);
}
inline int max(int x,int y){return x>y?x:y;}
const int N=2e5+5;
int p[N],leaf[N];
struct node{
	int to,nxt;
}e[N<<1];
int head[N],cnt;
inline void add(int u,int v){
	e[++cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
int can,ans;
inline void dfs(int u){
	if(!can) ++ans;
	else --can;
	int son=0;
	for(int i=head[u];i;i=e[i].nxt){
		++son;
	}
	bool flag=1;
	for(int i=head[u];i;i=e[i].nxt){
		--son;
		int v=e[i].to;
		if(son==0){
			++can;
			flag=0;
			dfs(v);
		}
		else{
			dfs(v);
		}
	}
	can+=flag;
}
int n;
inline void clr(){
	for(int i=1;i<=cnt;++i) e[i]={0,0};
	for(int i=1;i<=n;++i) head[i]=0;
	cnt=0;
	for(int i=1;i<=n;++i) leaf[i]=0,p[i]=0;
}
int main(){
	int T=read();
	while(T--){
		n=read();
		for(int i=1;i<=n;++i){
			p[i]=read();
			add(p[i],i);
			leaf[p[i]]=1;
		}
		ans=0;
		can=0;
		dfs(1);
		write(ans,'\n');
		clr();
	}
	return 0;
}

詳細信息

Test #1:

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

input:

2
3
0 1 2
7
0 1 2 2 1 4 1

output:

1
2

result:

ok 2 number(s): "1 2"

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 1300kb

input:

1000
197
0 1 1 2 1 4 1 5 8 3 5 1 4 7 12 14 4 7 10 9 12 11 16 10 21 19 22 17 25 13 28 9 5 15 26 26 33 25 15 1 35 6 32 17 37 8 19 43 19 27 29 9 30 6 31 27 35 35 37 13 28 38 57 31 38 8 22 14 33 9 18 62 52 37 10 19 22 60 54 12 38 59 64 65 80 82 28 60 85 78 27 25 71 14 52 6 59 14 87 32 33 41 59 41 88 38 ...

output:

6
6
3
5
4
5
6
8
6
6
5
5
6
6
4
5
6
7
4
7
6
3
6
6
6
5
6
4
5
7
2
6
6
4
7
5
4
3
3
6
6
6
6
4
5
5
7
6
5
5
4
3
5
6
8
5
6
6
5
5
6
6
3
6
7
5
4
7
7
4
5
7
7
6
6
7
2
3
4
1
1
7
6
6
4
5
6
7
7
6
5
6
5
4
5
4
5
4
5
5
6
7
4
6
7
6
3
3
8
5
5
5
5
5
6
5
3
3
5
5
3
7
4
7
3
5
5
4
7
4
7
4
5
4
5
4
6
5
5
3
4
5
3
2
8
6
4
3
5
8
...

result:

wrong answer 1st numbers differ - expected: '4', found: '6'