Submission #910265

# Submission time Handle Problem Language Result Execution time Memory
910265 2024-01-18T03:26:41 Z LOLOLO Election Campaign (JOI15_election_campaign) C++17
0 / 100
115 ms 27468 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 = 1;

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];

    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, int v) {
    for (auto x : ed[u]) {
        dfs2(x, u);
        s[u] += f[x];
    }

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

    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]));
    }
}

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, 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;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9564 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Incorrect 2 ms 9816 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Incorrect 2 ms 9800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Incorrect 2 ms 9800 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 115 ms 27468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9564 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Incorrect 2 ms 9816 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9564 KB Output is correct
2 Correct 3 ms 9564 KB Output is correct
3 Incorrect 2 ms 9816 KB Output isn't correct
4 Halted 0 ms 0 KB -