Submission #1040266

# Submission time Handle Problem Language Result Execution time Memory
1040266 2024-07-31T21:24:05 Z shezitt Election Campaign (JOI15_election_campaign) C++14
100 / 100
148 ms 57004 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define int ll
#define fore(a, b, c) for(int a=b; a<c; ++a)
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define ii pair<int,int>
#define dbg(x) cerr << #x << ": " << x << endl
#define raya cerr << "=============" << endl

const int N = 2e5+5;
const int LG = 20;

struct fenwick {
    int n;
    vi t;
    fenwick(int n) : n(n) {
        t.assign(n+5, 0);
    }
    void add(int i, int x){
        for(; i<=n; i+=i&-i){
            t[i] += x;
        }
    }
    int query(int i){
        int r = 0;
        for(; i>0; i-=i&-i){
            r += t[i];
        }
        return r;
    }
} bit(N-5);

vi adj[N];
vector<array<int,3>> ways[N];
int dp[N][2];
int pa[N][LG];
int dep[N];
int timer;
int entro[N], salgo[N];

int n, m;

void dfs(int at, int ante=0){
    entro[at] = ++timer;
    pa[at][0] = ante;
    for(int u : adj[at]){
        if(u == ante) continue;
        dep[u] = dep[at] + 1;
        dfs(u, at);
    }
    salgo[at] = timer;
}

int jump(int a, int k){
    fore(i, 0, LG){
        if((k >> i) & 1){
            a = pa[a][i];
        }
    }
    return a;
}

int lca(int a, int b){
    if(dep[a] > dep[b]) swap(a, b);
    b = jump(b, dep[b] - dep[a]);
    if(a == b) return a;
    for(int i=LG-1; i>=0; --i){
        if(pa[a][i] != pa[b][i]){
            a = pa[a][i];
            b = pa[b][i];
        }
    }
    return pa[a][0];
}

int f(int at, int tomo){
    int &res = dp[at][tomo];
    if(res > -1) return res;
    res = 0;

    if(tomo == 0){
        int cur = 0;
        for(int u : adj[at]){
            if(u == pa[at][0]) continue;
            cur += max(f(u, 0), f(u, 1));
        }
        res = cur;
    }

    if(tomo == 1){
        int sintomar = f(at, 0);
        int mx = sintomar;
        for(auto [a, b, c] : ways[at]){
            int tmp = bit.query(entro[a]) + bit.query(entro[b]) + c;
            mx = max(mx, sintomar + tmp);
        }
        res = mx;  // tomando
        bit.add(entro[at], sintomar - res);
        bit.add(salgo[at]+1, res - sintomar);
    }

    return res;

}

signed main(){
    memset(dp, -1, sizeof dp);
    cin >> n;
    fore(i, 1, n){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }    

    dfs(1);
    for(int i=1; i<LG; ++i){
        for(int j=1; j<=n; ++j){
            pa[j][i] = pa[pa[j][i-1]][i-1];
        }
    }

    cin >> m;
    fore(i, 0, m){
        int a, b, c;
        cin >> a >> b >> c;
        ways[lca(a, b)].pb({a, b, c});
    }

    int ans = max(f(1, 0), f(1, 1));
    
    cout << ans << endl;

}

Compilation message

election_campaign.cpp: In function 'll f(ll, ll)':
election_campaign.cpp:102:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  102 |         for(auto [a, b, c] : ways[at]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 3 ms 19548 KB Output is correct
3 Correct 3 ms 19556 KB Output is correct
4 Correct 3 ms 19548 KB Output is correct
5 Correct 71 ms 38568 KB Output is correct
6 Correct 57 ms 53216 KB Output is correct
7 Correct 68 ms 48208 KB Output is correct
8 Correct 56 ms 38484 KB Output is correct
9 Correct 72 ms 45136 KB Output is correct
10 Correct 54 ms 38648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19548 KB Output is correct
2 Correct 3 ms 19548 KB Output is correct
3 Correct 3 ms 19804 KB Output is correct
4 Correct 114 ms 56828 KB Output is correct
5 Correct 118 ms 56908 KB Output is correct
6 Correct 113 ms 56916 KB Output is correct
7 Correct 129 ms 56808 KB Output is correct
8 Correct 121 ms 56936 KB Output is correct
9 Correct 108 ms 56916 KB Output is correct
10 Correct 117 ms 56772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 19548 KB Output is correct
2 Correct 3 ms 19548 KB Output is correct
3 Correct 3 ms 19804 KB Output is correct
4 Correct 114 ms 56828 KB Output is correct
5 Correct 118 ms 56908 KB Output is correct
6 Correct 113 ms 56916 KB Output is correct
7 Correct 129 ms 56808 KB Output is correct
8 Correct 121 ms 56936 KB Output is correct
9 Correct 108 ms 56916 KB Output is correct
10 Correct 117 ms 56772 KB Output is correct
11 Correct 13 ms 20620 KB Output is correct
12 Correct 121 ms 56920 KB Output is correct
13 Correct 119 ms 56916 KB Output is correct
14 Correct 114 ms 56916 KB Output is correct
15 Correct 118 ms 56788 KB Output is correct
16 Correct 111 ms 56912 KB Output is correct
17 Correct 120 ms 56912 KB Output is correct
18 Correct 125 ms 56916 KB Output is correct
19 Correct 110 ms 56788 KB Output is correct
20 Correct 122 ms 56952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 41480 KB Output is correct
2 Correct 107 ms 56912 KB Output is correct
3 Correct 140 ms 51260 KB Output is correct
4 Correct 106 ms 41732 KB Output is correct
5 Correct 126 ms 49888 KB Output is correct
6 Correct 106 ms 41924 KB Output is correct
7 Correct 140 ms 49568 KB Output is correct
8 Correct 114 ms 41552 KB Output is correct
9 Correct 106 ms 56916 KB Output is correct
10 Correct 140 ms 47824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 3 ms 19548 KB Output is correct
3 Correct 3 ms 19556 KB Output is correct
4 Correct 3 ms 19548 KB Output is correct
5 Correct 71 ms 38568 KB Output is correct
6 Correct 57 ms 53216 KB Output is correct
7 Correct 68 ms 48208 KB Output is correct
8 Correct 56 ms 38484 KB Output is correct
9 Correct 72 ms 45136 KB Output is correct
10 Correct 54 ms 38648 KB Output is correct
11 Correct 4 ms 19548 KB Output is correct
12 Correct 4 ms 19804 KB Output is correct
13 Correct 3 ms 19548 KB Output is correct
14 Correct 3 ms 19548 KB Output is correct
15 Correct 4 ms 19548 KB Output is correct
16 Correct 4 ms 19548 KB Output is correct
17 Correct 3 ms 19548 KB Output is correct
18 Correct 4 ms 19548 KB Output is correct
19 Correct 3 ms 19548 KB Output is correct
20 Correct 4 ms 19804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19548 KB Output is correct
2 Correct 3 ms 19548 KB Output is correct
3 Correct 3 ms 19556 KB Output is correct
4 Correct 3 ms 19548 KB Output is correct
5 Correct 71 ms 38568 KB Output is correct
6 Correct 57 ms 53216 KB Output is correct
7 Correct 68 ms 48208 KB Output is correct
8 Correct 56 ms 38484 KB Output is correct
9 Correct 72 ms 45136 KB Output is correct
10 Correct 54 ms 38648 KB Output is correct
11 Correct 3 ms 19548 KB Output is correct
12 Correct 3 ms 19548 KB Output is correct
13 Correct 3 ms 19804 KB Output is correct
14 Correct 114 ms 56828 KB Output is correct
15 Correct 118 ms 56908 KB Output is correct
16 Correct 113 ms 56916 KB Output is correct
17 Correct 129 ms 56808 KB Output is correct
18 Correct 121 ms 56936 KB Output is correct
19 Correct 108 ms 56916 KB Output is correct
20 Correct 117 ms 56772 KB Output is correct
21 Correct 13 ms 20620 KB Output is correct
22 Correct 121 ms 56920 KB Output is correct
23 Correct 119 ms 56916 KB Output is correct
24 Correct 114 ms 56916 KB Output is correct
25 Correct 118 ms 56788 KB Output is correct
26 Correct 111 ms 56912 KB Output is correct
27 Correct 120 ms 56912 KB Output is correct
28 Correct 125 ms 56916 KB Output is correct
29 Correct 110 ms 56788 KB Output is correct
30 Correct 122 ms 56952 KB Output is correct
31 Correct 128 ms 41480 KB Output is correct
32 Correct 107 ms 56912 KB Output is correct
33 Correct 140 ms 51260 KB Output is correct
34 Correct 106 ms 41732 KB Output is correct
35 Correct 126 ms 49888 KB Output is correct
36 Correct 106 ms 41924 KB Output is correct
37 Correct 140 ms 49568 KB Output is correct
38 Correct 114 ms 41552 KB Output is correct
39 Correct 106 ms 56916 KB Output is correct
40 Correct 140 ms 47824 KB Output is correct
41 Correct 4 ms 19548 KB Output is correct
42 Correct 4 ms 19804 KB Output is correct
43 Correct 3 ms 19548 KB Output is correct
44 Correct 3 ms 19548 KB Output is correct
45 Correct 4 ms 19548 KB Output is correct
46 Correct 4 ms 19548 KB Output is correct
47 Correct 3 ms 19548 KB Output is correct
48 Correct 4 ms 19548 KB Output is correct
49 Correct 3 ms 19548 KB Output is correct
50 Correct 4 ms 19804 KB Output is correct
51 Correct 124 ms 41360 KB Output is correct
52 Correct 122 ms 56912 KB Output is correct
53 Correct 144 ms 47876 KB Output is correct
54 Correct 120 ms 41540 KB Output is correct
55 Correct 123 ms 41372 KB Output is correct
56 Correct 118 ms 56864 KB Output is correct
57 Correct 133 ms 49040 KB Output is correct
58 Correct 111 ms 41628 KB Output is correct
59 Correct 124 ms 41416 KB Output is correct
60 Correct 119 ms 57004 KB Output is correct
61 Correct 134 ms 49340 KB Output is correct
62 Correct 110 ms 41772 KB Output is correct
63 Correct 131 ms 41448 KB Output is correct
64 Correct 121 ms 56852 KB Output is correct
65 Correct 148 ms 49136 KB Output is correct
66 Correct 122 ms 41344 KB Output is correct
67 Correct 136 ms 42024 KB Output is correct
68 Correct 141 ms 56912 KB Output is correct
69 Correct 130 ms 46732 KB Output is correct
70 Correct 112 ms 41852 KB Output is correct