QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#685567#5455. TreeScriptDanRan02WA 0ms3616kbC++20746b2024-10-28 20:11:362024-10-28 20:11:36

Judging History

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

  • [2024-10-28 20:11:36]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3616kb
  • [2024-10-28 20:11:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int _;
	cin >> _;
	while (_ --) {
		int n;
		cin >> n;

		vector<int> p(n + 1);
		vector<vector<int>> adj(n + 1);
		for (int i = 1; i <= n; ++ i) {
			cin >> p[i];
			adj[p[i]].push_back(i);
		}

		vector<int> dp(n + 1, 0);
		auto dfs = [&](auto dfs, int u) -> void{
			vector<int> v;
			for (auto ne : adj[u]) {
				dfs(dfs, ne);
				v.push_back(ne);
			}

			sort(v.begin(), v.end(), greater());
			if (v.size() == 0) dp[u] = 1;
			else if (v.size() == 1) dp[u] = v[0];
			else {
				dp[u] = max(v[0], v[1] + 1);
			}
		};

		dfs(dfs, 1);

		cout << dp[1] << '\n';
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3616kb

input:

2
3
0 1 2
7
0 1 2 2 1 4 1

output:

2
7

result:

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