Submission #482904

# Submission time Handle Problem Language Result Execution time Memory
482904 2021-10-26T17:14:30 Z K4YAN Election Campaign (JOI15_election_campaign) C++17
100 / 100
317 ms 43872 KB
//Be Name Khoda

#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast,unroll-loops")
#pragma GCC target ("avx2")

using namespace std;

#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define piii pair<pii, int>
#define piiii pair<pii, pii>
#define piiiii pair<pii, piii>
#define pll pair<ll, ll>
#define plll pair<pll, ll>

const ll mxn = 1e5 + 16;
ll n, k, m, q, w;
ll dp[mxn][2], par[mxn][17], hch[mxn], chn[mxn], lbl[mxn], dis[mxn], subt[mxn];
vector<int> adj[mxn];
vector<plll> g[mxn];
bool mark[mxn];
plll p;

void DFS(int v) {
    mark[v] = 1;
    subt[v] = 1, dis[v] = w++;
    for(auto u : adj[v]) {
        if(!mark[u]) {
            par[u][0] = v, DFS(u);
            subt[v] += subt[u];
            if(subt[hch[v]] < subt[u]) hch[v] = u;
        }
    }w--;
}
void SAGZAN(int v) {
    lbl[v] = w++;
    if(chn[v] == -1) chn[v] = v;
    if(hch[v] != -1) {
        chn[hch[v]] = chn[v];
        SAGZAN(hch[v]);
    }
    for(auto u : adj[v]) {
        if(u == par[v][0] || u == hch[v]) continue;
        SAGZAN(u);
    }
}
ll find_jad(int v, int u) {
    if(dis[v] > dis[u]) {
        swap(v, u);
    }
    for(int i = 16; i > -1; i--) {
        if(dis[u] - (1 << i) < dis[v]) continue;
        u = par[u][i];
    }
    if(u == v) return v;
    for(int i = 16; i > -1; i--) {
        if(par[v][i] == par[u][i]) continue;
        v = par[v][i], u = par[u][i];
    }
    v = par[v][0];
    return v;
}
struct segtree {

    int s = 1;
    vector<int> v;
    void init(ll n) {
        while(s < n) s <<= 1;
        v.assign(2 * s, 0);
    }
    void add(int i, ll k, int x, int lx, int rx) {
        if(rx - lx == 1) {
            v[x] = k;
            return;
        }
        int m = (lx + rx) >> 1;
        if(i < m) add(i, k, 2 * x + 1, lx, m);
        else add(i, k, 2 * x + 2, m, rx);
        v[x] = v[2 * x + 1] + v[2 * x + 2];
    }
    void add(int i, ll k) {
        add(i, k, 0, 0, s);
    }
    ll cal(int l, int r, int x, int lx, int rx) {
        if(rx <= l || r <= lx) return 0;
        if(l <= lx && rx <= r) return v[x];
        int m = (lx + rx) >> 1;
        ll ans1 = cal(l, r, 2 * x + 1, lx, m);
        ll ans2 = cal(l, r, 2 * x + 2, m, rx);
        return ans1 + ans2;
    }
    ll cal(int l, int r) {
        return cal(l, r, 0, 0, s);
    }

};
segtree st;

ll cal(int v, int u, int l) {
    ll r = v, res = 0, t;
    while(r > 1) {
        t = chn[r];
        if(dis[l] >= dis[t]) {
            res = res + st.cal(lbl[r] - dis[r] + dis[l] + 1, lbl[r] + 1);
            break;
        }
        res = res + st.cal(lbl[chn[r]], lbl[r] + 1);
        r = par[chn[r]][0];
    }r = u;
    while(r > 1) {
        t = chn[r];
        if(dis[l] >= dis[t]) {
            res = res + st.cal(lbl[r] - dis[r] + dis[l] + 1, lbl[r] + 1);
            break;
        }
        res = res + st.cal(lbl[chn[r]], lbl[r] + 1);
        r = par[chn[r]][0];
    }
    res += dp[l][1];
    return res;
}

void SAGZAN_Solve(int v) {

    for(auto u : adj[v]) {
        if(u == par[v][0]) continue;
        SAGZAN_Solve(u);
        dp[v][1] += dp[u][0];
    }
    for(auto u : g[v]) {
        dp[v][0] = max(dp[v][0], u.second + cal(u.first.first, u.first.second, v));
    }
    dp[v][0] = max(dp[v][1], dp[v][0]);
    st.add(lbl[v], dp[v][1] - dp[v][0]);

}

void input() {

    fill(hch, hch + mxn, -1), fill(chn, chn + mxn, -1);
    cin >> n;
    for(int i = 1; i < n; i++) {
        int v, u;
        cin >> v >> u;
        adj[v].push_back(u), adj[u].push_back(v);
    }DFS(1), SAGZAN(1);
    for(int j = 1; j < 17; j++) {
        for(int i = 1; i <= n; i++) {
            par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }
    cin >> m;
    for(int i = 0; i < m; i++) {
        int v, u, c, w;
        cin >> v >> u >> c;
        w = find_jad(v, u);
        g[w].push_back({{v, u}, c});
    }

}

void solve() {

    st.init(n);
    SAGZAN_Solve(1);
    cout << dp[1][0] << endl;

}

int main() {

    ios::sync_with_stdio(false); cin.tie(NULL);

    input(), solve();

    return 0;

}
/*
20
17 10
11 4
8 3
3 16
1 14
15 18
5 4
6 18
10 18
19 4
16 7
2 13
4 12
12 20
9 20
18 13
20 14
14 7
13 7
15
19 9 2341
13 8 6974
8 3 3339
15 17 6515
10 13 4370
1 7 8376
18 2 9272
6 7 4595
1 20 505
10 9 308
6 19 8937
2 15 5072
5 4 4217
2 4 4170
19 12 8204
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 5 ms 6604 KB Output is correct
3 Correct 5 ms 6604 KB Output is correct
4 Correct 5 ms 6732 KB Output is correct
5 Correct 105 ms 29316 KB Output is correct
6 Correct 63 ms 38584 KB Output is correct
7 Correct 95 ms 35348 KB Output is correct
8 Correct 80 ms 29744 KB Output is correct
9 Correct 120 ms 33396 KB Output is correct
10 Correct 83 ms 29680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6604 KB Output is correct
2 Correct 5 ms 6560 KB Output is correct
3 Correct 6 ms 6868 KB Output is correct
4 Correct 152 ms 43412 KB Output is correct
5 Correct 180 ms 43440 KB Output is correct
6 Correct 126 ms 43368 KB Output is correct
7 Correct 147 ms 43400 KB Output is correct
8 Correct 223 ms 43360 KB Output is correct
9 Correct 149 ms 43664 KB Output is correct
10 Correct 159 ms 43368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6604 KB Output is correct
2 Correct 5 ms 6560 KB Output is correct
3 Correct 6 ms 6868 KB Output is correct
4 Correct 152 ms 43412 KB Output is correct
5 Correct 180 ms 43440 KB Output is correct
6 Correct 126 ms 43368 KB Output is correct
7 Correct 147 ms 43400 KB Output is correct
8 Correct 223 ms 43360 KB Output is correct
9 Correct 149 ms 43664 KB Output is correct
10 Correct 159 ms 43368 KB Output is correct
11 Correct 17 ms 8176 KB Output is correct
12 Correct 231 ms 43632 KB Output is correct
13 Correct 167 ms 43716 KB Output is correct
14 Correct 137 ms 43716 KB Output is correct
15 Correct 160 ms 43716 KB Output is correct
16 Correct 133 ms 43724 KB Output is correct
17 Correct 167 ms 43716 KB Output is correct
18 Correct 157 ms 43632 KB Output is correct
19 Correct 149 ms 43840 KB Output is correct
20 Correct 156 ms 43724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 317 ms 33612 KB Output is correct
2 Correct 128 ms 43464 KB Output is correct
3 Correct 274 ms 39644 KB Output is correct
4 Correct 192 ms 34144 KB Output is correct
5 Correct 204 ms 39016 KB Output is correct
6 Correct 170 ms 33952 KB Output is correct
7 Correct 279 ms 38664 KB Output is correct
8 Correct 215 ms 33800 KB Output is correct
9 Correct 127 ms 43364 KB Output is correct
10 Correct 262 ms 37480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 5 ms 6604 KB Output is correct
3 Correct 5 ms 6604 KB Output is correct
4 Correct 5 ms 6732 KB Output is correct
5 Correct 105 ms 29316 KB Output is correct
6 Correct 63 ms 38584 KB Output is correct
7 Correct 95 ms 35348 KB Output is correct
8 Correct 80 ms 29744 KB Output is correct
9 Correct 120 ms 33396 KB Output is correct
10 Correct 83 ms 29680 KB Output is correct
11 Correct 6 ms 6860 KB Output is correct
12 Correct 6 ms 6860 KB Output is correct
13 Correct 5 ms 6860 KB Output is correct
14 Correct 7 ms 6804 KB Output is correct
15 Correct 6 ms 6872 KB Output is correct
16 Correct 5 ms 6860 KB Output is correct
17 Correct 6 ms 6872 KB Output is correct
18 Correct 6 ms 6860 KB Output is correct
19 Correct 6 ms 6860 KB Output is correct
20 Correct 6 ms 6860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 5 ms 6604 KB Output is correct
3 Correct 5 ms 6604 KB Output is correct
4 Correct 5 ms 6732 KB Output is correct
5 Correct 105 ms 29316 KB Output is correct
6 Correct 63 ms 38584 KB Output is correct
7 Correct 95 ms 35348 KB Output is correct
8 Correct 80 ms 29744 KB Output is correct
9 Correct 120 ms 33396 KB Output is correct
10 Correct 83 ms 29680 KB Output is correct
11 Correct 5 ms 6604 KB Output is correct
12 Correct 5 ms 6560 KB Output is correct
13 Correct 6 ms 6868 KB Output is correct
14 Correct 152 ms 43412 KB Output is correct
15 Correct 180 ms 43440 KB Output is correct
16 Correct 126 ms 43368 KB Output is correct
17 Correct 147 ms 43400 KB Output is correct
18 Correct 223 ms 43360 KB Output is correct
19 Correct 149 ms 43664 KB Output is correct
20 Correct 159 ms 43368 KB Output is correct
21 Correct 17 ms 8176 KB Output is correct
22 Correct 231 ms 43632 KB Output is correct
23 Correct 167 ms 43716 KB Output is correct
24 Correct 137 ms 43716 KB Output is correct
25 Correct 160 ms 43716 KB Output is correct
26 Correct 133 ms 43724 KB Output is correct
27 Correct 167 ms 43716 KB Output is correct
28 Correct 157 ms 43632 KB Output is correct
29 Correct 149 ms 43840 KB Output is correct
30 Correct 156 ms 43724 KB Output is correct
31 Correct 317 ms 33612 KB Output is correct
32 Correct 128 ms 43464 KB Output is correct
33 Correct 274 ms 39644 KB Output is correct
34 Correct 192 ms 34144 KB Output is correct
35 Correct 204 ms 39016 KB Output is correct
36 Correct 170 ms 33952 KB Output is correct
37 Correct 279 ms 38664 KB Output is correct
38 Correct 215 ms 33800 KB Output is correct
39 Correct 127 ms 43364 KB Output is correct
40 Correct 262 ms 37480 KB Output is correct
41 Correct 6 ms 6860 KB Output is correct
42 Correct 6 ms 6860 KB Output is correct
43 Correct 5 ms 6860 KB Output is correct
44 Correct 7 ms 6804 KB Output is correct
45 Correct 6 ms 6872 KB Output is correct
46 Correct 5 ms 6860 KB Output is correct
47 Correct 6 ms 6872 KB Output is correct
48 Correct 6 ms 6860 KB Output is correct
49 Correct 6 ms 6860 KB Output is correct
50 Correct 6 ms 6860 KB Output is correct
51 Correct 221 ms 34080 KB Output is correct
52 Correct 153 ms 43592 KB Output is correct
53 Correct 261 ms 37884 KB Output is correct
54 Correct 157 ms 34364 KB Output is correct
55 Correct 284 ms 33732 KB Output is correct
56 Correct 152 ms 43660 KB Output is correct
57 Correct 249 ms 38752 KB Output is correct
58 Correct 181 ms 34184 KB Output is correct
59 Correct 223 ms 34008 KB Output is correct
60 Correct 150 ms 43716 KB Output is correct
61 Correct 224 ms 38780 KB Output is correct
62 Correct 166 ms 33816 KB Output is correct
63 Correct 277 ms 33496 KB Output is correct
64 Correct 153 ms 43872 KB Output is correct
65 Correct 261 ms 38680 KB Output is correct
66 Correct 171 ms 34132 KB Output is correct
67 Correct 282 ms 33836 KB Output is correct
68 Correct 156 ms 43716 KB Output is correct
69 Correct 201 ms 37308 KB Output is correct
70 Correct 172 ms 34252 KB Output is correct