QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#795195#5455. TreeScriptmoosyWA 13ms6020kbC++201.4kb2024-11-30 18:30:502024-11-30 18:30:51

Judging History

你现在查看的是最新测评结果

  • [2024-11-30 18:30:51]
  • 评测
  • 测评结果:WA
  • 用时:13ms
  • 内存:6020kb
  • [2024-11-30 18:30:50]
  • 提交

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 == 999) {
		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: 100
Accepted
time: 3ms
memory: 5632kb

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: 13ms
memory: 6020kb

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:

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 24 41 107...

result:

wrong answer 1st numbers differ - expected: '4', found: '0'