QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#506740 | #5455. TreeScript | HatodaAko# | WA | 19ms | 9068kb | C++17 | 900b | 2024-08-05 21:04:57 | 2024-08-05 21:04:57 |
Judging History
answer
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int a[200005];
vector<int> vtx[200005];
int dp[200005];
int f(int cur, int pre){
int &ret = dp[cur];
if(ret != -1) return ret;
multiset<int> st;
ret = 1;
for(auto nxt : vtx[cur]){
if(nxt == pre) continue;
st.insert(f(nxt, cur));
}
if(st.empty()){
return ret;
}
if(st.size() < 2){
return ret = *st.begin();
}
else{
auto it = --st.end();
it--;
return ret = 1+(*it);
}
}
void solve(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
if(i != 1){
vtx[i].push_back(a[i]);
vtx[a[i]].push_back(i);
}
}
for(int i=1; i<=n; i++){
dp[i] = -1;
}
f(1, -1);
printf("%d\n", dp[1]);
for(int i=1; i<=n; i++){
vtx[i].clear();
}
}
int main(){
int t = 1;
scanf("%d", &t);
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9068kb
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: 8856kb
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:
4 4 3 4 3 4 4 3 4 3 2 3 3 5 3 4 3 4 3 5 5 3 4 4 5 4 5 3 4 4 2 4 4 2 4 4 3 2 2 4 3 4 3 3 4 3 5 4 4 3 4 2 2 5 5 4 3 4 4 4 3 3 3 4 2 3 3 4 5 4 3 5 5 3 4 4 2 3 4 1 1 5 3 4 3 3 4 5 4 4 3 4 4 4 4 3 3 3 4 4 4 4 4 6 4 3 3 3 5 3 3 3 2 3 4 3 3 2 4 2 3 5 3 5 3 4 4 4 5 4 5 3 4 4 4 3 5 3 4 3 3 3 3 2 5 5 4 2 4 4 ...
result:
wrong answer 7th numbers differ - expected: '5', found: '4'