QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#673596 | #8134. LCA Counting | CruiseY_ | WA | 2ms | 10524kb | C++14 | 1.5kb | 2024-10-25 01:12:34 | 2024-10-25 01:12:35 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define db double
#define fi first
#define se second
#define pb push_back
using namespace std;
ll read() {
ll x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
return x * f;
}
const int mod = 1e9 + 7;
#define mid ((l + r) >> 1)
ll ksm(ll x, ll y, ll p) {
ll base = 1;
for(; y; y >>= 1) {
if(y & 1) base = base * x % p;
x = x * x % p;
}
return base;
}
ll ans[200010];
ll n, f[200010], m, sum, tip;
vector<int> mp[200010];
priority_queue<ll> q;
void dfs(int u) {
ll mx1 = 0, mx2 = 0;
if(!mp[u].size()) return m++, void();
for(int v : mp[u]) {
dfs(v);
if(f[v] > mx1) q.push(mx2), mx2 = mx1, mx1 = f[v];
else if(f[v] > mx2) q.push(mx2), mx2 = f[v];
else q.push(f[v]);
}
f[u] = mx1 + mx2 + (mp[u].size() > 1);
}
int main() {
n = read(); tip = 1;
for(int i = 2; i <= n; i++) mp[read()].pb(i);
dfs(1); q.push(f[1]);
while(!q.empty()) {
ll x = q.top(); q.pop(); if(!x) break;
ans[tip] = ans[tip - 1] + 1;
for(int i = 1; i <= x; i++)
tip++, ans[tip] = ans[tip - 1] + 2;
}
for(int i = 1; i <= m; i++) {
if(!ans[i]) ans[i] = ans[i - 1] + 1;
printf("%lld ", ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10524kb
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: 10272kb
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: 10128kb
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: 10364kb
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: 9680kb
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 6 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 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 104...
result:
wrong answer 4th numbers differ - expected: '7', found: '6'