QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#761455 | #9751. 覆盖一棵树 | chensunyishuo | WA | 0ms | 9840kb | C++11 | 1.3kb | 2024-11-18 23:09:15 | 2024-11-18 23:09:16 |
Judging History
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'