QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611330#5454. Subtree ActivationDimash33.333333 46ms29840kbC++231.4kb2024-10-04 20:27:122024-10-04 20:27:13

Judging History

你现在查看的是最新测评结果

  • [2024-10-04 20:27:13]
  • 评测
  • 测评结果:33.333333
  • 用时:46ms
  • 内存:29840kb
  • [2024-10-04 20:27:12]
  • 提交

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];
        assert(dp[to][0] - dp[to][1] + s[to] * 2 >= sec[to] * 2);
        d.push_back({dp[to][0] - dp[to][1] + s[to] * 2, to});
        d.push_back({sec[to] * 2, to});
        m+=2;
    }
    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;
    }
}
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: 2ms
memory: 7696kb

input:

3
1 1

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 4.7619
Accepted
time: 2ms
memory: 7624kb

input:

8
1 2 1 1 5 3 4

output:

20

result:

ok 1 number(s): "20"

Test #3:

score: 4.7619
Accepted
time: 2ms
memory: 7724kb

input:

8
1 2 1 4 5 4 4

output:

20

result:

ok 1 number(s): "20"

Test #4:

score: 4.7619
Accepted
time: 2ms
memory: 7624kb

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: 0ms
memory: 7580kb

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: 2ms
memory: 7648kb

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: 2ms
memory: 7852kb

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: 2ms
memory: 7652kb

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: 2ms
memory: 7716kb

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: 8200kb

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:

18818

result:

wrong answer 1st numbers differ - expected: '18824', found: '18818'

Test #11:

score: 0
Wrong Answer
time: 2ms
memory: 8184kb

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:

18906

result:

wrong answer 1st numbers differ - expected: '18914', found: '18906'

Test #12:

score: 0
Wrong Answer
time: 3ms
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:

18844

result:

wrong answer 1st numbers differ - expected: '18854', found: '18844'

Test #13:

score: 0
Wrong Answer
time: 3ms
memory: 8116kb

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:

18888

result:

wrong answer 1st numbers differ - expected: '18894', found: '18888'

Test #14:

score: 0
Wrong Answer
time: 3ms
memory: 8332kb

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:

18944

result:

wrong answer 1st numbers differ - expected: '18956', found: '18944'

Test #15:

score: 0
Wrong Answer
time: 3ms
memory: 8208kb

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:

18878

result:

wrong answer 1st numbers differ - expected: '18882', found: '18878'

Test #16:

score: 0
Wrong Answer
time: 34ms
memory: 29052kb

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:

756384

result:

wrong answer 1st numbers differ - expected: '756550', found: '756384'

Test #17:

score: 0
Wrong Answer
time: 26ms
memory: 29044kb

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:

755498

result:

wrong answer 1st numbers differ - expected: '755654', found: '755498'

Test #18:

score: 0
Wrong Answer
time: 46ms
memory: 29840kb

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:

755710

result:

wrong answer 1st numbers differ - expected: '755830', found: '755710'

Test #19:

score: 0
Wrong Answer
time: 28ms
memory: 27984kb

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:

756658

result:

wrong answer 1st numbers differ - expected: '756822', found: '756658'

Test #20:

score: 0
Wrong Answer
time: 41ms
memory: 28748kb

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:

756088

result:

wrong answer 1st numbers differ - expected: '756212', found: '756088'

Test #21:

score: 0
Wrong Answer
time: 44ms
memory: 28848kb

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:

755770

result:

wrong answer 1st numbers differ - expected: '755930', found: '755770'