Submission #891281

# Submission time Handle Problem Language Result Execution time Memory
891281 2023-12-22T16:18:11 Z viwlesxq Election Campaign (JOI15_election_campaign) C++17
100 / 100
236 ms 43600 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()

template<class S, class T>
bool chmin(S &a, const T &b) {
    return a > b && (a = b) == b;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
    return a < b && (a = b) == b;
}
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const int mod = 998244353;
const int N = 1e5 + 1;

struct Fenwick {
    int n;
    vector<int> t;
    void build(int n) {
        this->n = n;
        t.assign(n + 1, 0);
    }

    void upd(int i, int x) {
        for (; i <= n; i += i & -i) {
            t[i] += x;
        }
    }

    int sum(int i) {
        int res = 0;
        for (; i > 0; i -= i & -i) {
            res += t[i];
        }
        return res;
    } int get(int l, int r) {
        return sum(r) - sum(l - 1);
    }
};

int position, timer, up[N][17];
vector<int> g[N], sub(N), heavy(N), head(N), pos(N), dp(N), tin(N), tout(N), depth(N);
vector<array<int, 3>> path[N];
vector<Fenwick> t(2);

void dfs(int v, int p) {
    tin[v] = ++timer;
    sub[v] = 1;
    up[v][0] = p;
    for (int i = 1; i < 17; ++i) {
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for (int to : g[v]) {
        if (to != p) {
            depth[to] = depth[v] + 1;
            dfs(to, v);
            sub[v] += sub[to];
        }
    }
    for (int to : g[v]) {
        if (to != p && sub[to] * 2 >= sub[v]) {
            heavy[v] = to;
        }
    }
    tout[v] = ++timer;
}

void decompose(int v, int p, int h) {
    head[v] = h;
    pos[v] = ++position;
    if (heavy[v]) {
        decompose(heavy[v], v, h);
    }
    for (int to : g[v]) {
        if (to != p && to != heavy[v]) {
            decompose(to, v, to);
        }
    }
}

bool upper(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
    if (upper(u, v)) {
        return u;
    }
    if (upper(v, u)) {
        return v;
    }
    for (int i = 16; i >= 0; --i) {
        if (!upper(up[u][i], v)) {
            u = up[u][i];
        }
    }
    return up[u][0];
}

int count(int a, int b, int i) {
    int res = 0;
    while (head[a] != head[b]) {
        if (depth[b] > depth[a]) {
            swap(a, b);
        }
        res += t[i].get(pos[head[a]], pos[a]);
        a = up[head[a]][0];
    }
    if (depth[b] > depth[a]) {
        swap(a, b);
    }
    res += t[i].get(pos[b], pos[a]);
    return res;
}

void count_dp(int v, int p) {
    int sum = 0;
    for (int to : g[v]) {
        if (to != p) {
            count_dp(to, v);
            sum += dp[to];
        }
    }
    dp[v] = sum;
    for (auto [a, b, c] : path[v]) {
        chmax(dp[v], count(a, b, 0) - count(a, b, 1) + sum + c);
    }
    t[0].upd(pos[v], sum);
    t[1].upd(pos[v], dp[v]);
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    t[0].build(n), t[1].build(n);
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 1);
    decompose(1, -1, 1);
    int m;
    cin >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        path[lca(a, b)].push_back({a, b, c});
    }
    count_dp(1, 1);
    cout << dp[1];
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12892 KB Output is correct
2 Correct 5 ms 13096 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 79 ms 31392 KB Output is correct
6 Correct 50 ms 38412 KB Output is correct
7 Correct 85 ms 35924 KB Output is correct
8 Correct 49 ms 31324 KB Output is correct
9 Correct 65 ms 34384 KB Output is correct
10 Correct 45 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12888 KB Output is correct
2 Correct 5 ms 12888 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 76 ms 43028 KB Output is correct
5 Correct 72 ms 43092 KB Output is correct
6 Correct 75 ms 43280 KB Output is correct
7 Correct 74 ms 43184 KB Output is correct
8 Correct 74 ms 43088 KB Output is correct
9 Correct 70 ms 43088 KB Output is correct
10 Correct 73 ms 43020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12888 KB Output is correct
2 Correct 5 ms 12888 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 76 ms 43028 KB Output is correct
5 Correct 72 ms 43092 KB Output is correct
6 Correct 75 ms 43280 KB Output is correct
7 Correct 74 ms 43184 KB Output is correct
8 Correct 74 ms 43088 KB Output is correct
9 Correct 70 ms 43088 KB Output is correct
10 Correct 73 ms 43020 KB Output is correct
11 Correct 10 ms 14428 KB Output is correct
12 Correct 78 ms 43304 KB Output is correct
13 Correct 76 ms 43340 KB Output is correct
14 Correct 72 ms 43364 KB Output is correct
15 Correct 75 ms 43348 KB Output is correct
16 Correct 73 ms 43508 KB Output is correct
17 Correct 80 ms 43432 KB Output is correct
18 Correct 74 ms 43328 KB Output is correct
19 Correct 75 ms 43396 KB Output is correct
20 Correct 75 ms 43548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 35404 KB Output is correct
2 Correct 70 ms 43048 KB Output is correct
3 Correct 138 ms 40508 KB Output is correct
4 Correct 116 ms 35844 KB Output is correct
5 Correct 127 ms 39820 KB Output is correct
6 Correct 106 ms 35876 KB Output is correct
7 Correct 169 ms 39404 KB Output is correct
8 Correct 143 ms 35500 KB Output is correct
9 Correct 71 ms 43092 KB Output is correct
10 Correct 183 ms 38436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12892 KB Output is correct
2 Correct 5 ms 13096 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 79 ms 31392 KB Output is correct
6 Correct 50 ms 38412 KB Output is correct
7 Correct 85 ms 35924 KB Output is correct
8 Correct 49 ms 31324 KB Output is correct
9 Correct 65 ms 34384 KB Output is correct
10 Correct 45 ms 31324 KB Output is correct
11 Correct 4 ms 13148 KB Output is correct
12 Correct 4 ms 13268 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 5 ms 13144 KB Output is correct
15 Correct 5 ms 13148 KB Output is correct
16 Correct 5 ms 13108 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 5 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 13148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12892 KB Output is correct
2 Correct 5 ms 13096 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 4 ms 13148 KB Output is correct
5 Correct 79 ms 31392 KB Output is correct
6 Correct 50 ms 38412 KB Output is correct
7 Correct 85 ms 35924 KB Output is correct
8 Correct 49 ms 31324 KB Output is correct
9 Correct 65 ms 34384 KB Output is correct
10 Correct 45 ms 31324 KB Output is correct
11 Correct 4 ms 12888 KB Output is correct
12 Correct 5 ms 12888 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 76 ms 43028 KB Output is correct
15 Correct 72 ms 43092 KB Output is correct
16 Correct 75 ms 43280 KB Output is correct
17 Correct 74 ms 43184 KB Output is correct
18 Correct 74 ms 43088 KB Output is correct
19 Correct 70 ms 43088 KB Output is correct
20 Correct 73 ms 43020 KB Output is correct
21 Correct 10 ms 14428 KB Output is correct
22 Correct 78 ms 43304 KB Output is correct
23 Correct 76 ms 43340 KB Output is correct
24 Correct 72 ms 43364 KB Output is correct
25 Correct 75 ms 43348 KB Output is correct
26 Correct 73 ms 43508 KB Output is correct
27 Correct 80 ms 43432 KB Output is correct
28 Correct 74 ms 43328 KB Output is correct
29 Correct 75 ms 43396 KB Output is correct
30 Correct 75 ms 43548 KB Output is correct
31 Correct 182 ms 35404 KB Output is correct
32 Correct 70 ms 43048 KB Output is correct
33 Correct 138 ms 40508 KB Output is correct
34 Correct 116 ms 35844 KB Output is correct
35 Correct 127 ms 39820 KB Output is correct
36 Correct 106 ms 35876 KB Output is correct
37 Correct 169 ms 39404 KB Output is correct
38 Correct 143 ms 35500 KB Output is correct
39 Correct 71 ms 43092 KB Output is correct
40 Correct 183 ms 38436 KB Output is correct
41 Correct 4 ms 13148 KB Output is correct
42 Correct 4 ms 13268 KB Output is correct
43 Correct 4 ms 13148 KB Output is correct
44 Correct 5 ms 13144 KB Output is correct
45 Correct 5 ms 13148 KB Output is correct
46 Correct 5 ms 13108 KB Output is correct
47 Correct 5 ms 13148 KB Output is correct
48 Correct 5 ms 13148 KB Output is correct
49 Correct 4 ms 13148 KB Output is correct
50 Correct 4 ms 13148 KB Output is correct
51 Correct 145 ms 35728 KB Output is correct
52 Correct 75 ms 43348 KB Output is correct
53 Correct 162 ms 38756 KB Output is correct
54 Correct 97 ms 36048 KB Output is correct
55 Correct 196 ms 35652 KB Output is correct
56 Correct 75 ms 43420 KB Output is correct
57 Correct 134 ms 39508 KB Output is correct
58 Correct 106 ms 35848 KB Output is correct
59 Correct 139 ms 35844 KB Output is correct
60 Correct 74 ms 43348 KB Output is correct
61 Correct 154 ms 40024 KB Output is correct
62 Correct 113 ms 36308 KB Output is correct
63 Correct 236 ms 35696 KB Output is correct
64 Correct 74 ms 43308 KB Output is correct
65 Correct 142 ms 40088 KB Output is correct
66 Correct 99 ms 35716 KB Output is correct
67 Correct 230 ms 36352 KB Output is correct
68 Correct 92 ms 43600 KB Output is correct
69 Correct 124 ms 38544 KB Output is correct
70 Correct 106 ms 36224 KB Output is correct