QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#774025 | #5455. TreeScript | Yurily# | WA | 19ms | 5884kb | C++20 | 1.2kb | 2024-11-23 11:20:16 | 2024-11-23 11:20:16 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5;
int n, rt, p[N + 3];
vector<int>g[N + 3];
int d[N + 3], md[N + 3];
void dfs0(int u){
md[u] = d[u] = d[p[u]] + 1;
for(int i = 0, v; i < g[u].size(); i++){
v = g[u][i];
dfs0(v);
md[u] = max(md[u], md[v]);
}
}
bool cmp(int i, int j){
return md[i] < md[j];
}
int ans;
void dfs(int u, int x){
ans = max(ans, x);
for(int i = 0, v; i < g[u].size(); i++){
v = g[u][i];
if(i < g[u].size() - 1)
dfs(v, x + 1);
else
dfs(v, x);
}
}
void sol(){
scanf("%d", &n);
for(int i = 1; i <= n; i++)
g[i].clear();
rt = -1;
for(int i = 1; i <= n; i++){
scanf("%d", &p[i]);
g[p[i]].push_back(i);
if(p[i] == 0)
rt = i;
}
d[0] = 0;
dfs0(rt);
for(int i = 1; i <= n; i++)
sort(g[i].begin(), g[i].end(), cmp);
ans = 0;
dfs(rt, 0);
printf("%d\n", ans + 1);
}
int main(){
int T;
scanf("%d", &T);
while(T--)
sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3904kb
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: 19ms
memory: 5884kb
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 3 4 4 4 5 6 5 5 5 4 7 6 4 4 5 7 4 6 7 3 5 5 5 5 6 3 4 5 2 5 4 4 6 6 4 4 3 4 5 5 5 3 5 4 5 6 4 5 4 2 5 5 6 5 4 5 5 4 7 7 3 5 5 4 5 4 6 5 5 5 6 5 5 6 3 3 5 1 1 7 6 4 4 4 6 5 5 5 4 5 5 4 4 4 5 4 5 4 5 5 4 7 6 5 3 3 5 4 5 3 3 3 5 4 4 2 5 4 3 6 3 6 3 5 6 4 6 5 6 5 6 4 4 3 6 5 5 3 3 4 4 2 6 5 4 3 5 6 ...
result:
wrong answer 1st numbers differ - expected: '4', found: '5'