Submission #879007

# Submission time Handle Problem Language Result Execution time Memory
879007 2023-11-26T04:52:35 Z hafo Election Campaign (JOI15_election_campaign) C++14
100 / 100
113 ms 28756 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 17;
const int maxn = 1e5 + 7;
const ll oo = 1e9 + 69;

int n, m, u, v, c, dp[maxn], sum[maxn], st[maxn], en[maxn], timer = 0;
vector<int> g[maxn], ev[maxn];
struct query {
    int u, v, c;
} que[maxn];

struct LCA {
    int p[LOG][maxn], dep[maxn];

    void dfs(int u, int par) {
        p[0][u] = par;
        for(auto v:g[u]) {
            if(v == par) continue;
            dep[v] = dep[u] + 1;
            dfs(v, u);
        }
    }

    int get_lca(int u, int v) {
        if(dep[u] < dep[v]) swap(u, v);
        int k = dep[u] - dep[v];
        for(int i = 0; i < LOG; i++)
            if((k >> i) & 1) u = p[i][u];
        if(u == v) return u;
        for(int i = LOG - 1; i >= 0; i--)
        if(p[i][u] != p[i][v]) {
            u = p[i][u];
            v = p[i][v];
        }
        return p[0][u];
    }

    int jump(int u, int k) {
        for(int i = 0; i < LOG; i++) 
            if((k >> i) & 1) u = p[i][u];
        return u;
    }

    void init() {
        dep[1] = 0;
        dfs(1, 0);
        for(int i = 1; i < LOG; i++) {
            for(int u = 1; u <= n; u++) p[i][u] = p[i - 1][p[i - 1][u]];
        }
    }

} lca;

void dfs_st(int u, int par) {
    st[u] = ++timer;
    for(auto v:g[u]) {
        if(v == par) continue;
        dfs_st(v, u);
    }
    en[u] = timer;
} 

struct BIT {
    int bit[maxn];

    void update(int x, int val) {
        for(; x <= n; x += x&-x) bit[x] += val;
    }
    
    int get(int x) {
        int ans = 0;
        for(; x; x -= x&-x) ans += bit[x];
        return ans;
    }

} bit;

int trace(int v, int u) {
    int res = 0;
    while(v != u) {
        res += dp[v] - sum[v];
        v = lca.p[0][v];
    }
    return res;
}

void dfs(int u, int par) {
    for(auto v:g[u]) {
        if(v == par) continue;
        dfs(v, u);
        sum[u] += dp[v];
    }

    maxi(dp[u], sum[u]);
    for(auto i:ev[u]) {
        int tot = bit.get(st[que[i].u]) + bit.get(st[que[i].v]);
        // if(que[i].u != u) tot += trace(que[i].u, u);
        // if(que[i].v != u) tot += trace(que[i].v, u);
        maxi(dp[u], sum[u] - tot + que[i].c);
    }
    bit.update(st[u], dp[u] - sum[u]);
    bit.update(en[u] + 1, -dp[u] + sum[u]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n;
    for(int i = 1; i < n; i++) {
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }

    lca.init();
    dfs_st(1, 0);

    cin>>m;
    for(int i = 1; i <= m; i++) {
        cin>>que[i].u>>que[i].v>>que[i].c;
        ev[lca.get_lca(que[i].u, que[i].v)].pb(i);
    }
    dfs(1, 0);
    cout<<dp[1];
    return 0;
} 
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14680 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 38 ms 18976 KB Output is correct
6 Correct 32 ms 26736 KB Output is correct
7 Correct 40 ms 24164 KB Output is correct
8 Correct 29 ms 19288 KB Output is correct
9 Correct 39 ms 22612 KB Output is correct
10 Correct 31 ms 19204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 69 ms 28552 KB Output is correct
5 Correct 74 ms 28600 KB Output is correct
6 Correct 60 ms 28584 KB Output is correct
7 Correct 70 ms 28596 KB Output is correct
8 Correct 68 ms 28464 KB Output is correct
9 Correct 65 ms 28640 KB Output is correct
10 Correct 68 ms 28536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 2 ms 14684 KB Output is correct
3 Correct 3 ms 14940 KB Output is correct
4 Correct 69 ms 28552 KB Output is correct
5 Correct 74 ms 28600 KB Output is correct
6 Correct 60 ms 28584 KB Output is correct
7 Correct 70 ms 28596 KB Output is correct
8 Correct 68 ms 28464 KB Output is correct
9 Correct 65 ms 28640 KB Output is correct
10 Correct 68 ms 28536 KB Output is correct
11 Correct 8 ms 15196 KB Output is correct
12 Correct 69 ms 28544 KB Output is correct
13 Correct 69 ms 28452 KB Output is correct
14 Correct 65 ms 28596 KB Output is correct
15 Correct 74 ms 28756 KB Output is correct
16 Correct 66 ms 28604 KB Output is correct
17 Correct 74 ms 28520 KB Output is correct
18 Correct 71 ms 28500 KB Output is correct
19 Correct 65 ms 28496 KB Output is correct
20 Correct 76 ms 28516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 20660 KB Output is correct
2 Correct 75 ms 28564 KB Output is correct
3 Correct 87 ms 25736 KB Output is correct
4 Correct 57 ms 20600 KB Output is correct
5 Correct 78 ms 25428 KB Output is correct
6 Correct 79 ms 20684 KB Output is correct
7 Correct 87 ms 24916 KB Output is correct
8 Correct 75 ms 21072 KB Output is correct
9 Correct 60 ms 28568 KB Output is correct
10 Correct 111 ms 24108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14680 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 38 ms 18976 KB Output is correct
6 Correct 32 ms 26736 KB Output is correct
7 Correct 40 ms 24164 KB Output is correct
8 Correct 29 ms 19288 KB Output is correct
9 Correct 39 ms 22612 KB Output is correct
10 Correct 31 ms 19204 KB Output is correct
11 Correct 3 ms 14680 KB Output is correct
12 Correct 3 ms 14860 KB Output is correct
13 Correct 4 ms 14936 KB Output is correct
14 Correct 3 ms 14936 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14684 KB Output is correct
18 Correct 3 ms 14940 KB Output is correct
19 Correct 3 ms 14776 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 2 ms 14680 KB Output is correct
3 Correct 3 ms 14780 KB Output is correct
4 Correct 2 ms 14684 KB Output is correct
5 Correct 38 ms 18976 KB Output is correct
6 Correct 32 ms 26736 KB Output is correct
7 Correct 40 ms 24164 KB Output is correct
8 Correct 29 ms 19288 KB Output is correct
9 Correct 39 ms 22612 KB Output is correct
10 Correct 31 ms 19204 KB Output is correct
11 Correct 2 ms 14680 KB Output is correct
12 Correct 2 ms 14684 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 69 ms 28552 KB Output is correct
15 Correct 74 ms 28600 KB Output is correct
16 Correct 60 ms 28584 KB Output is correct
17 Correct 70 ms 28596 KB Output is correct
18 Correct 68 ms 28464 KB Output is correct
19 Correct 65 ms 28640 KB Output is correct
20 Correct 68 ms 28536 KB Output is correct
21 Correct 8 ms 15196 KB Output is correct
22 Correct 69 ms 28544 KB Output is correct
23 Correct 69 ms 28452 KB Output is correct
24 Correct 65 ms 28596 KB Output is correct
25 Correct 74 ms 28756 KB Output is correct
26 Correct 66 ms 28604 KB Output is correct
27 Correct 74 ms 28520 KB Output is correct
28 Correct 71 ms 28500 KB Output is correct
29 Correct 65 ms 28496 KB Output is correct
30 Correct 76 ms 28516 KB Output is correct
31 Correct 78 ms 20660 KB Output is correct
32 Correct 75 ms 28564 KB Output is correct
33 Correct 87 ms 25736 KB Output is correct
34 Correct 57 ms 20600 KB Output is correct
35 Correct 78 ms 25428 KB Output is correct
36 Correct 79 ms 20684 KB Output is correct
37 Correct 87 ms 24916 KB Output is correct
38 Correct 75 ms 21072 KB Output is correct
39 Correct 60 ms 28568 KB Output is correct
40 Correct 111 ms 24108 KB Output is correct
41 Correct 3 ms 14680 KB Output is correct
42 Correct 3 ms 14860 KB Output is correct
43 Correct 4 ms 14936 KB Output is correct
44 Correct 3 ms 14936 KB Output is correct
45 Correct 3 ms 14684 KB Output is correct
46 Correct 3 ms 14684 KB Output is correct
47 Correct 3 ms 14684 KB Output is correct
48 Correct 3 ms 14940 KB Output is correct
49 Correct 3 ms 14776 KB Output is correct
50 Correct 4 ms 14940 KB Output is correct
51 Correct 75 ms 21076 KB Output is correct
52 Correct 73 ms 28476 KB Output is correct
53 Correct 99 ms 24272 KB Output is correct
54 Correct 62 ms 20560 KB Output is correct
55 Correct 87 ms 20648 KB Output is correct
56 Correct 70 ms 28564 KB Output is correct
57 Correct 81 ms 24916 KB Output is correct
58 Correct 66 ms 20748 KB Output is correct
59 Correct 75 ms 21076 KB Output is correct
60 Correct 79 ms 28620 KB Output is correct
61 Correct 85 ms 24972 KB Output is correct
62 Correct 58 ms 20504 KB Output is correct
63 Correct 75 ms 20712 KB Output is correct
64 Correct 69 ms 28576 KB Output is correct
65 Correct 113 ms 24780 KB Output is correct
66 Correct 58 ms 20556 KB Output is correct
67 Correct 75 ms 20556 KB Output is correct
68 Correct 72 ms 28472 KB Output is correct
69 Correct 79 ms 23636 KB Output is correct
70 Correct 65 ms 20792 KB Output is correct