QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#790868 | #5455. TreeScript | Tom22l | WA | 0ms | 3840kb | C++17 | 1.1kb | 2024-11-28 15:48:02 | 2024-11-28 15:48:03 |
Judging History
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'