답안 #554730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554730 2022-04-29T10:00:15 Z Jarif_Rahman Election Campaign (JOI15_election_campaign) C++17
20 / 100
1000 ms 30372 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int K = 19;

int n, m;
vector<vector<int>> v;
vector<int> anc[K];
vector<int> depth;

int get_anc(int nd, int h){
    for(int i = 0; i < K; i++) { if(h%2 == 1) nd = anc[i][nd]; h/=2; } return nd;
}
int lca_jump(int a, int b){
    if(a == b) return a;
    for(int i = K-1; i >= 0; i--) if(anc[i][a] != anc[i][b])
        return lca_jump(anc[i][a], anc[i][b]);
    return anc[0][a];
}
int lca(int a, int b){
    if(depth[a] > depth[b]) swap(a, b);
    b = get_anc(b, depth[b]-depth[a]);
    return lca_jump(a, b);
}

void pre_dfs(int nd, int ss, int d){
    for(int x: v[nd]) if(x != ss) pre_dfs(x, nd, d+1);
    if(ss != -1) anc[0][nd] = ss;
    depth[nd] = d;
}

vector<vector<tuple<int, int, ll>>> plans;
vector<ll> dp, childsum;

void dfs(int nd, int ss){
    for(int x: v[nd]) if(x != ss) dfs(x, nd);
    for(int x: v[nd]) if(x != ss) childsum[nd]+=dp[x];
    dp[nd] = childsum[nd];

    for(auto [a, b, c]: plans[nd]){
        ll cur = c;

        int _a = a, _b = b;
        while(_a != nd) cur+=childsum[_a], _a = anc[0][_a];
        while(_b != nd) cur+=childsum[_b], _b = anc[0][_b];
        cur+=childsum[nd];

        _a = a, _b = b;
        while(_a != nd) cur-=dp[_a], _a = anc[0][_a];
        while(_b != nd) cur-=dp[_b], _b = anc[0][_b];

        dp[nd] = max(dp[nd], cur);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;
    v.assign(n, {});
    fill(anc, anc+K, vector<int>(n, 0));
    depth.assign(n, -1);
    plans.assign(n, {});
    dp.assign(n, 0);
    childsum.assign(n, 0);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);
    }

    pre_dfs(0, -1, 0);
    for(int p = 1; p < K; p++) for(int i = 0; i < n; i++)
        anc[p][i] = anc[p-1][anc[p-1][i]];

    cin >> m;

    for(int i = 0; i < m; i++){
        int a, b; ll c; cin >> a >> b >> c; a--, b--;
        plans[lca(a, b)].pb({a, b, c});
    }

    dfs(0, -1);

    cout << dp[0] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 448 KB Output is correct
5 Correct 59 ms 18884 KB Output is correct
6 Correct 38 ms 26436 KB Output is correct
7 Correct 61 ms 23824 KB Output is correct
8 Correct 49 ms 19152 KB Output is correct
9 Correct 69 ms 22300 KB Output is correct
10 Correct 47 ms 19176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Execution timed out 1081 ms 30332 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Execution timed out 1081 ms 30332 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 22080 KB Output is correct
2 Execution timed out 1073 ms 30372 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 448 KB Output is correct
5 Correct 59 ms 18884 KB Output is correct
6 Correct 38 ms 26436 KB Output is correct
7 Correct 61 ms 23824 KB Output is correct
8 Correct 49 ms 19152 KB Output is correct
9 Correct 69 ms 22300 KB Output is correct
10 Correct 47 ms 19176 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 592 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 528 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 464 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 2 ms 468 KB Output is correct
19 Correct 1 ms 456 KB Output is correct
20 Correct 4 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 448 KB Output is correct
5 Correct 59 ms 18884 KB Output is correct
6 Correct 38 ms 26436 KB Output is correct
7 Correct 61 ms 23824 KB Output is correct
8 Correct 49 ms 19152 KB Output is correct
9 Correct 69 ms 22300 KB Output is correct
10 Correct 47 ms 19176 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Execution timed out 1081 ms 30332 KB Time limit exceeded
15 Halted 0 ms 0 KB -