QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761465#9751. 覆盖一棵树chensunyishuoWA 24ms5724kbC++111.3kb2024-11-18 23:20:452024-11-18 23:20:47

Judging History

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

  • [2024-11-18 23:20:47]
  • 评测
  • 测评结果:WA
  • 用时:24ms
  • 内存:5724kb
  • [2024-11-18 23:20:45]
  • 提交

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();
		for (int i = 1; i <= n; i++) {
			bedad[i] = 0;
			dad[i] = 0;
			vis[i] = 0;
		}
		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 (true) {
				if (!vis[tmp] && tmp != 1) {
					vis[tmp] = 1;
					cnt++;
					tmp = tmp[dad];
				} else {
					cnt++;
					break;
				}
			}
			ans = max(ans, cnt - 1);
		}
		cout << ans << endl;
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5724kb

input:

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

output:

3
7

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 24ms
memory: 5716kb

input:

33428
10
1 2 3 3 4 6 7 7 9
10
1 2 3 4 5 6 7 8 8
8
1 2 3 4 5 6 7
8
1 2 3 4 4 6 7
4
1 2 3
3
1 2
3
1 1
9
1 2 3 4 5 6 7 8
2
1
3
1 2
10
1 2 3 4 5 6 7 8 9
3
1 2
2
1
10
1 2 3 4 5 6 7 8 9
2
1
5
1 2 2 4
8
1 2 3 4 5 6 7
5
1 2 3 3
2
1
5
1 2 3 4
3
1 2
9
1 2 3 4 5 6 6 8
9
1 2 3 4 5 6 7 8
9
1 2 3 4 5 5 7 8
8
1 2 ...

output:

4
8
8
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
9
...

result:

wrong answer 3rd lines differ - expected: '7', found: '8'