답안 #421773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
421773 2021-06-09T11:51:35 Z Hideo Election Campaign (JOI15_election_campaign) C++17
100 / 100
188 ms 32332 KB
#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair

const int N = 1e5 + 7;
const int INF = 1e9 + 7;

ll dp[N][2];

int tin[N], tout[N], t;
int x[N], y[N], z[N];
int p[N][20];
int n, m;

int gr[N], gr_p[N], gr_l[N];
ll gr_s[N], s[N];

vector < int > g[N], q[N];

void prec (int v, int pr = 0){
    tin[v] = ++t;
    p[v][0] = pr;
    for (int j = 1; j < 20; j++)
        p[v][j] = p[p[v][j - 1]][j - 1];
    for (int to : g[v])
        if (to != pr)
            prec(to, v);
    tout[v] = t;
}

bool check (int v, int u){
    return tin[v] <= tin[u] && tout[u] <= tout[v];
}

int lca (int v, int u){
    if (check(v, u))
        return v;
    if (check(u, v))
        return u;
    for (int j = 19; j >= 0; j--)
        if (p[v][j] && !check(p[v][j], u))
            v = p[v][j];
    return p[v][0];
}

int cnt = 0;

ll get (int v, int pr){
    ll res = 0;
    while (v != pr){
        res += gr_s[gr[v]] - s[v];
        v = gr_p[gr[v]];
    }
    return res;
}

void dfs (int v){
    int mx = 0;
    for (int to : g[v]){
        if (to != p[v][0]){
            dfs(to);
            dp[v][1] += dp[to][0];
            if (mx < gr_l[gr[to]]){
                gr[v] = gr[to];
                mx = gr_l[gr[v]];
            }
        }
    }
    dp[v][0] = dp[v][1];
    for (int it : q[v]){
        dp[v][0] = max(dp[v][0], get(x[it], v) + get(y[it], v) + dp[v][1] + z[it]);
    }
    dp[v][1] -= dp[v][0];
    if (!gr[v])
        gr[v] = ++cnt;
    s[v] = gr_s[gr[v]];
    gr_s[gr[v]] += dp[v][1];
    gr_p[gr[v]] = p[v][0];
    gr_l[gr[v]]++;
}

main (){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++){
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    prec(1);
    cin >> m;
    for (int i = 1; i <= m; i++){
        cin >> x[i] >> y[i] >> z[i];
        int l = lca(x[i], y[i]);
        q[l].pb(i);
    }
    dfs(1);
    cout << dp[1][0];
}

Compilation message

election_campaign.cpp:88:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   88 | main (){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 99 ms 20384 KB Output is correct
6 Correct 57 ms 27224 KB Output is correct
7 Correct 99 ms 24968 KB Output is correct
8 Correct 76 ms 21564 KB Output is correct
9 Correct 97 ms 23392 KB Output is correct
10 Correct 82 ms 21620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 99 ms 29512 KB Output is correct
5 Correct 97 ms 29536 KB Output is correct
6 Correct 96 ms 29604 KB Output is correct
7 Correct 100 ms 29532 KB Output is correct
8 Correct 99 ms 29568 KB Output is correct
9 Correct 96 ms 29624 KB Output is correct
10 Correct 96 ms 29492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 99 ms 29512 KB Output is correct
5 Correct 97 ms 29536 KB Output is correct
6 Correct 96 ms 29604 KB Output is correct
7 Correct 100 ms 29532 KB Output is correct
8 Correct 99 ms 29568 KB Output is correct
9 Correct 96 ms 29624 KB Output is correct
10 Correct 96 ms 29492 KB Output is correct
11 Correct 11 ms 5708 KB Output is correct
12 Correct 100 ms 29568 KB Output is correct
13 Correct 108 ms 29472 KB Output is correct
14 Correct 99 ms 29552 KB Output is correct
15 Correct 103 ms 29472 KB Output is correct
16 Correct 101 ms 29532 KB Output is correct
17 Correct 103 ms 29508 KB Output is correct
18 Correct 112 ms 29532 KB Output is correct
19 Correct 99 ms 29528 KB Output is correct
20 Correct 101 ms 29504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 22100 KB Output is correct
2 Correct 96 ms 29508 KB Output is correct
3 Correct 175 ms 26844 KB Output is correct
4 Correct 118 ms 25660 KB Output is correct
5 Correct 155 ms 28996 KB Output is correct
6 Correct 116 ms 25720 KB Output is correct
7 Correct 185 ms 28716 KB Output is correct
8 Correct 149 ms 24952 KB Output is correct
9 Correct 98 ms 31952 KB Output is correct
10 Correct 180 ms 27704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 99 ms 20384 KB Output is correct
6 Correct 57 ms 27224 KB Output is correct
7 Correct 99 ms 24968 KB Output is correct
8 Correct 76 ms 21564 KB Output is correct
9 Correct 97 ms 23392 KB Output is correct
10 Correct 82 ms 21620 KB Output is correct
11 Correct 4 ms 5188 KB Output is correct
12 Correct 4 ms 5232 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 5 ms 5196 KB Output is correct
15 Correct 4 ms 5196 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5196 KB Output is correct
18 Correct 5 ms 5196 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 4 ms 5196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5068 KB Output is correct
3 Correct 3 ms 5068 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 99 ms 20384 KB Output is correct
6 Correct 57 ms 27224 KB Output is correct
7 Correct 99 ms 24968 KB Output is correct
8 Correct 76 ms 21564 KB Output is correct
9 Correct 97 ms 23392 KB Output is correct
10 Correct 82 ms 21620 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 99 ms 29512 KB Output is correct
15 Correct 97 ms 29536 KB Output is correct
16 Correct 96 ms 29604 KB Output is correct
17 Correct 100 ms 29532 KB Output is correct
18 Correct 99 ms 29568 KB Output is correct
19 Correct 96 ms 29624 KB Output is correct
20 Correct 96 ms 29492 KB Output is correct
21 Correct 11 ms 5708 KB Output is correct
22 Correct 100 ms 29568 KB Output is correct
23 Correct 108 ms 29472 KB Output is correct
24 Correct 99 ms 29552 KB Output is correct
25 Correct 103 ms 29472 KB Output is correct
26 Correct 101 ms 29532 KB Output is correct
27 Correct 103 ms 29508 KB Output is correct
28 Correct 112 ms 29532 KB Output is correct
29 Correct 99 ms 29528 KB Output is correct
30 Correct 101 ms 29504 KB Output is correct
31 Correct 147 ms 22100 KB Output is correct
32 Correct 96 ms 29508 KB Output is correct
33 Correct 175 ms 26844 KB Output is correct
34 Correct 118 ms 25660 KB Output is correct
35 Correct 155 ms 28996 KB Output is correct
36 Correct 116 ms 25720 KB Output is correct
37 Correct 185 ms 28716 KB Output is correct
38 Correct 149 ms 24952 KB Output is correct
39 Correct 98 ms 31952 KB Output is correct
40 Correct 180 ms 27704 KB Output is correct
41 Correct 4 ms 5188 KB Output is correct
42 Correct 4 ms 5232 KB Output is correct
43 Correct 4 ms 5196 KB Output is correct
44 Correct 5 ms 5196 KB Output is correct
45 Correct 4 ms 5196 KB Output is correct
46 Correct 4 ms 5204 KB Output is correct
47 Correct 4 ms 5196 KB Output is correct
48 Correct 5 ms 5196 KB Output is correct
49 Correct 4 ms 5196 KB Output is correct
50 Correct 4 ms 5196 KB Output is correct
51 Correct 166 ms 25268 KB Output is correct
52 Correct 103 ms 32216 KB Output is correct
53 Correct 177 ms 28080 KB Output is correct
54 Correct 118 ms 25924 KB Output is correct
55 Correct 155 ms 24848 KB Output is correct
56 Correct 102 ms 32276 KB Output is correct
57 Correct 163 ms 28864 KB Output is correct
58 Correct 119 ms 25940 KB Output is correct
59 Correct 158 ms 25264 KB Output is correct
60 Correct 104 ms 32288 KB Output is correct
61 Correct 173 ms 28836 KB Output is correct
62 Correct 124 ms 25908 KB Output is correct
63 Correct 157 ms 24860 KB Output is correct
64 Correct 101 ms 32224 KB Output is correct
65 Correct 188 ms 28736 KB Output is correct
66 Correct 118 ms 26000 KB Output is correct
67 Correct 156 ms 24984 KB Output is correct
68 Correct 106 ms 32332 KB Output is correct
69 Correct 164 ms 27756 KB Output is correct
70 Correct 130 ms 26052 KB Output is correct