QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611329#5454. Subtree ActivationDimashCompile Error//C++231.4kb2024-10-04 20:26:432024-10-04 20:26:45

Judging History

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

  • [2024-10-04 20:26:45]
  • 评测
  • [2024-10-04 20:26:43]
  • 提交

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 << res - 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

answer.code: In function ‘void test()’:
answer.code:54:13: error: ‘res’ was not declared in this scope
   54 |     cout << res - max(dp[1][0], dp[1][1]) << '\n';
      |             ^~~