Submission #776411

# Submission time Handle Problem Language Result Execution time Memory
776411 2023-07-07T20:22:17 Z ThegeekKnight16 Election Campaign (JOI15_election_campaign) C++17
100 / 100
358 ms 46328 KB
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXK = 21;
#define int long long
array<vector<int>, MAXN> grafo;
array<vector<tuple<int, int, int>>, MAXN> plans;
array<array<int, MAXK>, MAXN> bnLift;
array<int, MAXN> prof, sub, tin, tout, head;
array<array<int, 2>, MAXN> dp;
array<int, 4*MAXN> seg;
int temp = 0;
int N;

void update(int pos, int ini, int fim, int id, int val)
{
    if (id < ini || id > fim) return;
    if (ini == fim)
    {
        seg[pos] = val;
        return;
    }
    int m = (ini + fim) >> 1;
    int e = 2*pos; int d = 2*pos + 1;
    update(e, ini, m, id, val);
    update(d, m+1, fim, id, val);
    seg[pos] = seg[e] + seg[d];
}

int query(int pos, int ini, int fim, int p, int q)
{
    if (q < ini || p > fim) return 0;
    if (p <= ini && fim <= q) return seg[pos];
    int m = (ini + fim) >> 1;
    int e = 2*pos; int d = 2*pos + 1;
    return (query(e, ini, m, p, q) + query(d, m+1, fim, p, q));
}

void dfsProf(int v, int p)
{
    sub[v] = 1; prof[v] = prof[p] + 1;
    bnLift[v][0] = p;
    for (int k = 1; k < MAXK; k++) bnLift[v][k] = bnLift[bnLift[v][k-1]][k-1];

    for (auto &viz : grafo[v])
    {
        if (viz == p) continue;
        dfsProf(viz, v);
        sub[v] += sub[viz];
        if (grafo[v][0] == p || sub[grafo[v][0]] < sub[viz]) swap(viz, grafo[v][0]);
    }
}

void dfsInit(int v, int p, int paiCur)
{
    tin[v] = ++temp; head[v] = paiCur;

    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        if (viz == grafo[v][0]) dfsInit(viz, v, paiCur);
        else dfsInit(viz, v, viz);
    }

    tout[v] = temp;
}

//Path from x to y
int query_path(int x, int y)
{
    if (head[x] == head[y]) return query(1, 1, N, tin[y]+1, tin[x]);
    else return (query(1, 1, N, tin[head[x]], tin[x]) + query_path(bnLift[head[x]][0], y));
}

int lca(int a, int b)
{
    if (prof[a] < prof[b]) swap(a, b);
    int jump = prof[a] - prof[b];
    for (int k = 0; k < MAXK; k++) if (jump & (1 << k)) a = bnLift[a][k];
    if (a == b) return a;
    for (int k = MAXK-1; k >= 0; k--)
        if (bnLift[a][k] != bnLift[b][k]) a = bnLift[a][k], b = bnLift[b][k];

    return bnLift[a][0];
}

void dfsDp(int v, int p)
{
    dp[v][0] = dp[v][1] = 0;

    for (auto viz : grafo[v])
    {
        if (viz == p) continue;
        dfsDp(viz, v);
        dp[v][0] += dp[viz][1];
    }

    dp[v][1] = dp[v][0];
    for (auto [X, Y, C] : plans[v])
    {
        int aux = dp[v][0];
        aux += query_path(X, v);
        aux += query_path(Y, v);
        aux += C;
        dp[v][1] = max(dp[v][1], aux);
    }

    update(1, 1, N, tin[v], dp[v][0] - dp[v][1]);
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N;
    for (int i = 1; i < N; i++)
    {
        int X, Y;
        cin >> X >> Y;
        grafo[X].push_back(Y);
        grafo[Y].push_back(X);
    }
    dfsProf(1, 1); dfsInit(1, 1, 1);
    dfsDp(1, 1);

    int M;
    cin >> M;
    for (int i = 1; i <= M; i++)
    {
        int X, Y, C;
        cin >> X >> Y >> C;
        if (prof[X] > prof[Y]) swap(X, Y);
        plans[lca(X, Y)].emplace_back(X, Y, C);
    }

    dfsDp(1, 1);

    cout << max(dp[1][0], dp[1][1]) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5332 KB Output is correct
5 Correct 118 ms 32844 KB Output is correct
6 Correct 72 ms 39896 KB Output is correct
7 Correct 128 ms 37516 KB Output is correct
8 Correct 97 ms 33092 KB Output is correct
9 Correct 118 ms 36044 KB Output is correct
10 Correct 84 ms 33036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4992 KB Output is correct
3 Correct 3 ms 5332 KB Output is correct
4 Correct 148 ms 45948 KB Output is correct
5 Correct 146 ms 46008 KB Output is correct
6 Correct 128 ms 45952 KB Output is correct
7 Correct 142 ms 45896 KB Output is correct
8 Correct 142 ms 45900 KB Output is correct
9 Correct 150 ms 46008 KB Output is correct
10 Correct 154 ms 45924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4992 KB Output is correct
3 Correct 3 ms 5332 KB Output is correct
4 Correct 148 ms 45948 KB Output is correct
5 Correct 146 ms 46008 KB Output is correct
6 Correct 128 ms 45952 KB Output is correct
7 Correct 142 ms 45896 KB Output is correct
8 Correct 142 ms 45900 KB Output is correct
9 Correct 150 ms 46008 KB Output is correct
10 Correct 154 ms 45924 KB Output is correct
11 Correct 12 ms 6612 KB Output is correct
12 Correct 146 ms 46156 KB Output is correct
13 Correct 166 ms 46328 KB Output is correct
14 Correct 130 ms 46256 KB Output is correct
15 Correct 161 ms 46248 KB Output is correct
16 Correct 130 ms 46248 KB Output is correct
17 Correct 154 ms 46184 KB Output is correct
18 Correct 150 ms 46200 KB Output is correct
19 Correct 129 ms 46156 KB Output is correct
20 Correct 145 ms 46232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 281 ms 35736 KB Output is correct
2 Correct 131 ms 43440 KB Output is correct
3 Correct 291 ms 40736 KB Output is correct
4 Correct 175 ms 36208 KB Output is correct
5 Correct 214 ms 40088 KB Output is correct
6 Correct 173 ms 36312 KB Output is correct
7 Correct 313 ms 39836 KB Output is correct
8 Correct 235 ms 35816 KB Output is correct
9 Correct 137 ms 43468 KB Output is correct
10 Correct 266 ms 38712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5332 KB Output is correct
5 Correct 118 ms 32844 KB Output is correct
6 Correct 72 ms 39896 KB Output is correct
7 Correct 128 ms 37516 KB Output is correct
8 Correct 97 ms 33092 KB Output is correct
9 Correct 118 ms 36044 KB Output is correct
10 Correct 84 ms 33036 KB Output is correct
11 Correct 4 ms 5332 KB Output is correct
12 Correct 3 ms 5332 KB Output is correct
13 Correct 4 ms 5416 KB Output is correct
14 Correct 4 ms 5332 KB Output is correct
15 Correct 4 ms 5332 KB Output is correct
16 Correct 3 ms 5332 KB Output is correct
17 Correct 3 ms 5332 KB Output is correct
18 Correct 3 ms 5332 KB Output is correct
19 Correct 3 ms 5332 KB Output is correct
20 Correct 3 ms 5332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 5332 KB Output is correct
5 Correct 118 ms 32844 KB Output is correct
6 Correct 72 ms 39896 KB Output is correct
7 Correct 128 ms 37516 KB Output is correct
8 Correct 97 ms 33092 KB Output is correct
9 Correct 118 ms 36044 KB Output is correct
10 Correct 84 ms 33036 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4992 KB Output is correct
13 Correct 3 ms 5332 KB Output is correct
14 Correct 148 ms 45948 KB Output is correct
15 Correct 146 ms 46008 KB Output is correct
16 Correct 128 ms 45952 KB Output is correct
17 Correct 142 ms 45896 KB Output is correct
18 Correct 142 ms 45900 KB Output is correct
19 Correct 150 ms 46008 KB Output is correct
20 Correct 154 ms 45924 KB Output is correct
21 Correct 12 ms 6612 KB Output is correct
22 Correct 146 ms 46156 KB Output is correct
23 Correct 166 ms 46328 KB Output is correct
24 Correct 130 ms 46256 KB Output is correct
25 Correct 161 ms 46248 KB Output is correct
26 Correct 130 ms 46248 KB Output is correct
27 Correct 154 ms 46184 KB Output is correct
28 Correct 150 ms 46200 KB Output is correct
29 Correct 129 ms 46156 KB Output is correct
30 Correct 145 ms 46232 KB Output is correct
31 Correct 281 ms 35736 KB Output is correct
32 Correct 131 ms 43440 KB Output is correct
33 Correct 291 ms 40736 KB Output is correct
34 Correct 175 ms 36208 KB Output is correct
35 Correct 214 ms 40088 KB Output is correct
36 Correct 173 ms 36312 KB Output is correct
37 Correct 313 ms 39836 KB Output is correct
38 Correct 235 ms 35816 KB Output is correct
39 Correct 137 ms 43468 KB Output is correct
40 Correct 266 ms 38712 KB Output is correct
41 Correct 4 ms 5332 KB Output is correct
42 Correct 3 ms 5332 KB Output is correct
43 Correct 4 ms 5416 KB Output is correct
44 Correct 4 ms 5332 KB Output is correct
45 Correct 4 ms 5332 KB Output is correct
46 Correct 3 ms 5332 KB Output is correct
47 Correct 3 ms 5332 KB Output is correct
48 Correct 3 ms 5332 KB Output is correct
49 Correct 3 ms 5332 KB Output is correct
50 Correct 3 ms 5332 KB Output is correct
51 Correct 231 ms 38604 KB Output is correct
52 Correct 146 ms 46116 KB Output is correct
53 Correct 256 ms 41632 KB Output is correct
54 Correct 172 ms 38800 KB Output is correct
55 Correct 358 ms 38472 KB Output is correct
56 Correct 149 ms 46224 KB Output is correct
57 Correct 257 ms 42400 KB Output is correct
58 Correct 183 ms 38760 KB Output is correct
59 Correct 222 ms 38576 KB Output is correct
60 Correct 159 ms 46188 KB Output is correct
61 Correct 222 ms 42468 KB Output is correct
62 Correct 184 ms 38912 KB Output is correct
63 Correct 298 ms 38496 KB Output is correct
64 Correct 145 ms 46180 KB Output is correct
65 Correct 259 ms 42836 KB Output is correct
66 Correct 173 ms 38544 KB Output is correct
67 Correct 292 ms 39056 KB Output is correct
68 Correct 150 ms 46156 KB Output is correct
69 Correct 216 ms 41224 KB Output is correct
70 Correct 184 ms 38916 KB Output is correct