QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#761455#9751. 覆盖一棵树chensunyishuoWA 0ms9840kbC++111.3kb2024-11-18 23:09:152024-11-18 23:09:16

Judging History

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

  • [2024-11-18 23:09:16]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:9840kb
  • [2024-11-18 23:09:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
int dad[200001];
int deep[200001];
int vis[200001];
int bedad[200001];
vector<int> leaves;

bool cmp(int x1, int x2) {
	return deep[x1] < deep[x2];
}

int solve(int x) {
	if (deep[x] != 0) {
		return deep[x];
	}
	//decide deep
	if (x == 1) {
		deep[x] = 1;
		return 1;
	}
	deep[x] = solve(dad[x]) + 1;
	return deep[x];
}

signed main() {
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	int n;
	int ans = 0;
	int cnt = 0;
	while (T--) {
		cin >> n;
		leaves.clear();
		memset(bedad, 0, sizeof(bedad));
		memset(dad, 0, sizeof(dad));
		memset(vis, 0, sizeof(vis));
		for (int i = 2; i <= n; i++) {
			cin >> dad[i];
			bedad[dad[i]] = 1;
		}
		dad[1] = 1;
		for (int i = 1; i <= n; i++) {
			deep[i] = solve(i);
			//cout << i << ' ' << deep[i] << endl;
		}
		for (int i = 1; i <= n; i++) {
			if (!bedad[i]) {
				leaves.push_back(i);
				//cout << i << endl;
			}
		}
		sort(leaves.begin(), leaves.end(), cmp);
		for (size_t i = 0; i < leaves.size(); i++) {
			//cout << leaves[i] << endl;
			cnt = 0;
			int tmp = leaves[i];
			while (!vis[tmp]) {
				vis[tmp] = 1;
				cnt++;
				tmp = tmp[dad];
			}
			ans = max(ans, cnt);
		}
		cout << ans << endl;
	}
	return 0;
}

详细

Test #1:

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

input:

2
8
1 2 3 2 5 1 7
8
1 2 3 4 5 6 7

output:

3
8

result:

wrong answer 2nd lines differ - expected: '7', found: '8'