답안 #347783

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
347783 2021-01-13T12:36:22 Z Blerargh Election Campaign (JOI15_election_campaign) C++17
100 / 100
574 ms 45292 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define _ <<" "<<
const ll MAXN = 1e5+5;
const ll INF = 1e18;

vector<ll> adjlist[MAXN];
bool visited[MAXN];

//lca 
ll parent[18][MAXN], depth[MAXN];
ll n;

//hld
ll stsize[MAXN], preorder[MAXN], head[MAXN], id;

void hld_sz(ll u, ll par){
    stsize[u] = 1;
    for (auto &v : adjlist[u]){
        if (v != par) {
            hld_sz(v, u);
            stsize[u] += stsize[v];

            if (adjlist[u][0] == par || stsize[v] > stsize[adjlist[u][0]]) swap(v, adjlist[u][0]);
        }
    }
    return;
}

void hld(ll u, ll par){
    preorder[u] = ++id;
    if (u == par) head[u] = u;
    for (auto v : adjlist[u]){
        if (v != par){
            head[v] = (v == adjlist[u][0]) ? head[u] : v;
            hld(v, u);
        }
    }
    return;
}

void dfs(ll u, ll par, ll dep){
    visited[u] = 1;
    depth[u] = dep;
    parent[0][u] = par;
    for (auto v : adjlist[u]){
        if (!visited[v]) dfs(v, u, dep+1);
    }
    return;
}

void lcainit(){
    for (int power=1; power<18; power++) for (int i=1; i<=n; i++){
        parent[power][i] = parent[power-1][parent[power-1][i]];
    }
    return;
}

ll lca(ll a, ll b){
    if (depth[a] > depth[b]) swap(a,b);

    ll diff = depth[b] - depth[a];
    ll power = 0;
    while (diff){
        if (diff%2) b = parent[power][b];

        diff>>=1;
        power++;
    }

    if (a == b) return a;

    for (int i=17; i>=0; i--){
        if (parent[i][a] != parent[i][b]){
            a = parent[i][a];
            b = parent[i][b];
        }
    }
    return parent[0][a];
}

ll nodesum[MAXN], childsum[MAXN];

void upd(ll x, ll val, ll type){
    if (type) {
        for (int i=x; i<=n; i+=i&(-i)) nodesum[i] += val;
    } else {
        for (int i=x; i<=n; i+=i&(-i)) childsum[i] += val;
    }
    return;
}

ll query(ll a, ll b, ll type){
    ll resa=0, resb=0;
    if (type) {
        for (int i=--a; i; i-=i&(-i)) resa += nodesum[i];
        for (int i=b; i; i-=i&(-i)) resb += nodesum[i];
    } else {
        for (int i=--a; i; i-=i&(-i)) resa += childsum[i];
        for (int i=b; i; i-=i&(-i)) resb += childsum[i];
    }
    return resb-resa;
}

ll dp[MAXN];
bool visitd[MAXN];
vector<tuple<ll,ll,ll> > lcas[MAXN];

void solve(ll u){
    visitd[u] = 1;
    for (auto v : adjlist[u]){
        if (visitd[v]) continue;
        solve(v);
        dp[u] += dp[v];
    }

    for (auto paths : lcas[u]){
        ll x, y, val;
        ll nsum = 0, csum = 0;
        tie(x, y, val) = paths;
        // cout << x _ y _ val << "gay ";

        while (true){
            if (head[x] == head[y]){
                if (preorder[x] > preorder[y]) swap(x, y);
                nsum += query(preorder[x], preorder[y], 1);
                csum += query(preorder[x], preorder[y], 0);
                break;
            } else {
                if (depth[head[x]] >= depth[head[y]]) {
                    nsum += query(preorder[head[x]], preorder[x], 1);
                    csum += query(preorder[head[x]], preorder[x], 0);
                    x = parent[0][head[x]];
                } else {
                    nsum += query(preorder[head[y]], preorder[y], 1);
                    csum += query(preorder[head[y]], preorder[y], 0);
                    y = parent[0][head[y]];
                }
            }
        }

        // cout << csum _ nsum _ val << "vals ";

        ll newval = csum - nsum + val;
        dp[u] = max(dp[u], newval);
    }

    upd(preorder[u], dp[u], 1);
    upd(preorder[parent[0][u]], dp[u], 0);

    // cout << u _ dp[u] << '\n';
    return;
}

int main(){
    cin >> n;
    for (int i=1; i<n; i++){
        ll a, b;
        cin >> a >> b;
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
    }

    dfs(1,1,0);
    lcainit();

    hld_sz(1,1);
    hld(1,1);

    ll q;
    cin >> q;

    while (q--) {
        ll a, b, c;
        cin >> a >> b >> c;
        lcas[lca(a,b)].emplace_back(a,b,c);
    }

    solve(1);
    cout << dp[1];

    // for (int i=1; i<11; i++) cout << stsize[i] _ preorder[i] _ head[i] << '\n';
    // cout << lca(2, 7) _ lca(3, 5) _ lca(1, 1) _ lca(6, 7);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5228 KB Output is correct
4 Correct 4 ms 5376 KB Output is correct
5 Correct 168 ms 29932 KB Output is correct
6 Correct 102 ms 40044 KB Output is correct
7 Correct 132 ms 36588 KB Output is correct
8 Correct 126 ms 28524 KB Output is correct
9 Correct 131 ms 34412 KB Output is correct
10 Correct 120 ms 28600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5248 KB Output is correct
3 Correct 5 ms 5484 KB Output is correct
4 Correct 239 ms 44928 KB Output is correct
5 Correct 249 ms 44984 KB Output is correct
6 Correct 217 ms 44908 KB Output is correct
7 Correct 247 ms 44908 KB Output is correct
8 Correct 239 ms 44908 KB Output is correct
9 Correct 217 ms 44908 KB Output is correct
10 Correct 245 ms 44908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5100 KB Output is correct
2 Correct 3 ms 5248 KB Output is correct
3 Correct 5 ms 5484 KB Output is correct
4 Correct 239 ms 44928 KB Output is correct
5 Correct 249 ms 44984 KB Output is correct
6 Correct 217 ms 44908 KB Output is correct
7 Correct 247 ms 44908 KB Output is correct
8 Correct 239 ms 44908 KB Output is correct
9 Correct 217 ms 44908 KB Output is correct
10 Correct 245 ms 44908 KB Output is correct
11 Correct 27 ms 6764 KB Output is correct
12 Correct 259 ms 45164 KB Output is correct
13 Correct 266 ms 45164 KB Output is correct
14 Correct 271 ms 45292 KB Output is correct
15 Correct 253 ms 45164 KB Output is correct
16 Correct 235 ms 45292 KB Output is correct
17 Correct 255 ms 45292 KB Output is correct
18 Correct 256 ms 45164 KB Output is correct
19 Correct 231 ms 45164 KB Output is correct
20 Correct 249 ms 45164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 574 ms 34256 KB Output is correct
2 Correct 217 ms 44908 KB Output is correct
3 Correct 430 ms 41000 KB Output is correct
4 Correct 342 ms 33148 KB Output is correct
5 Correct 331 ms 40232 KB Output is correct
6 Correct 312 ms 32856 KB Output is correct
7 Correct 424 ms 39948 KB Output is correct
8 Correct 393 ms 34156 KB Output is correct
9 Correct 228 ms 45036 KB Output is correct
10 Correct 449 ms 38432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5228 KB Output is correct
4 Correct 4 ms 5376 KB Output is correct
5 Correct 168 ms 29932 KB Output is correct
6 Correct 102 ms 40044 KB Output is correct
7 Correct 132 ms 36588 KB Output is correct
8 Correct 126 ms 28524 KB Output is correct
9 Correct 131 ms 34412 KB Output is correct
10 Correct 120 ms 28600 KB Output is correct
11 Correct 5 ms 5484 KB Output is correct
12 Correct 5 ms 5484 KB Output is correct
13 Correct 5 ms 5484 KB Output is correct
14 Correct 6 ms 5484 KB Output is correct
15 Correct 6 ms 5484 KB Output is correct
16 Correct 5 ms 5484 KB Output is correct
17 Correct 5 ms 5484 KB Output is correct
18 Correct 6 ms 5484 KB Output is correct
19 Correct 5 ms 5484 KB Output is correct
20 Correct 5 ms 5484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 3 ms 5100 KB Output is correct
3 Correct 3 ms 5228 KB Output is correct
4 Correct 4 ms 5376 KB Output is correct
5 Correct 168 ms 29932 KB Output is correct
6 Correct 102 ms 40044 KB Output is correct
7 Correct 132 ms 36588 KB Output is correct
8 Correct 126 ms 28524 KB Output is correct
9 Correct 131 ms 34412 KB Output is correct
10 Correct 120 ms 28600 KB Output is correct
11 Correct 3 ms 5100 KB Output is correct
12 Correct 3 ms 5248 KB Output is correct
13 Correct 5 ms 5484 KB Output is correct
14 Correct 239 ms 44928 KB Output is correct
15 Correct 249 ms 44984 KB Output is correct
16 Correct 217 ms 44908 KB Output is correct
17 Correct 247 ms 44908 KB Output is correct
18 Correct 239 ms 44908 KB Output is correct
19 Correct 217 ms 44908 KB Output is correct
20 Correct 245 ms 44908 KB Output is correct
21 Correct 27 ms 6764 KB Output is correct
22 Correct 259 ms 45164 KB Output is correct
23 Correct 266 ms 45164 KB Output is correct
24 Correct 271 ms 45292 KB Output is correct
25 Correct 253 ms 45164 KB Output is correct
26 Correct 235 ms 45292 KB Output is correct
27 Correct 255 ms 45292 KB Output is correct
28 Correct 256 ms 45164 KB Output is correct
29 Correct 231 ms 45164 KB Output is correct
30 Correct 249 ms 45164 KB Output is correct
31 Correct 574 ms 34256 KB Output is correct
32 Correct 217 ms 44908 KB Output is correct
33 Correct 430 ms 41000 KB Output is correct
34 Correct 342 ms 33148 KB Output is correct
35 Correct 331 ms 40232 KB Output is correct
36 Correct 312 ms 32856 KB Output is correct
37 Correct 424 ms 39948 KB Output is correct
38 Correct 393 ms 34156 KB Output is correct
39 Correct 228 ms 45036 KB Output is correct
40 Correct 449 ms 38432 KB Output is correct
41 Correct 5 ms 5484 KB Output is correct
42 Correct 5 ms 5484 KB Output is correct
43 Correct 5 ms 5484 KB Output is correct
44 Correct 6 ms 5484 KB Output is correct
45 Correct 6 ms 5484 KB Output is correct
46 Correct 5 ms 5484 KB Output is correct
47 Correct 5 ms 5484 KB Output is correct
48 Correct 6 ms 5484 KB Output is correct
49 Correct 5 ms 5484 KB Output is correct
50 Correct 5 ms 5484 KB Output is correct
51 Correct 388 ms 34472 KB Output is correct
52 Correct 264 ms 45292 KB Output is correct
53 Correct 450 ms 38944 KB Output is correct
54 Correct 286 ms 33212 KB Output is correct
55 Correct 511 ms 34404 KB Output is correct
56 Correct 252 ms 45164 KB Output is correct
57 Correct 338 ms 39720 KB Output is correct
58 Correct 322 ms 33184 KB Output is correct
59 Correct 390 ms 34404 KB Output is correct
60 Correct 249 ms 45164 KB Output is correct
61 Correct 351 ms 39976 KB Output is correct
62 Correct 356 ms 32836 KB Output is correct
63 Correct 520 ms 34228 KB Output is correct
64 Correct 265 ms 45200 KB Output is correct
65 Correct 441 ms 39840 KB Output is correct
66 Correct 277 ms 33052 KB Output is correct
67 Correct 523 ms 34280 KB Output is correct
68 Correct 255 ms 45164 KB Output is correct
69 Correct 362 ms 38252 KB Output is correct
70 Correct 328 ms 33172 KB Output is correct