QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#751899#9751. 覆盖一棵树huang123zsWA 1ms10064kbC++14900b2024-11-15 21:13:342024-11-15 21:13:34

Judging History

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

  • [2024-11-15 21:13:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:10064kb
  • [2024-11-15 21:13:34]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N=1.1e6;
inline LL read(){
	LL x=0,ff=1;
	char c=getchar();
	while(!isdigit(c)&&c!='-') c=getchar();
	if(c=='-') c=getchar(),ff=-1;
	while(isdigit(c)) x=x*10ll+c-'0',c=getchar();
	return x*ff;
}
int n;
int head[N],to[N*2],nxt[N*2],tot;
void add_edge(int x,int y){
	++tot;
	to[tot]=y;
	nxt[tot]=head[x];
	head[x]=tot;
}
int f[N];
void dfs(int x){
	if(!head[x]){
		f[x]=0;
		return ;
	}
	f[x]=n+1;
	for(int i=head[x];i;i=nxt[i]){
		int y=to[i];
		dfs(y);
		f[x]=min(f[x],f[y]);
	}
	f[x]++;
}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int casecnt=read();
	while(casecnt--){
		n=read();
		for(int i=1;i<=n;++i)
			head[i]=0;
		tot=0;
		for(int i=2;i<=n;++i){
			int x=read();
			add_edge(x,i);
		}
		dfs(1);
		printf("%d\n",f[1]);
	}
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 10064kb

input:

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

output:

2
7

result:

wrong answer 1st lines differ - expected: '3', found: '2'