Submission #441193

# Submission time Handle Problem Language Result Execution time Memory
441193 2021-07-04T14:45:47 Z JovanB Election Campaign (JOI15_election_campaign) C++17
100 / 100
275 ms 28152 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

const int MAXN = 100000;
const int MXLOG = 16;

vector <int> graf[MAXN+5];
int dp[MAXN+5];
int par[MAXN+5][MXLOG+1];

int tjm;

int in[MAXN+5];
int out[MAXN+5];

int bit[2*MAXN+5];
int niz[2*MAXN+5];

void upd(int x, int val){
    while(x <= 2*MAXN){
        bit[x] += val;
        x += x & -x;
    }
}

int bitget(int x){
    int res = 0;
    while(x){
        res += bit[x];
        x -= x & -x;
    }
    return res;
}

int query(int l, int r){
    return bitget(r) - bitget(l-1);
}

void dfs_calc(int v, int p){
    in[v] = ++tjm;
    par[v][0] = p;
    for(int j=1; j<=MXLOG; j++) par[v][j] = par[par[v][j-1]][j-1];
    for(auto c : graf[v]) if(c != p) dfs_calc(c, v);
    out[v] = ++tjm;
}

bool is_parent(int a, int b){
    return (a == 0) || (in[a] <= in[b] && out[b] <= out[a]);
}

int lca(int a, int b){
    if(is_parent(a, b)) return a;
    for(int j=MXLOG; j>=0; j--) if(!is_parent(par[a][j], b)) a = par[a][j];
    return par[a][0];
}

struct Option{
    int a, b, c;
};

vector <Option> options[MAXN+5];

int prvidole(int a, int b){
    for(int j=MXLOG; j>=0; j--) if(!is_parent(par[b][j], a)) b = par[b][j];
    return b;
}

void dfs_solve(int v, int p){
    for(auto c : graf[v]){
        if(c != p){
            dfs_solve(c, v);
            dp[v] += dp[c];
        }
    }
    for(auto option : options[v]){
        int a = option.a;
        int b = option.b;
        int c = option.c;
        int val = c;
        if(is_parent(b, a)) swap(a, b);
        if(a == v){
            val += query(in[v], in[b]);
        }
        else{
            val += query(in[v], in[a]);
            int k = prvidole(v, b);
            val += query(in[k], in[b]);
        }
        dp[v] = max(dp[v], val);
    }
    if(v != 1){
        upd(in[p], dp[v]);
        upd(in[v], -dp[v]);
        upd(out[v], dp[v]);
        upd(out[p], -dp[v]);
    }
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n;
    cin >> n;
    for(int i=1; i<n; i++){
        int a, b;
        cin >> a >> b;
        graf[a].push_back(b);
        graf[b].push_back(a);
    }
    dfs_calc(1, 0);
    int m;
    cin >> m;
    while(m--){
        int a, b, c;
        cin >> a >> b >> c;
        options[lca(a, b)].push_back({a, b, c});
    }
    dfs_solve(1, 0);
    cout << dp[1] << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 100 ms 16940 KB Output is correct
6 Correct 60 ms 26092 KB Output is correct
7 Correct 115 ms 22796 KB Output is correct
8 Correct 78 ms 16840 KB Output is correct
9 Correct 99 ms 21020 KB Output is correct
10 Correct 80 ms 16892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 128 ms 28140 KB Output is correct
5 Correct 127 ms 28140 KB Output is correct
6 Correct 122 ms 28100 KB Output is correct
7 Correct 130 ms 28060 KB Output is correct
8 Correct 143 ms 28108 KB Output is correct
9 Correct 123 ms 28100 KB Output is correct
10 Correct 142 ms 28020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 128 ms 28140 KB Output is correct
5 Correct 127 ms 28140 KB Output is correct
6 Correct 122 ms 28100 KB Output is correct
7 Correct 130 ms 28060 KB Output is correct
8 Correct 143 ms 28108 KB Output is correct
9 Correct 123 ms 28100 KB Output is correct
10 Correct 142 ms 28020 KB Output is correct
11 Correct 15 ms 5708 KB Output is correct
12 Correct 129 ms 28052 KB Output is correct
13 Correct 128 ms 28136 KB Output is correct
14 Correct 112 ms 28116 KB Output is correct
15 Correct 142 ms 28040 KB Output is correct
16 Correct 117 ms 28080 KB Output is correct
17 Correct 126 ms 28120 KB Output is correct
18 Correct 133 ms 28040 KB Output is correct
19 Correct 115 ms 28152 KB Output is correct
20 Correct 134 ms 28100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 18300 KB Output is correct
2 Correct 115 ms 28068 KB Output is correct
3 Correct 239 ms 24428 KB Output is correct
4 Correct 138 ms 18424 KB Output is correct
5 Correct 196 ms 23752 KB Output is correct
6 Correct 134 ms 18360 KB Output is correct
7 Correct 231 ms 23684 KB Output is correct
8 Correct 200 ms 18500 KB Output is correct
9 Correct 119 ms 28148 KB Output is correct
10 Correct 220 ms 22344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 100 ms 16940 KB Output is correct
6 Correct 60 ms 26092 KB Output is correct
7 Correct 115 ms 22796 KB Output is correct
8 Correct 78 ms 16840 KB Output is correct
9 Correct 99 ms 21020 KB Output is correct
10 Correct 80 ms 16892 KB Output is correct
11 Correct 4 ms 5196 KB Output is correct
12 Correct 4 ms 5196 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 4 ms 5196 KB Output is correct
15 Correct 4 ms 5196 KB Output is correct
16 Correct 4 ms 5196 KB Output is correct
17 Correct 4 ms 5196 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 100 ms 16940 KB Output is correct
6 Correct 60 ms 26092 KB Output is correct
7 Correct 115 ms 22796 KB Output is correct
8 Correct 78 ms 16840 KB Output is correct
9 Correct 99 ms 21020 KB Output is correct
10 Correct 80 ms 16892 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 128 ms 28140 KB Output is correct
15 Correct 127 ms 28140 KB Output is correct
16 Correct 122 ms 28100 KB Output is correct
17 Correct 130 ms 28060 KB Output is correct
18 Correct 143 ms 28108 KB Output is correct
19 Correct 123 ms 28100 KB Output is correct
20 Correct 142 ms 28020 KB Output is correct
21 Correct 15 ms 5708 KB Output is correct
22 Correct 129 ms 28052 KB Output is correct
23 Correct 128 ms 28136 KB Output is correct
24 Correct 112 ms 28116 KB Output is correct
25 Correct 142 ms 28040 KB Output is correct
26 Correct 117 ms 28080 KB Output is correct
27 Correct 126 ms 28120 KB Output is correct
28 Correct 133 ms 28040 KB Output is correct
29 Correct 115 ms 28152 KB Output is correct
30 Correct 134 ms 28100 KB Output is correct
31 Correct 176 ms 18300 KB Output is correct
32 Correct 115 ms 28068 KB Output is correct
33 Correct 239 ms 24428 KB Output is correct
34 Correct 138 ms 18424 KB Output is correct
35 Correct 196 ms 23752 KB Output is correct
36 Correct 134 ms 18360 KB Output is correct
37 Correct 231 ms 23684 KB Output is correct
38 Correct 200 ms 18500 KB Output is correct
39 Correct 119 ms 28148 KB Output is correct
40 Correct 220 ms 22344 KB Output is correct
41 Correct 4 ms 5196 KB Output is correct
42 Correct 4 ms 5196 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 4 ms 5196 KB Output is correct
45 Correct 4 ms 5196 KB Output is correct
46 Correct 4 ms 5196 KB Output is correct
47 Correct 4 ms 5196 KB Output is correct
48 Correct 4 ms 5196 KB Output is correct
49 Correct 5 ms 5196 KB Output is correct
50 Correct 4 ms 5196 KB Output is correct
51 Correct 181 ms 18628 KB Output is correct
52 Correct 142 ms 28112 KB Output is correct
53 Correct 275 ms 22440 KB Output is correct
54 Correct 132 ms 18432 KB Output is correct
55 Correct 200 ms 18420 KB Output is correct
56 Correct 130 ms 28100 KB Output is correct
57 Correct 221 ms 23216 KB Output is correct
58 Correct 148 ms 18300 KB Output is correct
59 Correct 230 ms 18572 KB Output is correct
60 Correct 130 ms 28100 KB Output is correct
61 Correct 215 ms 23372 KB Output is correct
62 Correct 153 ms 18208 KB Output is correct
63 Correct 166 ms 18184 KB Output is correct
64 Correct 141 ms 28100 KB Output is correct
65 Correct 226 ms 23232 KB Output is correct
66 Correct 135 ms 18432 KB Output is correct
67 Correct 165 ms 18336 KB Output is correct
68 Correct 127 ms 28096 KB Output is correct
69 Correct 221 ms 21828 KB Output is correct
70 Correct 157 ms 18508 KB Output is correct