QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#529135 | #8134. LCA Counting | Phang | WA | 2ms | 8856kb | C++14 | 985b | 2024-08-24 09:54:04 | 2024-08-24 09:54:04 |
Judging History
answer
#include<bits/stdc++.h>
#define rep1(i, a, b) for(int i = a; i <= b; ++i)
#define rep2(i, a, b) for(int i = a; i >= b; --i)
#define ll long long
#define ft first
#define sd second
#define pb push_back
#define pii pair <int, int>
#define ptc putchar
#define ld long double
#define debug puts("-------------------")
const int N = 2e5 + 10;
using namespace std;
int n, f[N], ans, cnt = 0;
vector <int> e[N];
void dfs(int u) {
int mx1 = 0, mx2 = 0;
for(auto v : e[u]) {
dfs(v);
if(f[v] >= f[mx1]) mx2 = mx1, mx1 = v;
else if(f[v] >= f[mx2]) mx2 = v;
} int x = e[u].size();
f[u] = max(f[u], f[mx1] + f[mx2] + (mx1 > 0 && mx2 > 0));
ans = max(ans, f[u]); if(u > 1 && !x) ++cnt;
}
int main() {
cin >> n;
rep1(i, 2, n) {
int x; cin >> x;
e[x].pb(i);
} dfs(1);
int nw = 1;
// cout << cnt << '\n';
rep1(i, 1, cnt) {
cout << nw << ' ';
++nw; if(ans) ++nw, --ans;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8364kb
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: 1ms
memory: 8856kb
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: 1ms
memory: 8436kb
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: 1ms
memory: 8332kb
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: 2ms
memory: 8420kb
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'