답안 #746811

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
746811 2023-05-23T06:37:14 Z GrindMachine Election Campaign (JOI15_election_campaign) C++17
100 / 100
288 ms 36908 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<ll> adj[N];

struct lca_algo {
    // LCA template (for graphs with 1-based indexing)

    int LOG = 1;
    vector<int> depth;
    vector<vector<int>> up;

    lca_algo() {

    }

    lca_algo(int n) {
        lca_init(n);
    }

    void lca_init(int n) {
        while ((1 << LOG) < n) LOG++;
        up = vector<vector<int>>(n + 1, vector<int>(LOG, 1));
        depth = vector<int>(n + 1);

        lca_dfs(1, -1);
    }

    void lca_dfs(int node, int par) {
        trav(child, adj[node]) {
            if (child == par) conts;

            up[child][0] = node;
            rep1(j, LOG - 1) {
                up[child][j] = up[up[child][j - 1]][j - 1];
            }

            depth[child] = depth[node] + 1;

            lca_dfs(child, node);
        }
    }

    int lift(int u, int k) {
        rep(j, LOG) {
            if (k & (1 << j)) {
                u = up[u][j];
            }
        }

        return u;
    }

    int query(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int k = depth[u] - depth[v];
        u = lift(u, k);

        if (u == v) return u;

        rev(j, LOG - 1, 0) {
            if (up[u][j] != up[v][j]) {
                u = up[u][j];
                v = up[v][j];
            }
        }

        u = up[u][0];
        return u;
    }

    int get_dis(int u, int v) {
        int lca = query(u, v);
        return depth[u] + depth[v] - 2 * depth[lca];
    }
};

template<typename T>
struct fenwick {
    int siz;
    vector<T> tree;

    fenwick(int n) {
        siz = n;
        tree = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void build(vector<T> &a, int n) {
        for (int i = 1; i <= n; ++i) {
            int par = i + lsb(i);
            tree[i] += a[i];

            if (par <= siz) {
                tree[par] += tree[i];
            }
        }
    }

    void pupd(int i, T v) {
        while (i <= siz) {
            tree[i] += v;
            i += lsb(i);
        }
    }

    T sum(int i) {
        T res = 0;

        while (i) {
            res += tree[i];
            i -= lsb(i);
        }

        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }
};

vector<array<ll,3>> here[N];
vector<ll> dp(N), adj_dp(N);
vector<ll> tin(N), tout(N);
vector<ll> par(N,-1);
ll timer = 1;
ll n;

void dfs1(ll u, ll p){
    tin[u] = timer++;
    trav(v, adj[u]){
        if(v == p) conts;
        par[v] = u;
        dfs1(v, u);
    }
    tout[u] = timer++;
}

fenwick<ll> fenw(2*N);

void dfs2(ll u, ll p){
    trav(v, adj[u]){
        if(v == p) conts;
        dfs2(v, u);
        adj_dp[u] += dp[v];
    }

    // dont pick any path going through u
    amax(dp[u], adj_dp[u]);

    // pick 1 path passing through u
    for(auto [x, y, w] : here[u]){
        ll val = w + fenw.query(1,tin[x]) + fenw.query(1,tin[y]) + adj_dp[u];
        amax(dp[u], val);

        /*

        ll x2 = x;
        while(x2 != u){
            val -= dp[x2];
            val += adj_dp[x2];
            x2 = par[x2];
        }

        ll y2 = y;
        while(y2 != u){
            val -= dp[y2];
            val += adj_dp[y2];
            y2 = par[y2];
        }

        val += adj_dp[u];
        amax(dp[u], val);

        */
    }

    fenw.pupd(tin[u], -dp[u] + adj_dp[u]);
    fenw.pupd(tout[u], dp[u] - adj_dp[u]);
}

void solve(int test_case)
{
    cin >> n;
    rep1(i,n-1){
        ll u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    lca_algo LCA(n);

    ll m; cin >> m;

    rep1(i,m){
        ll u,v,w; cin >> u >> v >> w;
        ll lca = LCA.query(u,v);
        here[lca].pb({u, v, w});
    }

    dfs1(1, -1);
    dfs2(1, -1);

    ll ans = dp[1];
    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 6 ms 10452 KB Output is correct
3 Correct 7 ms 10452 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 109 ms 25016 KB Output is correct
6 Correct 53 ms 30412 KB Output is correct
7 Correct 114 ms 28616 KB Output is correct
8 Correct 82 ms 25120 KB Output is correct
9 Correct 111 ms 27372 KB Output is correct
10 Correct 82 ms 25036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10476 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 8 ms 10688 KB Output is correct
4 Correct 119 ms 33984 KB Output is correct
5 Correct 123 ms 36480 KB Output is correct
6 Correct 115 ms 36532 KB Output is correct
7 Correct 122 ms 36428 KB Output is correct
8 Correct 121 ms 36368 KB Output is correct
9 Correct 118 ms 36388 KB Output is correct
10 Correct 121 ms 36424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10476 KB Output is correct
2 Correct 5 ms 10452 KB Output is correct
3 Correct 8 ms 10688 KB Output is correct
4 Correct 119 ms 33984 KB Output is correct
5 Correct 123 ms 36480 KB Output is correct
6 Correct 115 ms 36532 KB Output is correct
7 Correct 122 ms 36428 KB Output is correct
8 Correct 121 ms 36368 KB Output is correct
9 Correct 118 ms 36388 KB Output is correct
10 Correct 121 ms 36424 KB Output is correct
11 Correct 13 ms 11860 KB Output is correct
12 Correct 127 ms 36680 KB Output is correct
13 Correct 122 ms 36736 KB Output is correct
14 Correct 113 ms 36688 KB Output is correct
15 Correct 123 ms 36744 KB Output is correct
16 Correct 113 ms 36768 KB Output is correct
17 Correct 135 ms 36648 KB Output is correct
18 Correct 125 ms 36680 KB Output is correct
19 Correct 114 ms 36712 KB Output is correct
20 Correct 131 ms 36908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 28020 KB Output is correct
2 Correct 117 ms 33984 KB Output is correct
3 Correct 288 ms 31936 KB Output is correct
4 Correct 118 ms 30792 KB Output is correct
5 Correct 229 ms 33788 KB Output is correct
6 Correct 120 ms 30764 KB Output is correct
7 Correct 259 ms 33476 KB Output is correct
8 Correct 200 ms 30472 KB Output is correct
9 Correct 121 ms 36448 KB Output is correct
10 Correct 254 ms 32632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 6 ms 10452 KB Output is correct
3 Correct 7 ms 10452 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 109 ms 25016 KB Output is correct
6 Correct 53 ms 30412 KB Output is correct
7 Correct 114 ms 28616 KB Output is correct
8 Correct 82 ms 25120 KB Output is correct
9 Correct 111 ms 27372 KB Output is correct
10 Correct 82 ms 25036 KB Output is correct
11 Correct 6 ms 10612 KB Output is correct
12 Correct 6 ms 10708 KB Output is correct
13 Correct 6 ms 10580 KB Output is correct
14 Correct 6 ms 10580 KB Output is correct
15 Correct 7 ms 10652 KB Output is correct
16 Correct 6 ms 10580 KB Output is correct
17 Correct 6 ms 10672 KB Output is correct
18 Correct 6 ms 10580 KB Output is correct
19 Correct 6 ms 10668 KB Output is correct
20 Correct 6 ms 10708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10452 KB Output is correct
2 Correct 6 ms 10452 KB Output is correct
3 Correct 7 ms 10452 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 109 ms 25016 KB Output is correct
6 Correct 53 ms 30412 KB Output is correct
7 Correct 114 ms 28616 KB Output is correct
8 Correct 82 ms 25120 KB Output is correct
9 Correct 111 ms 27372 KB Output is correct
10 Correct 82 ms 25036 KB Output is correct
11 Correct 5 ms 10476 KB Output is correct
12 Correct 5 ms 10452 KB Output is correct
13 Correct 8 ms 10688 KB Output is correct
14 Correct 119 ms 33984 KB Output is correct
15 Correct 123 ms 36480 KB Output is correct
16 Correct 115 ms 36532 KB Output is correct
17 Correct 122 ms 36428 KB Output is correct
18 Correct 121 ms 36368 KB Output is correct
19 Correct 118 ms 36388 KB Output is correct
20 Correct 121 ms 36424 KB Output is correct
21 Correct 13 ms 11860 KB Output is correct
22 Correct 127 ms 36680 KB Output is correct
23 Correct 122 ms 36736 KB Output is correct
24 Correct 113 ms 36688 KB Output is correct
25 Correct 123 ms 36744 KB Output is correct
26 Correct 113 ms 36768 KB Output is correct
27 Correct 135 ms 36648 KB Output is correct
28 Correct 125 ms 36680 KB Output is correct
29 Correct 114 ms 36712 KB Output is correct
30 Correct 131 ms 36908 KB Output is correct
31 Correct 179 ms 28020 KB Output is correct
32 Correct 117 ms 33984 KB Output is correct
33 Correct 288 ms 31936 KB Output is correct
34 Correct 118 ms 30792 KB Output is correct
35 Correct 229 ms 33788 KB Output is correct
36 Correct 120 ms 30764 KB Output is correct
37 Correct 259 ms 33476 KB Output is correct
38 Correct 200 ms 30472 KB Output is correct
39 Correct 121 ms 36448 KB Output is correct
40 Correct 254 ms 32632 KB Output is correct
41 Correct 6 ms 10612 KB Output is correct
42 Correct 6 ms 10708 KB Output is correct
43 Correct 6 ms 10580 KB Output is correct
44 Correct 6 ms 10580 KB Output is correct
45 Correct 7 ms 10652 KB Output is correct
46 Correct 6 ms 10580 KB Output is correct
47 Correct 6 ms 10672 KB Output is correct
48 Correct 6 ms 10580 KB Output is correct
49 Correct 6 ms 10668 KB Output is correct
50 Correct 6 ms 10708 KB Output is correct
51 Correct 185 ms 30660 KB Output is correct
52 Correct 134 ms 36680 KB Output is correct
53 Correct 287 ms 32972 KB Output is correct
54 Correct 133 ms 30912 KB Output is correct
55 Correct 182 ms 30632 KB Output is correct
56 Correct 140 ms 36744 KB Output is correct
57 Correct 213 ms 33744 KB Output is correct
58 Correct 130 ms 30788 KB Output is correct
59 Correct 192 ms 30624 KB Output is correct
60 Correct 136 ms 36688 KB Output is correct
61 Correct 214 ms 33844 KB Output is correct
62 Correct 128 ms 31064 KB Output is correct
63 Correct 175 ms 30660 KB Output is correct
64 Correct 142 ms 36652 KB Output is correct
65 Correct 288 ms 33628 KB Output is correct
66 Correct 122 ms 30720 KB Output is correct
67 Correct 173 ms 31228 KB Output is correct
68 Correct 127 ms 36624 KB Output is correct
69 Correct 199 ms 32832 KB Output is correct
70 Correct 127 ms 31008 KB Output is correct