QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#795188#5455. TreeScriptmoosyWA 2ms5572kbC++201.4kb2024-11-30 18:29:222024-11-30 18:29:28

Judging History

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

  • [2024-11-30 18:29:28]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5572kb
  • [2024-11-30 18:29:22]
  • 提交

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