QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#732899 | #5455. TreeScript | ChensenCHX# | WA | 3ms | 3728kb | C++23 | 1.0kb | 2024-11-10 16:21:36 | 2024-11-10 16:21:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN=200010;
int maxnum[maxN];
vector<int> edge[maxN];
bool dfs(ll x,ll fa)
{
if(edge[x].size()==1 && edge[x][0]==fa) return 0;
ll tmp=0;
for(auto i:edge[x])
{
if(i==fa) continue;
if(dfs(i,x)) tmp++;
}
if(edge[x].size()==2 && x==0 ) maxnum[x]=max(maxnum[x],1);
if(edge[x].size()>2) maxnum[x]=max(maxnum[x],1);
//cout<<x<<"~~~"<<maxnum[x]<<endl;
maxnum[fa]=max(maxnum[fa],maxnum[x]+(tmp>1?1:0));
if(edge[x].size()>2) return 1;
else return 0;
}
void solve()
{
int n,thefa;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>thefa;
edge[i].push_back(thefa);
edge[thefa].push_back(i);
}
dfs(0,0);
cout<<maxnum[0]+1<<endl;
for(int i=0;i<=n;i++)
{
maxnum[i]=0;
edge[i].clear();
}
return;
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3576kb
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: 3ms
memory: 3728kb
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 7 3 6 5 6 8 8 4 5 5 8 9 7 3 5 7 9 3 8 8 2 6 6 7 5 10 4 6 8 2 12 6 3 9 6 4 4 3 6 7 4 6 3 5 5 8 9 5 5 4 2 7 7 9 7 5 7 5 7 9 8 4 7 7 5 4 7 8 6 7 7 10 5 8 10 2 4 7 1 1 8 8 6 4 5 7 10 9 8 8 8 5 6 5 4 10 4 6 5 8 8 8 8 8 8 3 3 9 5 7 6 4 4 6 6 3 2 5 8 3 7 3 8 5 5 6 4 8 7 13 6 8 4 6 2 10 6 6 3 2 6 3 2 9 7 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '5'