QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#790876 | #5455. TreeScript | Tom22l | WA | 7ms | 6876kb | C++17 | 1.1kb | 2024-11-28 15:49:38 | 2024-11-28 15:49:39 |
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;
while(!q.empty())q.pop();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3732kb
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: 7ms
memory: 6876kb
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:
5 5 2 4 4 5 5 7 6 6 4 5 6 5 4 4 6 6 4 7 5 3 6 5 5 5 6 3 5 6 2 6 5 4 7 4 3 3 3 6 6 5 5 3 5 5 6 5 5 5 3 2 4 5 7 4 6 5 4 4 5 5 2 6 7 4 3 6 7 4 4 7 7 5 5 7 2 3 4 1 1 6 6 6 3 4 6 6 7 5 5 6 4 4 4 3 5 3 5 5 5 7 4 6 6 6 3 2 7 4 4 5 4 5 5 5 2 3 4 5 3 6 3 7 3 5 4 4 6 4 6 4 5 4 4 4 6 4 4 2 3 4 3 2 7 5 4 2 5 7 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '5'