QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611307 | #5454. Subtree Activation | Dimash | 33.333333 | 62ms | 29456kb | C++23 | 1.4kb | 2024-10-04 20:17:36 | 2024-10-04 20:17:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 12, MOD = (int)1e9 + 7;
vector<int> g[N];
int s[N], n, sec[N];
ll ans = 0, dp[N][2];
void calc(int v) {
s[v] = 1;
ll sum = 0;
vector<pair<ll, ll>> d;
int m = 0;
for(int to:g[v]) {
calc(to);
s[v] += s[to];
sum += dp[to][1];
d.push_back({dp[to][0] - dp[to][1] + s[to] * 2, to});
d.push_back({sec[to] * 2, to});
m++;
}
sort(d.rbegin(), d.rend());
dp[v][0] = dp[v][1] = sum;
ans += s[v] * 2;
if(s[v] == 1) {
return;
}
for(int to:g[v]) {
if(to != d[0].second) {
sec[v] = max(sec[v], s[to]);
} else {
sec[v] = max(sec[v], sec[to]);
}
}
if(m >= 1) {
dp[v][0] += d[0].first;
dp[v][1] += d[0].first;
}
if(m >= 2) {
dp[v][1] += d[1].first;
}
// cout << v << ' ' << dp[v][0] << ' ' << dp[v][1] << ' ' << sec[v] << '\n';
}
void test() {
cin >> n;
for(int i = 2; i <= n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
calc(1);
cout << ans - max(dp[1][0], dp[1][1]) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 4.7619
Accepted
time: 3ms
memory: 7632kb
input:
3 1 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 4.7619
Accepted
time: 3ms
memory: 7720kb
input:
8 1 2 1 1 5 3 4
output:
20
result:
ok 1 number(s): "20"
Test #3:
score: 4.7619
Accepted
time: 3ms
memory: 7912kb
input:
8 1 2 1 4 5 4 4
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 4.7619
Accepted
time: 0ms
memory: 7628kb
input:
40 1 1 1 1 1 1 1 1 1 7 11 10 12 13 15 16 16 15 18 20 21 21 21 21 21 21 21 21 21 6 18 28 12 34 22 6 15 23 32
output:
130
result:
ok 1 number(s): "130"
Test #5:
score: 4.7619
Accepted
time: 3ms
memory: 7852kb
input:
40 1 1 1 1 1 1 1 1 1 10 9 12 8 14 15 16 17 12 17 19 21 21 21 21 21 21 21 21 21 23 26 15 9 6 27 32 5 11 8
output:
132
result:
ok 1 number(s): "132"
Test #6:
score: 4.7619
Accepted
time: 3ms
memory: 7692kb
input:
40 1 1 1 1 1 1 1 1 1 6 11 12 13 8 15 15 16 18 17 18 21 21 21 21 21 21 21 21 21 23 13 17 8 20 6 24 27 38 36
output:
132
result:
ok 1 number(s): "132"
Test #7:
score: 4.7619
Accepted
time: 3ms
memory: 7720kb
input:
40 1 1 1 1 1 1 1 1 1 10 11 7 13 14 15 16 17 18 18 18 21 21 21 21 21 21 21 21 21 25 11 13 13 3 24 11 25 35 28
output:
132
result:
ok 1 number(s): "132"
Test #8:
score: 0
Wrong Answer
time: 3ms
memory: 7704kb
input:
40 1 1 1 1 1 1 1 1 1 10 11 12 11 14 14 14 17 16 16 20 21 21 21 21 21 21 21 21 21 17 22 25 14 25 29 23 1 17 14
output:
136
result:
wrong answer 1st numbers differ - expected: '138', found: '136'
Test #9:
score: 0
Wrong Answer
time: 0ms
memory: 7632kb
input:
40 1 1 1 1 1 1 1 1 1 7 11 12 12 13 14 13 16 18 19 20 21 21 21 21 21 21 21 21 21 17 8 17 33 27 3 34 20 4 10
output:
124
result:
wrong answer 1st numbers differ - expected: '126', found: '124'
Test #10:
score: 0
Wrong Answer
time: 0ms
memory: 8196kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
18870
result:
wrong answer 1st numbers differ - expected: '18824', found: '18870'
Test #11:
score: 0
Wrong Answer
time: 0ms
memory: 8080kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
18988
result:
wrong answer 1st numbers differ - expected: '18914', found: '18988'
Test #12:
score: 0
Wrong Answer
time: 4ms
memory: 10232kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
18916
result:
wrong answer 1st numbers differ - expected: '18854', found: '18916'
Test #13:
score: 0
Wrong Answer
time: 0ms
memory: 8124kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
18956
result:
wrong answer 1st numbers differ - expected: '18894', found: '18956'
Test #14:
score: 0
Wrong Answer
time: 4ms
memory: 8376kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
19020
result:
wrong answer 1st numbers differ - expected: '18956', found: '19020'
Test #15:
score: 0
Wrong Answer
time: 4ms
memory: 8416kb
input:
5000 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 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 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 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 1 1 1 1 1 1 1 1...
output:
18966
result:
wrong answer 1st numbers differ - expected: '18882', found: '18966'
Test #16:
score: 0
Wrong Answer
time: 48ms
memory: 28780kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
759252
result:
wrong answer 1st numbers differ - expected: '756550', found: '759252'
Test #17:
score: 0
Wrong Answer
time: 53ms
memory: 29456kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
758412
result:
wrong answer 1st numbers differ - expected: '755654', found: '758412'
Test #18:
score: 0
Wrong Answer
time: 54ms
memory: 28700kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
758434
result:
wrong answer 1st numbers differ - expected: '755830', found: '758434'
Test #19:
score: 0
Wrong Answer
time: 54ms
memory: 28860kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
759422
result:
wrong answer 1st numbers differ - expected: '756822', found: '759422'
Test #20:
score: 0
Wrong Answer
time: 62ms
memory: 29192kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
758948
result:
wrong answer 1st numbers differ - expected: '756212', found: '758948'
Test #21:
score: 0
Wrong Answer
time: 45ms
memory: 28676kb
input:
200000 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 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 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 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 1 1 1 1 1 1 1...
output:
758698
result:
wrong answer 1st numbers differ - expected: '755930', found: '758698'