답안 #825562

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
825562 2023-08-15T03:23:27 Z Koyote Election Campaign (JOI15_election_campaign) C++14
100 / 100
209 ms 37184 KB
#include <bits/stdc++.h>
using namespace std;

template<class T> bool ckmax(T &u, const T &v) { return u < v ? (u = v, true) : false; }
template<class T> bool ckmin(T &u, const T &v) { return u > v ? (u = v, true) : false; }

struct lca_algo {
//   private:
    int nodeCnt, root;
    vector<int> tin, tout;
    vector<int64_t> depth;
    vector<vector<pair<int, int>>> adj;
    vector<vector<int>> par;
    static int floor_log2(int x) { return x ? 31 - __builtin_clz(x) : -1; }
    void dfs(int u) {
        static int timer = 0;
        tin[u] = ++timer;
        for (int j = 1; j < floor_log2(nodeCnt); j++)
            par[u][j] = par[par[u][j - 1]][j - 1];
        for (auto v : adj[u]) if (v.first != par[u][0]) {
            par[v.first][0] = u;
            depth[v.first] = depth[u] + v.second;
            dfs(v.first);
        }
        tout[u] = ++timer;
    }
  public:
    lca_algo() {}
    lca_algo(int _n, int _root = 0) : nodeCnt(_n), root(_root), tin(_n), tout(_n),
        depth(_n), adj(_n), par(_n, vector<int>(floor_log2(_n), -1)) {}
    void add_edge(int u, int v, int w = 1) {
        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }
    void init() {
        par[root][0] = root;
        depth[root] = 0;
        dfs(root);
    }
    bool is_ancestor(int u, int v) {
        return tin[u] <= tin[v] && tout[u] >= tout[v];
    }
    int lcaQuery(int u, int v) {
        if (is_ancestor(u, v)) return u;
        if (is_ancestor(v, u)) return v;
        for (int j = floor_log2(nodeCnt) - 1; j >= 0; j--)
            if (!is_ancestor(par[u][j], v)) u = par[u][j];
        return par[u][0];
    }
    int64_t distQuery(int u, int v) {
        return depth[u] + depth[v] - 2 * depth[lcaQuery(u, v)];
    }
};

struct fenwick_tree {
  private:
    int tree_size;
    vector<int64_t> ft1, ft2;
    int lowbit(int i) { return i & -i; }
    int64_t sum(vector<int64_t> &ft, int p) {
        int64_t res = 0; for (; p; p -= lowbit(p)) res += ft[p];
        return res;
    }
    void upd(vector<int64_t> &ft, int p, int64_t v) { for (; p <= tree_size; p += lowbit(p)) ft[p] += v; }
    void update(int p, int64_t v)                   { upd(ft1, p, v), upd(ft2, p, p * v); }
    int64_t query(int p)                            { return (p + 1) * sum(ft1, p) - sum(ft2, p); }
  public:
    fenwick_tree(int _ts) : tree_size(_ts), ft1(_ts + 1), ft2(_ts + 1) {}
    void update(int l, int r, int64_t v)            { assert(l <= r); update(l, v); update(r + 1, -v); }
    int64_t query(int l, int r)                     { assert(l <= r); return query(r) - query(l - 1); }
};

typedef long long ll;

const int N = 1e5 + 2;
int n, m;
vector<array<int, 3>> plans[N];
ll dp[N][2];
lca_algo LCA(N);
fenwick_tree fw(N << 1);

void dfs_dp(int u) {
    for (auto v : LCA.adj[u]) if (v.first != LCA.par[u][0]) {
        dfs_dp(v.first);
        dp[u][0] += dp[v.first][1];
    }
    ckmax(dp[u][1], dp[u][0]);
    for (auto it : plans[u]) {
        int a = it[0], b = it[1], c = it[2];
        ckmax(dp[u][1], c + fw.query(1, LCA.tin[a]) + fw.query(1, LCA.tin[b]) + dp[u][0]);
    }
    fw.update(LCA.tin[u], LCA.tin[u], dp[u][0] - dp[u][1]);
    fw.update(LCA.tout[u], LCA.tout[u], dp[u][1] - dp[u][0]);
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v; --u, --v;
        LCA.add_edge(u, v);
    }
    LCA.init();
    cin >> m;
    for (int i = 0; i < m; i++) {
        int a, b, c; cin >> a >> b >> c; --a, --b;
        plans[LCA.lcaQuery(a, b)].push_back({a, b, c});
    }
    dfs_dp(0);
    cout << dp[0][1] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19796 KB Output is correct
2 Correct 11 ms 19796 KB Output is correct
3 Correct 11 ms 19796 KB Output is correct
4 Correct 12 ms 19928 KB Output is correct
5 Correct 124 ms 26552 KB Output is correct
6 Correct 64 ms 33564 KB Output is correct
7 Correct 123 ms 31164 KB Output is correct
8 Correct 86 ms 25152 KB Output is correct
9 Correct 137 ms 29680 KB Output is correct
10 Correct 97 ms 25036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19796 KB Output is correct
2 Correct 11 ms 19800 KB Output is correct
3 Correct 12 ms 20052 KB Output is correct
4 Correct 91 ms 36892 KB Output is correct
5 Correct 106 ms 36776 KB Output is correct
6 Correct 90 ms 36832 KB Output is correct
7 Correct 105 ms 37000 KB Output is correct
8 Correct 93 ms 36816 KB Output is correct
9 Correct 90 ms 36784 KB Output is correct
10 Correct 92 ms 36804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19796 KB Output is correct
2 Correct 11 ms 19800 KB Output is correct
3 Correct 12 ms 20052 KB Output is correct
4 Correct 91 ms 36892 KB Output is correct
5 Correct 106 ms 36776 KB Output is correct
6 Correct 90 ms 36832 KB Output is correct
7 Correct 105 ms 37000 KB Output is correct
8 Correct 93 ms 36816 KB Output is correct
9 Correct 90 ms 36784 KB Output is correct
10 Correct 92 ms 36804 KB Output is correct
11 Correct 17 ms 20820 KB Output is correct
12 Correct 106 ms 37060 KB Output is correct
13 Correct 109 ms 37140 KB Output is correct
14 Correct 93 ms 37112 KB Output is correct
15 Correct 93 ms 37068 KB Output is correct
16 Correct 91 ms 37080 KB Output is correct
17 Correct 95 ms 37116 KB Output is correct
18 Correct 92 ms 37152 KB Output is correct
19 Correct 91 ms 37152 KB Output is correct
20 Correct 102 ms 37136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 29204 KB Output is correct
2 Correct 96 ms 36916 KB Output is correct
3 Correct 175 ms 34124 KB Output is correct
4 Correct 123 ms 28192 KB Output is correct
5 Correct 155 ms 33672 KB Output is correct
6 Correct 131 ms 28032 KB Output is correct
7 Correct 162 ms 33392 KB Output is correct
8 Correct 149 ms 29388 KB Output is correct
9 Correct 88 ms 36856 KB Output is correct
10 Correct 179 ms 32516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19796 KB Output is correct
2 Correct 11 ms 19796 KB Output is correct
3 Correct 11 ms 19796 KB Output is correct
4 Correct 12 ms 19928 KB Output is correct
5 Correct 124 ms 26552 KB Output is correct
6 Correct 64 ms 33564 KB Output is correct
7 Correct 123 ms 31164 KB Output is correct
8 Correct 86 ms 25152 KB Output is correct
9 Correct 137 ms 29680 KB Output is correct
10 Correct 97 ms 25036 KB Output is correct
11 Correct 13 ms 19924 KB Output is correct
12 Correct 13 ms 20012 KB Output is correct
13 Correct 12 ms 19932 KB Output is correct
14 Correct 16 ms 20020 KB Output is correct
15 Correct 14 ms 19912 KB Output is correct
16 Correct 12 ms 19936 KB Output is correct
17 Correct 13 ms 19916 KB Output is correct
18 Correct 12 ms 19932 KB Output is correct
19 Correct 13 ms 19924 KB Output is correct
20 Correct 11 ms 20052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19796 KB Output is correct
2 Correct 11 ms 19796 KB Output is correct
3 Correct 11 ms 19796 KB Output is correct
4 Correct 12 ms 19928 KB Output is correct
5 Correct 124 ms 26552 KB Output is correct
6 Correct 64 ms 33564 KB Output is correct
7 Correct 123 ms 31164 KB Output is correct
8 Correct 86 ms 25152 KB Output is correct
9 Correct 137 ms 29680 KB Output is correct
10 Correct 97 ms 25036 KB Output is correct
11 Correct 12 ms 19796 KB Output is correct
12 Correct 11 ms 19800 KB Output is correct
13 Correct 12 ms 20052 KB Output is correct
14 Correct 91 ms 36892 KB Output is correct
15 Correct 106 ms 36776 KB Output is correct
16 Correct 90 ms 36832 KB Output is correct
17 Correct 105 ms 37000 KB Output is correct
18 Correct 93 ms 36816 KB Output is correct
19 Correct 90 ms 36784 KB Output is correct
20 Correct 92 ms 36804 KB Output is correct
21 Correct 17 ms 20820 KB Output is correct
22 Correct 106 ms 37060 KB Output is correct
23 Correct 109 ms 37140 KB Output is correct
24 Correct 93 ms 37112 KB Output is correct
25 Correct 93 ms 37068 KB Output is correct
26 Correct 91 ms 37080 KB Output is correct
27 Correct 95 ms 37116 KB Output is correct
28 Correct 92 ms 37152 KB Output is correct
29 Correct 91 ms 37152 KB Output is correct
30 Correct 102 ms 37136 KB Output is correct
31 Correct 151 ms 29204 KB Output is correct
32 Correct 96 ms 36916 KB Output is correct
33 Correct 175 ms 34124 KB Output is correct
34 Correct 123 ms 28192 KB Output is correct
35 Correct 155 ms 33672 KB Output is correct
36 Correct 131 ms 28032 KB Output is correct
37 Correct 162 ms 33392 KB Output is correct
38 Correct 149 ms 29388 KB Output is correct
39 Correct 88 ms 36856 KB Output is correct
40 Correct 179 ms 32516 KB Output is correct
41 Correct 13 ms 19924 KB Output is correct
42 Correct 13 ms 20012 KB Output is correct
43 Correct 12 ms 19932 KB Output is correct
44 Correct 16 ms 20020 KB Output is correct
45 Correct 14 ms 19912 KB Output is correct
46 Correct 12 ms 19936 KB Output is correct
47 Correct 13 ms 19916 KB Output is correct
48 Correct 12 ms 19932 KB Output is correct
49 Correct 13 ms 19924 KB Output is correct
50 Correct 11 ms 20052 KB Output is correct
51 Correct 148 ms 29776 KB Output is correct
52 Correct 92 ms 37172 KB Output is correct
53 Correct 190 ms 32720 KB Output is correct
54 Correct 116 ms 28276 KB Output is correct
55 Correct 147 ms 29652 KB Output is correct
56 Correct 100 ms 37144 KB Output is correct
57 Correct 209 ms 33528 KB Output is correct
58 Correct 119 ms 28164 KB Output is correct
59 Correct 143 ms 29720 KB Output is correct
60 Correct 94 ms 37064 KB Output is correct
61 Correct 168 ms 33612 KB Output is correct
62 Correct 146 ms 28296 KB Output is correct
63 Correct 148 ms 29568 KB Output is correct
64 Correct 101 ms 37052 KB Output is correct
65 Correct 171 ms 33396 KB Output is correct
66 Correct 120 ms 28292 KB Output is correct
67 Correct 144 ms 29504 KB Output is correct
68 Correct 97 ms 37184 KB Output is correct
69 Correct 176 ms 32300 KB Output is correct
70 Correct 123 ms 28384 KB Output is correct