QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#795188 | #5455. TreeScript | moosy | WA | 2ms | 5572kb | C++20 | 1.4kb | 2024-11-30 18:29:22 | 2024-11-30 18:29:28 |
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, T;
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();
}
if(T == 1000) {
for(ll i = 1; i <= n; i ++ ) {
cout << p[i] << " \n"[i % 10 == 0];
}
}
return;
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);
T = 1;
cin >> T;
while(T -- ) {
solve();
}
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 5572kb
input:
2 3 0 1 2 7 0 1 2 2 1 4 1
output:
result:
wrong answer Answer contains longer sequence [length = 2], but output contains 0 elements