답안 #405151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
405151 2021-05-15T19:11:50 Z timmyfeng Election Campaign (JOI15_election_campaign) C++17
100 / 100
276 ms 27356 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100001, L = __lg(N) + 1;

vector<array<int, 3>> updates[N];
int par[L][N], in[N], out[N], t;
vector<int> adj[N];

int votes[N], fenw[N], n;

void update(int a, int x) {
    for ( ; a <= n; a += a & -a) {
        fenw[a] += x;
    }
}

int query(int a) {
    int sum = 0;
    for ( ; a > 0; a -= a & -a) {
        sum += fenw[a];
    }
    return sum;
}

void dfs_init(int u) {
    in[u] = ++t;
    for (auto c : adj[u]) {
        if (c != par[0][u]) {
            par[0][c] = u;
            dfs_init(c);
        }
    }
    out[u] = t;
}

void dfs_solve(int u) {
    for (auto c : adj[u]) {
        if (c != par[0][u]) {
            dfs_solve(c);
        }
    }

    for (auto c : adj[u]) {
        if (c != par[0][u]) {
            update(in[u], votes[c]);
            update(out[u] + 1, -votes[c]);

            update(in[c], -votes[c]);
            update(out[c] + 1, votes[c]);
        }
    }

    votes[u] = query(in[u]);
    for (auto [x, y, w] : updates[u]) {
        votes[u] = max(votes[u], query(in[x]) + query(in[y]) - query(in[u]) + w);
    }
}

bool ancestor(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}

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

    cin >> n;

    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    dfs_init(1);
    out[0] = t;
    cerr << "\n";

    for (int i = 1; i <= __lg(n); ++i) {
        for (int j = 1; j <= n; ++j) {
            par[i][j] = par[i - 1][par[i - 1][j]];
        }
    }

    int m;
    cin >> m;

    for (int i = 0; i < m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;

        int p = u;
        if (!ancestor(p, v)) {
            for (int j = __lg(n); j >= 0; --j) {
                if (!ancestor(par[j][p], v)) {
                    p = par[j][p];
                }
            }
            p = par[0][p];
        }

        updates[p].push_back({u, v, w});
    }

    dfs_solve(1);

    cout << votes[1] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5020 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5024 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 105 ms 17664 KB Output is correct
6 Correct 51 ms 23880 KB Output is correct
7 Correct 73 ms 21520 KB Output is correct
8 Correct 72 ms 18028 KB Output is correct
9 Correct 72 ms 20292 KB Output is correct
10 Correct 67 ms 17984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5016 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 121 ms 27064 KB Output is correct
5 Correct 148 ms 27028 KB Output is correct
6 Correct 105 ms 26984 KB Output is correct
7 Correct 125 ms 27040 KB Output is correct
8 Correct 132 ms 27048 KB Output is correct
9 Correct 110 ms 27076 KB Output is correct
10 Correct 125 ms 27172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5016 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 121 ms 27064 KB Output is correct
5 Correct 148 ms 27028 KB Output is correct
6 Correct 105 ms 26984 KB Output is correct
7 Correct 125 ms 27040 KB Output is correct
8 Correct 132 ms 27048 KB Output is correct
9 Correct 110 ms 27076 KB Output is correct
10 Correct 125 ms 27172 KB Output is correct
11 Correct 14 ms 6020 KB Output is correct
12 Correct 132 ms 27348 KB Output is correct
13 Correct 146 ms 27356 KB Output is correct
14 Correct 105 ms 27332 KB Output is correct
15 Correct 130 ms 27332 KB Output is correct
16 Correct 112 ms 27336 KB Output is correct
17 Correct 129 ms 27336 KB Output is correct
18 Correct 142 ms 27252 KB Output is correct
19 Correct 121 ms 27340 KB Output is correct
20 Correct 126 ms 27336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 20416 KB Output is correct
2 Correct 128 ms 27048 KB Output is correct
3 Correct 216 ms 24772 KB Output is correct
4 Correct 153 ms 20920 KB Output is correct
5 Correct 242 ms 24116 KB Output is correct
6 Correct 160 ms 20904 KB Output is correct
7 Correct 195 ms 23908 KB Output is correct
8 Correct 226 ms 20612 KB Output is correct
9 Correct 114 ms 27168 KB Output is correct
10 Correct 186 ms 23032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5020 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5024 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 105 ms 17664 KB Output is correct
6 Correct 51 ms 23880 KB Output is correct
7 Correct 73 ms 21520 KB Output is correct
8 Correct 72 ms 18028 KB Output is correct
9 Correct 72 ms 20292 KB Output is correct
10 Correct 67 ms 17984 KB Output is correct
11 Correct 5 ms 5196 KB Output is correct
12 Correct 5 ms 5196 KB Output is correct
13 Correct 5 ms 5196 KB Output is correct
14 Correct 4 ms 5196 KB Output is correct
15 Correct 5 ms 5196 KB Output is correct
16 Correct 5 ms 5196 KB Output is correct
17 Correct 5 ms 5160 KB Output is correct
18 Correct 5 ms 5188 KB Output is correct
19 Correct 5 ms 5196 KB Output is correct
20 Correct 5 ms 5196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5020 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 5024 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 105 ms 17664 KB Output is correct
6 Correct 51 ms 23880 KB Output is correct
7 Correct 73 ms 21520 KB Output is correct
8 Correct 72 ms 18028 KB Output is correct
9 Correct 72 ms 20292 KB Output is correct
10 Correct 67 ms 17984 KB Output is correct
11 Correct 4 ms 5016 KB Output is correct
12 Correct 4 ms 4940 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 121 ms 27064 KB Output is correct
15 Correct 148 ms 27028 KB Output is correct
16 Correct 105 ms 26984 KB Output is correct
17 Correct 125 ms 27040 KB Output is correct
18 Correct 132 ms 27048 KB Output is correct
19 Correct 110 ms 27076 KB Output is correct
20 Correct 125 ms 27172 KB Output is correct
21 Correct 14 ms 6020 KB Output is correct
22 Correct 132 ms 27348 KB Output is correct
23 Correct 146 ms 27356 KB Output is correct
24 Correct 105 ms 27332 KB Output is correct
25 Correct 130 ms 27332 KB Output is correct
26 Correct 112 ms 27336 KB Output is correct
27 Correct 129 ms 27336 KB Output is correct
28 Correct 142 ms 27252 KB Output is correct
29 Correct 121 ms 27340 KB Output is correct
30 Correct 126 ms 27336 KB Output is correct
31 Correct 175 ms 20416 KB Output is correct
32 Correct 128 ms 27048 KB Output is correct
33 Correct 216 ms 24772 KB Output is correct
34 Correct 153 ms 20920 KB Output is correct
35 Correct 242 ms 24116 KB Output is correct
36 Correct 160 ms 20904 KB Output is correct
37 Correct 195 ms 23908 KB Output is correct
38 Correct 226 ms 20612 KB Output is correct
39 Correct 114 ms 27168 KB Output is correct
40 Correct 186 ms 23032 KB Output is correct
41 Correct 5 ms 5196 KB Output is correct
42 Correct 5 ms 5196 KB Output is correct
43 Correct 5 ms 5196 KB Output is correct
44 Correct 4 ms 5196 KB Output is correct
45 Correct 5 ms 5196 KB Output is correct
46 Correct 5 ms 5196 KB Output is correct
47 Correct 5 ms 5160 KB Output is correct
48 Correct 5 ms 5188 KB Output is correct
49 Correct 5 ms 5196 KB Output is correct
50 Correct 5 ms 5196 KB Output is correct
51 Correct 234 ms 21008 KB Output is correct
52 Correct 130 ms 27324 KB Output is correct
53 Correct 225 ms 23528 KB Output is correct
54 Correct 172 ms 21164 KB Output is correct
55 Correct 167 ms 20732 KB Output is correct
56 Correct 142 ms 27316 KB Output is correct
57 Correct 195 ms 24140 KB Output is correct
58 Correct 138 ms 21092 KB Output is correct
59 Correct 276 ms 21032 KB Output is correct
60 Correct 125 ms 27304 KB Output is correct
61 Correct 180 ms 24192 KB Output is correct
62 Correct 149 ms 20808 KB Output is correct
63 Correct 177 ms 20652 KB Output is correct
64 Correct 126 ms 27348 KB Output is correct
65 Correct 230 ms 23928 KB Output is correct
66 Correct 175 ms 21160 KB Output is correct
67 Correct 181 ms 20568 KB Output is correct
68 Correct 160 ms 27324 KB Output is correct
69 Correct 207 ms 23188 KB Output is correct
70 Correct 139 ms 21256 KB Output is correct