답안 #910945

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
910945 2024-01-18T10:04:10 Z LOLOLO Election Campaign (JOI15_election_campaign) C++17
100 / 100
213 ms 39488 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 1e5 + 100;

vector <int> ed[N];
int in[N], ou[N], sp[N][20], timer = 0;

void dfs(int u, int v) {
    if (u != 1) {
        auto it = find(ed[u].begin(), ed[u].end(), v);
        if (it != end(ed[u]))
            ed[u].erase(it);
    }

    sp[u][0] = v;
    for (int i = 1; i < 20; i++)
        sp[u][i] = sp[sp[u][i - 1]][i - 1];

    timer++;
    in[u] = timer;

    for (auto x : ed[u]) {
        dfs(x, u);
    }
    ou[u] = timer;
}

bool is(int a, int b) {
    return in[a] <= in[b] && ou[a] >= ou[b];
}

int anc(int a, int b) {
    if (is(a, b))
        return a;

    if (is(b, a))
        return b;

    for (int i = 19; i >= 0; i--) {
        if (is(sp[a][i], b) == 0)
            a = sp[a][i];
    }

    return sp[a][0];
}

ll fen[N];
void upd(int u, ll x) {
    for (; u < N; u += u & (-u))
        fen[u] += x;
}

ll get(int u) {
    ll s = 0;
    for (; u; u -= u & (-u))
        s += fen[u];

    return s;
}

vector < vector <ll>> save[N];
ll f[N], s[N];

ll get(int u, int c) {
    if (u == c)
        return 0;

    int l = 0, r = sz(ed[u]) - 1, ans = 0;
    while (l <= r) {
        int m = (l + r) / 2;
        if (in[ed[u][m]] <= in[c]) {
            ans = m;
            l = m + 1;
        } else {
            r = m - 1;
        }
    }

    return f[ed[u][ans]] - s[c];
}

void dfs2(int u) {
    for (auto x : ed[u]) {
        dfs2(x);
        s[u] += f[x];
    }

    f[u] = s[u];

    sort(all(ed[u]), [&](int a, int b) {return in[a] < in[b];});
    for (auto x : save[u]) {
        f[u] = max(f[u], x[2] + get(in[x[0]]) + get(in[x[1]]) - get(u, x[0]) - get(u, x[1]) + s[u]);
    }

    for (auto x : ed[u]) {
        upd(in[x], s[u] - f[x]);
        upd(ou[x] + 1, f[x] - s[u]);
    }
}

ll solve() {
    int n;
    cin >> n;

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

    dfs(1, 1);
    int m;
    cin >> m;

    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        int x = anc(a, b);
        save[x].pb({a, b, c});
    }

    dfs2(1);

    return f[1];
}

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

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

    while (t--) {
        //solve();
        cout << solve() << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Correct 3 ms 9700 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 51 ms 20452 KB Output is correct
6 Correct 58 ms 31320 KB Output is correct
7 Correct 64 ms 27688 KB Output is correct
8 Correct 59 ms 20524 KB Output is correct
9 Correct 76 ms 25160 KB Output is correct
10 Correct 48 ms 20752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9792 KB Output is correct
2 Correct 3 ms 9788 KB Output is correct
3 Correct 3 ms 9804 KB Output is correct
4 Correct 118 ms 38964 KB Output is correct
5 Correct 104 ms 38992 KB Output is correct
6 Correct 116 ms 39148 KB Output is correct
7 Correct 96 ms 38920 KB Output is correct
8 Correct 91 ms 38992 KB Output is correct
9 Correct 115 ms 38948 KB Output is correct
10 Correct 87 ms 38908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9792 KB Output is correct
2 Correct 3 ms 9788 KB Output is correct
3 Correct 3 ms 9804 KB Output is correct
4 Correct 118 ms 38964 KB Output is correct
5 Correct 104 ms 38992 KB Output is correct
6 Correct 116 ms 39148 KB Output is correct
7 Correct 96 ms 38920 KB Output is correct
8 Correct 91 ms 38992 KB Output is correct
9 Correct 115 ms 38948 KB Output is correct
10 Correct 87 ms 38908 KB Output is correct
11 Correct 10 ms 11612 KB Output is correct
12 Correct 89 ms 39160 KB Output is correct
13 Correct 96 ms 39204 KB Output is correct
14 Correct 105 ms 39488 KB Output is correct
15 Correct 87 ms 39440 KB Output is correct
16 Correct 114 ms 39228 KB Output is correct
17 Correct 92 ms 39248 KB Output is correct
18 Correct 108 ms 39248 KB Output is correct
19 Correct 96 ms 39232 KB Output is correct
20 Correct 89 ms 39276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 27404 KB Output is correct
2 Correct 107 ms 39004 KB Output is correct
3 Correct 150 ms 34700 KB Output is correct
4 Correct 132 ms 27396 KB Output is correct
5 Correct 134 ms 33936 KB Output is correct
6 Correct 109 ms 27728 KB Output is correct
7 Correct 201 ms 33800 KB Output is correct
8 Correct 167 ms 27988 KB Output is correct
9 Correct 122 ms 39188 KB Output is correct
10 Correct 160 ms 32264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Correct 3 ms 9700 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 51 ms 20452 KB Output is correct
6 Correct 58 ms 31320 KB Output is correct
7 Correct 64 ms 27688 KB Output is correct
8 Correct 59 ms 20524 KB Output is correct
9 Correct 76 ms 25160 KB Output is correct
10 Correct 48 ms 20752 KB Output is correct
11 Correct 4 ms 9804 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 3 ms 9820 KB Output is correct
14 Correct 4 ms 9820 KB Output is correct
15 Correct 3 ms 9820 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 5 ms 9820 KB Output is correct
18 Correct 5 ms 9820 KB Output is correct
19 Correct 3 ms 9820 KB Output is correct
20 Correct 3 ms 9816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Correct 3 ms 9700 KB Output is correct
4 Correct 3 ms 9820 KB Output is correct
5 Correct 51 ms 20452 KB Output is correct
6 Correct 58 ms 31320 KB Output is correct
7 Correct 64 ms 27688 KB Output is correct
8 Correct 59 ms 20524 KB Output is correct
9 Correct 76 ms 25160 KB Output is correct
10 Correct 48 ms 20752 KB Output is correct
11 Correct 2 ms 9792 KB Output is correct
12 Correct 3 ms 9788 KB Output is correct
13 Correct 3 ms 9804 KB Output is correct
14 Correct 118 ms 38964 KB Output is correct
15 Correct 104 ms 38992 KB Output is correct
16 Correct 116 ms 39148 KB Output is correct
17 Correct 96 ms 38920 KB Output is correct
18 Correct 91 ms 38992 KB Output is correct
19 Correct 115 ms 38948 KB Output is correct
20 Correct 87 ms 38908 KB Output is correct
21 Correct 10 ms 11612 KB Output is correct
22 Correct 89 ms 39160 KB Output is correct
23 Correct 96 ms 39204 KB Output is correct
24 Correct 105 ms 39488 KB Output is correct
25 Correct 87 ms 39440 KB Output is correct
26 Correct 114 ms 39228 KB Output is correct
27 Correct 92 ms 39248 KB Output is correct
28 Correct 108 ms 39248 KB Output is correct
29 Correct 96 ms 39232 KB Output is correct
30 Correct 89 ms 39276 KB Output is correct
31 Correct 124 ms 27404 KB Output is correct
32 Correct 107 ms 39004 KB Output is correct
33 Correct 150 ms 34700 KB Output is correct
34 Correct 132 ms 27396 KB Output is correct
35 Correct 134 ms 33936 KB Output is correct
36 Correct 109 ms 27728 KB Output is correct
37 Correct 201 ms 33800 KB Output is correct
38 Correct 167 ms 27988 KB Output is correct
39 Correct 122 ms 39188 KB Output is correct
40 Correct 160 ms 32264 KB Output is correct
41 Correct 4 ms 9804 KB Output is correct
42 Correct 4 ms 9820 KB Output is correct
43 Correct 3 ms 9820 KB Output is correct
44 Correct 4 ms 9820 KB Output is correct
45 Correct 3 ms 9820 KB Output is correct
46 Correct 4 ms 9820 KB Output is correct
47 Correct 5 ms 9820 KB Output is correct
48 Correct 5 ms 9820 KB Output is correct
49 Correct 3 ms 9820 KB Output is correct
50 Correct 3 ms 9816 KB Output is correct
51 Correct 176 ms 28124 KB Output is correct
52 Correct 106 ms 39396 KB Output is correct
53 Correct 213 ms 32672 KB Output is correct
54 Correct 110 ms 27716 KB Output is correct
55 Correct 153 ms 27648 KB Output is correct
56 Correct 116 ms 39304 KB Output is correct
57 Correct 149 ms 33676 KB Output is correct
58 Correct 112 ms 27784 KB Output is correct
59 Correct 126 ms 28180 KB Output is correct
60 Correct 119 ms 39328 KB Output is correct
61 Correct 194 ms 33648 KB Output is correct
62 Correct 140 ms 28132 KB Output is correct
63 Correct 123 ms 27680 KB Output is correct
64 Correct 100 ms 39232 KB Output is correct
65 Correct 150 ms 33540 KB Output is correct
66 Correct 110 ms 27780 KB Output is correct
67 Correct 118 ms 27960 KB Output is correct
68 Correct 91 ms 39240 KB Output is correct
69 Correct 143 ms 31908 KB Output is correct
70 Correct 127 ms 27896 KB Output is correct