QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#795170 | #5455. TreeScript | moosy | WA | 12ms | 5716kb | C++20 | 1.2kb | 2024-11-30 18:16:18 | 2024-11-30 18:16:18 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
using ll = long long;
using PII = pair<int,int>;
constexpr ll INF = 2E18 + 10;
constexpr int N = 2E6 + 10;
int p[N],ans;
vector <int>g[N];
int dfs(int u,int fa)
{
ll res = 0;
if(g[u].size() >= 3) res = 1;
vector<ll> a;
for(auto v : g[u]) {
if(fa == v) continue;
a.push_back(dfs(v, u));
}
sort(a.begin(), a.end());
if(a.size() >= 2) {
// cout << u << " " << a[a.size() - 2] << "\n";
ans = max(ans, a[a.size() - 2] + 1);
}
if(a.size() != 0) {
// cout << u << " res = " << res << ", a.back() = " << a.back() << "\n";
return res + a.back();
}
else {
// cout << u << " res = " << res << "\n";
return res;
}
}
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>p[i];
g[i].clear();
}
for(int i=2;i<=n;i++){
g[i].push_back(p[i]);
g[p[i]].push_back(i);
}
ans=0;
dfs(1, 0);
cout<<ans+1<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T=1;
cin>>T;
while(T--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5624kb
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: 12ms
memory: 5716kb
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:
7 9 3 8 4 5 8 8 5 6 7 8 10 8 5 5 8 10 5 9 12 4 6 9 8 6 11 4 6 9 2 8 7 4 7 7 5 5 3 5 7 6 5 3 4 6 8 8 7 6 6 2 6 6 10 6 5 9 6 7 9 8 3 7 6 6 6 6 10 6 6 8 8 6 6 9 2 4 7 1 1 8 9 5 4 4 8 10 6 7 7 9 5 6 5 5 6 5 7 6 10 8 5 10 9 7 3 4 7 4 7 5 4 3 8 7 3 2 5 6 3 8 4 10 3 6 7 5 9 5 9 6 7 4 7 4 9 5 6 4 3 4 5 2 10...
result:
wrong answer 1st numbers differ - expected: '4', found: '7'