QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#790868#5455. TreeScriptTom22lWA 0ms3840kbC++171.1kb2024-11-28 15:48:022024-11-28 15:48:03

Judging History

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

  • [2024-11-28 15:48:03]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3840kb
  • [2024-11-28 15:48:02]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
int Read(){
	int x=0;
	char ch=getchar();bool f=0;
	while(ch<'0'||ch>'9') if(ch=='-')f=1,ch=getchar(); else if(ch==EOF)return 0; else ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
int h[200005];
int nxt[200005];
int to[200005];
int cnt;
int s[200005];
void link(int x,int y){
	nxt[++cnt]=h[x];
	h[x]=cnt;
	to[cnt]=y;
	s[x]++;
	return;
}
queue<int> q;
int ans;
void dfs(int x,int fa){
	if((!(q.size()==1&&q.front()==fa))&&(!q.empty())){
		int p=q.front();q.pop();
		if(p==fa){
			q.push(p);q.pop();
		}
	}else ans++;
//	cout<<x<<' '<<ans<<endl;
	if(!h[x]) q.push(x);
	for(int i=h[x];i;i=nxt[i]){
		if(!nxt[i]) q.push(x);
		dfs(to[i],x);
	}
	return;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T=Read();
	while(T--){
		int n=Read();
		ans=0;
		for(int i=1;i<=n;i++){
			int x=Read();
			link(x,i);
		}q.push(0);
		dfs(1,0);
		printf("%lld\n",ans);
		for(int i=0;i<=n;i++) h[i]=0;
	}
	return 0; 
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3840kb

input:

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

output:

1
0

result:

wrong answer 2nd numbers differ - expected: '2', found: '0'