Submission #1116885

# Submission time Handle Problem Language Result Execution time Memory
1116885 2024-11-22T14:08:45 Z Zero_OP Election Campaign (JOI15_election_campaign) C++14
100 / 100
130 ms 54468 KB
#include <bits/stdc++.h>

using namespace std;

#define dbg(x) "[" #x " = " << (x) << "]"

template<typename T>
bool maximize(T& a, const T& b){
    if(a < b) {
        return a = b, true;
    } return false;
}

struct fenwick_tree{
    vector<int> bit;
    fenwick_tree() : bit() {}
    fenwick_tree(int n) : bit(n + 1, 0) {}

    void update(int i, int v){
        for(; i < (int)bit.size(); i += i & (-i)) bit[i] += v;
    }

    void update(int l, int r, int v){
        update(l, +v);
        update(r + 1, -v);
    }

    int query(int i){
        int sum = 0;
        for(; i > 0; i -= i & (-i)) sum += bit[i];
        return sum;
    }

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

struct heavy_light_decomposition{
    int N, timerHLD;
    vector<int> depth, par, head, sz, tin, tout, dp, sum_dp;
    vector<vector<int>> adj;
    vector<vector<pair<int, int>>> one_subtree;
    vector<vector<tuple<int, int, int>>> two_subtrees;
    fenwick_tree ft;

    heavy_light_decomposition(int N) :
        timerHLD(0),
        N(N),
        depth(N),
        par(N, -1),
        head(N, -1),
        sz(N, -1),
        adj(N),
        tin(N),
        tout(N),
        one_subtree(N),
        two_subtrees(N),
        dp(N, 0),
        sum_dp(N, 0),
        ft(N) {}

    void add_edge(int u, int v){
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
//        cout << dbg(u) << dbg(v) << '\n';
    }

    void dfs_sz(int u){
        sz[u] = 1;
        for(auto& v : adj[u]){
            adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
            depth[v] = depth[u] + 1;
            par[v] = u;
            dfs_sz(v);
            sz[u] += sz[v];
            if(sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
        }
    }

    void dfs_hld(int u, int hd){
        tin[u] = ++timerHLD;
        head[u] = hd;

        for(auto v : adj[u]){
            if(v == adj[u][0]) dfs_hld(v, hd);
            else dfs_hld(v, v);
        }

        tout[u] = timerHLD;
    }

    void preprocess(int rt = 0){
        dfs_sz(rt);
        dfs_hld(rt, rt);
    }

    bool in_subtree(int u, int v){
        return tin[u] <= tin[v] && tout[v] <= tout[u];
    }

    int get_lca(int u, int v){
        if(in_subtree(u, v)) return u;
        if(in_subtree(v, u)) return v;

        while(head[u] != head[v]){
            if(depth[head[u]] < depth[head[v]]) swap(u, v);
            u = par[head[u]];
        }

        if(tin[u] > tin[v]) swap(u, v);
        return u;
    }

    void add_path(int u, int v, int w){
        if(tin[u] > tin[v]) swap(u, v);
        int lca = get_lca(u, v);

        if(lca == u){
            one_subtree[lca].emplace_back(v, w);
        } else{
            two_subtrees[lca].emplace_back(u, v, w);
        }
    }

    int find_exact_subtree(int parent, int u){
        int l = 0, r = (int)adj[parent].size() - 1, ans = -1;
        while(l <= r){
            int mid = l + r >> 1;
            if(tin[adj[parent][mid]] <= tin[u]) ans = mid, l = mid + 1;
            else r = mid - 1;
        }

        assert(ans != -1);
        return adj[parent][ans];
    }

    void update(int u, int val){
        ft.update(tin[u], val);
    }

    int sum_path(int parent, int u){
        if(depth[u] - depth[parent] < 0) return 0;
        int res = 0;
        while(head[parent] != head[u]){
            res += ft.query(tin[head[u]], tin[u]);
            u = par[head[u]];
        }

        assert(tin[parent] <= tin[u]);
        res += ft.query(tin[parent], tin[u]);
        return res;
    }

    void solve(int u){
        sum_dp[u] = 0;
        for(int v : adj[u]){
            solve(v);
            sum_dp[u] += dp[v];
        }

        //case 1 : skip u
        maximize(dp[u], sum_dp[u]);

        //case 2 : consider every paths where lca(A, B) = u
        ///case 2.1 : A = u or B = u
        for(auto [x, w] : one_subtree[u]){
            int px = find_exact_subtree(u, x);

            int cur = sum_dp[u] + sum_dp[x] + sum_path(px, x) - dp[px];
            maximize(dp[u], cur + w);
        }

        ///case 2.2 : A \neq u and B \neq u
        for(auto [x, y, w] : two_subtrees[u]){
            int px = find_exact_subtree(u, x);
            int py = find_exact_subtree(u, y);

            int cur = sum_dp[u] + sum_dp[x] + sum_dp[y] - dp[px] - dp[py] + sum_path(px, x) + sum_path(py, y);
            maximize(dp[u], cur + w);
        }

        for(auto v : adj[u]){
            update(v, sum_dp[u] - dp[v]);
        }
    }
};

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

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    int N; cin >> N;
    heavy_light_decomposition tr(N);
    for(int i = 1; i < N; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        tr.add_edge(u, v);
    }

    tr.preprocess();

    int M; cin >> M;
    vector<tuple<int, int, int>> paths;
    for(int i = 0; i < M; ++i){
        int u, v, w;
        cin >> u >> v >> w;
        --u, --v;
        tr.add_path(u, v, w);
    }

    tr.solve(0);
    cout << tr.dp[0] << '\n';

    return 0;
}

Compilation message

election_campaign.cpp: In constructor 'heavy_light_decomposition::heavy_light_decomposition(int)':
election_campaign.cpp:40:12: warning: 'heavy_light_decomposition::timerHLD' will be initialized after [-Wreorder]
   40 |     int N, timerHLD;
      |            ^~~~~~~~
election_campaign.cpp:40:9: warning:   'int heavy_light_decomposition::N' [-Wreorder]
   40 |     int N, timerHLD;
      |         ^
election_campaign.cpp:47:5: warning:   when initialized here [-Wreorder]
   47 |     heavy_light_decomposition(int N) :
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:42:25: warning: 'heavy_light_decomposition::adj' will be initialized after [-Wreorder]
   42 |     vector<vector<int>> adj;
      |                         ^~~
election_campaign.cpp:41:39: warning:   'std::vector<int> heavy_light_decomposition::tin' [-Wreorder]
   41 |     vector<int> depth, par, head, sz, tin, tout, dp, sum_dp;
      |                                       ^~~
election_campaign.cpp:47:5: warning:   when initialized here [-Wreorder]
   47 |     heavy_light_decomposition(int N) :
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:44:42: warning: 'heavy_light_decomposition::two_subtrees' will be initialized after [-Wreorder]
   44 |     vector<vector<tuple<int, int, int>>> two_subtrees;
      |                                          ^~~~~~~~~~~~
election_campaign.cpp:41:50: warning:   'std::vector<int> heavy_light_decomposition::dp' [-Wreorder]
   41 |     vector<int> depth, par, head, sz, tin, tout, dp, sum_dp;
      |                                                  ^~
election_campaign.cpp:47:5: warning:   when initialized here [-Wreorder]
   47 |     heavy_light_decomposition(int N) :
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp: In member function 'int heavy_light_decomposition::find_exact_subtree(int, int)':
election_campaign.cpp:129:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  129 |             int mid = l + r >> 1;
      |                       ~~^~~
election_campaign.cpp: In member function 'void heavy_light_decomposition::solve(int)':
election_campaign.cpp:167:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  167 |         for(auto [x, w] : one_subtree[u]){
      |                  ^
election_campaign.cpp:175:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  175 |         for(auto [x, y, w] : two_subtrees[u]){
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 37 ms 15440 KB Output is correct
6 Correct 68 ms 51280 KB Output is correct
7 Correct 66 ms 38632 KB Output is correct
8 Correct 34 ms 15688 KB Output is correct
9 Correct 54 ms 31696 KB Output is correct
10 Correct 30 ms 15692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 848 KB Output is correct
4 Correct 97 ms 54096 KB Output is correct
5 Correct 90 ms 54208 KB Output is correct
6 Correct 91 ms 54212 KB Output is correct
7 Correct 98 ms 54088 KB Output is correct
8 Correct 91 ms 54072 KB Output is correct
9 Correct 84 ms 54088 KB Output is correct
10 Correct 94 ms 54088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 848 KB Output is correct
4 Correct 97 ms 54096 KB Output is correct
5 Correct 90 ms 54208 KB Output is correct
6 Correct 91 ms 54212 KB Output is correct
7 Correct 98 ms 54088 KB Output is correct
8 Correct 91 ms 54072 KB Output is correct
9 Correct 84 ms 54088 KB Output is correct
10 Correct 94 ms 54088 KB Output is correct
11 Correct 6 ms 1360 KB Output is correct
12 Correct 95 ms 54468 KB Output is correct
13 Correct 103 ms 54264 KB Output is correct
14 Correct 111 ms 54344 KB Output is correct
15 Correct 117 ms 54344 KB Output is correct
16 Correct 99 ms 54468 KB Output is correct
17 Correct 103 ms 54436 KB Output is correct
18 Correct 99 ms 54264 KB Output is correct
19 Correct 107 ms 54344 KB Output is correct
20 Correct 98 ms 54288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 18188 KB Output is correct
2 Correct 117 ms 54088 KB Output is correct
3 Correct 130 ms 40720 KB Output is correct
4 Correct 80 ms 18688 KB Output is correct
5 Correct 96 ms 38076 KB Output is correct
6 Correct 82 ms 18632 KB Output is correct
7 Correct 105 ms 37472 KB Output is correct
8 Correct 90 ms 18248 KB Output is correct
9 Correct 98 ms 54048 KB Output is correct
10 Correct 107 ms 33396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 37 ms 15440 KB Output is correct
6 Correct 68 ms 51280 KB Output is correct
7 Correct 66 ms 38632 KB Output is correct
8 Correct 34 ms 15688 KB Output is correct
9 Correct 54 ms 31696 KB Output is correct
10 Correct 30 ms 15692 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 848 KB Output is correct
13 Correct 2 ms 848 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 1 ms 592 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 2 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 37 ms 15440 KB Output is correct
6 Correct 68 ms 51280 KB Output is correct
7 Correct 66 ms 38632 KB Output is correct
8 Correct 34 ms 15688 KB Output is correct
9 Correct 54 ms 31696 KB Output is correct
10 Correct 30 ms 15692 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 848 KB Output is correct
14 Correct 97 ms 54096 KB Output is correct
15 Correct 90 ms 54208 KB Output is correct
16 Correct 91 ms 54212 KB Output is correct
17 Correct 98 ms 54088 KB Output is correct
18 Correct 91 ms 54072 KB Output is correct
19 Correct 84 ms 54088 KB Output is correct
20 Correct 94 ms 54088 KB Output is correct
21 Correct 6 ms 1360 KB Output is correct
22 Correct 95 ms 54468 KB Output is correct
23 Correct 103 ms 54264 KB Output is correct
24 Correct 111 ms 54344 KB Output is correct
25 Correct 117 ms 54344 KB Output is correct
26 Correct 99 ms 54468 KB Output is correct
27 Correct 103 ms 54436 KB Output is correct
28 Correct 99 ms 54264 KB Output is correct
29 Correct 107 ms 54344 KB Output is correct
30 Correct 98 ms 54288 KB Output is correct
31 Correct 120 ms 18188 KB Output is correct
32 Correct 117 ms 54088 KB Output is correct
33 Correct 130 ms 40720 KB Output is correct
34 Correct 80 ms 18688 KB Output is correct
35 Correct 96 ms 38076 KB Output is correct
36 Correct 82 ms 18632 KB Output is correct
37 Correct 105 ms 37472 KB Output is correct
38 Correct 90 ms 18248 KB Output is correct
39 Correct 98 ms 54048 KB Output is correct
40 Correct 107 ms 33396 KB Output is correct
41 Correct 2 ms 592 KB Output is correct
42 Correct 2 ms 848 KB Output is correct
43 Correct 2 ms 848 KB Output is correct
44 Correct 1 ms 592 KB Output is correct
45 Correct 1 ms 592 KB Output is correct
46 Correct 1 ms 592 KB Output is correct
47 Correct 1 ms 592 KB Output is correct
48 Correct 1 ms 848 KB Output is correct
49 Correct 1 ms 592 KB Output is correct
50 Correct 2 ms 848 KB Output is correct
51 Correct 77 ms 18364 KB Output is correct
52 Correct 99 ms 54344 KB Output is correct
53 Correct 87 ms 34304 KB Output is correct
54 Correct 68 ms 18564 KB Output is correct
55 Correct 107 ms 18432 KB Output is correct
56 Correct 95 ms 54348 KB Output is correct
57 Correct 91 ms 36132 KB Output is correct
58 Correct 79 ms 18752 KB Output is correct
59 Correct 93 ms 18504 KB Output is correct
60 Correct 103 ms 54344 KB Output is correct
61 Correct 92 ms 36680 KB Output is correct
62 Correct 78 ms 18856 KB Output is correct
63 Correct 117 ms 18484 KB Output is correct
64 Correct 98 ms 54364 KB Output is correct
65 Correct 102 ms 37252 KB Output is correct
66 Correct 85 ms 18388 KB Output is correct
67 Correct 115 ms 18624 KB Output is correct
68 Correct 104 ms 54356 KB Output is correct
69 Correct 85 ms 31048 KB Output is correct
70 Correct 73 ms 18856 KB Output is correct