QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#583806 | #8134. LCA Counting | ucup-team1766 | WA | 0ms | 3768kb | C++20 | 1.2kb | 2024-09-22 22:36:58 | 2024-09-22 22:37:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// Returns maximum leaves in a subtree that can achieve the largest possible LCA set.
int dfs(vector<vector<int>> &tree, int cur) {
vector<int> max_leaves;
for (int child : tree[cur]) {
max_leaves.push_back(dfs(tree, child));
}
sort(max_leaves.begin(), max_leaves.end());
if (max_leaves.size() == 0) {
return 1;
} else if (max_leaves.size() == 1) {
return max_leaves[0];
} else {
int size = max_leaves.size();
return max_leaves[size - 1] + max_leaves[size - 2];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vector<int>> tree(n);
for (int i = 1; i < n; i++) {
int pi;
cin >> pi;
tree[pi - 1].push_back(i);
}
int leaf_cnt = 0;
for (int i = 1; i < n; i++) {
if (tree[i].size() == 0) {
leaf_cnt++;
}
}
int max_leaves = dfs(tree, 0);
int res = 1;
for (int i = 1; i <= leaf_cnt; i++) {
cout << res << " ";
if (i < max_leaves) {
res += 2;
} else {
res++;
}
}
cout << "\n";
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
input:
7 1 1 2 4 2 2
output:
1 3 5 6
result:
ok 4 number(s): "1 3 5 6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3768kb
input:
10 1 1 2 2 1 1 1 2 4
output:
1 3 5 6 7 8 9
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
10 1 2 2 4 5 6 1 2 4
output:
1 3 5 7 8
result:
ok 5 number(s): "1 3 5 7 8"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
10 1 2 3 3 3 5 6 4 9
output:
1 3 4
result:
ok 3 number(s): "1 3 4"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 3580kb
input:
1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 4 1 2 2 1 1 1 1 2 1 1 1 1 2 2 1 1 1 1 1 6 3 1 1 1 2 2 1 1 2 1 2 1 3 1 2 1 1 2 1 1 1 1 1 1 1 4 1 5 1 1 1 1 1 2 1 1 2 1 2 1 2 5 3 1 3 1 1 2 1 2 1 1 3 2 1 6 2 1 2 5 1 1 1 3 2 1 2 1 1 1 1 1...
output:
1 3 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 10...
result:
wrong answer 6th numbers differ - expected: '10', found: '9'