QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592110 | #5455. TreeScript | Baiyu0123# | RE | 0ms | 3608kb | C++20 | 707b | 2024-09-26 20:48:00 | 2024-09-26 20:48:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+100;
vector<int> ed[maxn];
int f[maxn];
bool cmp(const int &x,const int &y) {
return f[x]>f[y];
}
void dfs(int u) {
f[u]=1;
for (int v:ed[u]) {
dfs(v);
}
sort(ed[u].begin(),ed[u].end(),cmp);
if (ed[u].size()==0) ;
else if (ed[u].size()==1) f[u]=max(f[u],f[ed[u][0]]);
else f[u]=max(f[ed[u][1]]+1,f[ed[u][0]]);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;cin>>T;
while (T--) {
int n,rt;cin>>n;
for (int i=1;i<=n;i++) {
int p;cin>>p;
if (p==0) rt=i;
else ed[p].push_back(i);
}
dfs(rt);
cout<<f[rt]<<'\n';
for (int i=1;i<=n;i++) ed[i].clear();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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
Runtime Error
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 ...