Submission #921951

# Submission time Handle Problem Language Result Execution time Memory
921951 2024-02-04T15:00:16 Z LOLOLO Election Campaign (JOI15_election_campaign) C++17
100 / 100
169 ms 48216 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 = 2e5 + 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;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17244 KB Output is correct
2 Correct 3 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 4 ms 17272 KB Output is correct
5 Correct 58 ms 29908 KB Output is correct
6 Correct 48 ms 40016 KB Output is correct
7 Correct 69 ms 36060 KB Output is correct
8 Correct 49 ms 28720 KB Output is correct
9 Correct 62 ms 33872 KB Output is correct
10 Correct 52 ms 28756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 17244 KB Output is correct
2 Correct 4 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 87 ms 47644 KB Output is correct
5 Correct 101 ms 47956 KB Output is correct
6 Correct 94 ms 47696 KB Output is correct
7 Correct 85 ms 47700 KB Output is correct
8 Correct 88 ms 47708 KB Output is correct
9 Correct 86 ms 47656 KB Output is correct
10 Correct 87 ms 47696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 17244 KB Output is correct
2 Correct 4 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 87 ms 47644 KB Output is correct
5 Correct 101 ms 47956 KB Output is correct
6 Correct 94 ms 47696 KB Output is correct
7 Correct 85 ms 47700 KB Output is correct
8 Correct 88 ms 47708 KB Output is correct
9 Correct 86 ms 47656 KB Output is correct
10 Correct 87 ms 47696 KB Output is correct
11 Correct 11 ms 19032 KB Output is correct
12 Correct 103 ms 48100 KB Output is correct
13 Correct 89 ms 47940 KB Output is correct
14 Correct 102 ms 48216 KB Output is correct
15 Correct 105 ms 48060 KB Output is correct
16 Correct 92 ms 47952 KB Output is correct
17 Correct 89 ms 47956 KB Output is correct
18 Correct 90 ms 47980 KB Output is correct
19 Correct 90 ms 47956 KB Output is correct
20 Correct 108 ms 47956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 33624 KB Output is correct
2 Correct 88 ms 47644 KB Output is correct
3 Correct 167 ms 43580 KB Output is correct
4 Correct 103 ms 35524 KB Output is correct
5 Correct 122 ms 43580 KB Output is correct
6 Correct 120 ms 35772 KB Output is correct
7 Correct 133 ms 42500 KB Output is correct
8 Correct 106 ms 36532 KB Output is correct
9 Correct 90 ms 47696 KB Output is correct
10 Correct 130 ms 41136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17244 KB Output is correct
2 Correct 3 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 4 ms 17272 KB Output is correct
5 Correct 58 ms 29908 KB Output is correct
6 Correct 48 ms 40016 KB Output is correct
7 Correct 69 ms 36060 KB Output is correct
8 Correct 49 ms 28720 KB Output is correct
9 Correct 62 ms 33872 KB Output is correct
10 Correct 52 ms 28756 KB Output is correct
11 Correct 5 ms 17240 KB Output is correct
12 Correct 4 ms 17244 KB Output is correct
13 Correct 4 ms 17244 KB Output is correct
14 Correct 5 ms 17240 KB Output is correct
15 Correct 5 ms 17244 KB Output is correct
16 Correct 7 ms 17244 KB Output is correct
17 Correct 5 ms 17244 KB Output is correct
18 Correct 5 ms 17244 KB Output is correct
19 Correct 5 ms 17244 KB Output is correct
20 Correct 6 ms 17240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 17244 KB Output is correct
2 Correct 3 ms 17244 KB Output is correct
3 Correct 4 ms 17244 KB Output is correct
4 Correct 4 ms 17272 KB Output is correct
5 Correct 58 ms 29908 KB Output is correct
6 Correct 48 ms 40016 KB Output is correct
7 Correct 69 ms 36060 KB Output is correct
8 Correct 49 ms 28720 KB Output is correct
9 Correct 62 ms 33872 KB Output is correct
10 Correct 52 ms 28756 KB Output is correct
11 Correct 3 ms 17244 KB Output is correct
12 Correct 4 ms 17244 KB Output is correct
13 Correct 4 ms 17244 KB Output is correct
14 Correct 87 ms 47644 KB Output is correct
15 Correct 101 ms 47956 KB Output is correct
16 Correct 94 ms 47696 KB Output is correct
17 Correct 85 ms 47700 KB Output is correct
18 Correct 88 ms 47708 KB Output is correct
19 Correct 86 ms 47656 KB Output is correct
20 Correct 87 ms 47696 KB Output is correct
21 Correct 11 ms 19032 KB Output is correct
22 Correct 103 ms 48100 KB Output is correct
23 Correct 89 ms 47940 KB Output is correct
24 Correct 102 ms 48216 KB Output is correct
25 Correct 105 ms 48060 KB Output is correct
26 Correct 92 ms 47952 KB Output is correct
27 Correct 89 ms 47956 KB Output is correct
28 Correct 90 ms 47980 KB Output is correct
29 Correct 90 ms 47956 KB Output is correct
30 Correct 108 ms 47956 KB Output is correct
31 Correct 121 ms 33624 KB Output is correct
32 Correct 88 ms 47644 KB Output is correct
33 Correct 167 ms 43580 KB Output is correct
34 Correct 103 ms 35524 KB Output is correct
35 Correct 122 ms 43580 KB Output is correct
36 Correct 120 ms 35772 KB Output is correct
37 Correct 133 ms 42500 KB Output is correct
38 Correct 106 ms 36532 KB Output is correct
39 Correct 90 ms 47696 KB Output is correct
40 Correct 130 ms 41136 KB Output is correct
41 Correct 5 ms 17240 KB Output is correct
42 Correct 4 ms 17244 KB Output is correct
43 Correct 4 ms 17244 KB Output is correct
44 Correct 5 ms 17240 KB Output is correct
45 Correct 5 ms 17244 KB Output is correct
46 Correct 7 ms 17244 KB Output is correct
47 Correct 5 ms 17244 KB Output is correct
48 Correct 5 ms 17244 KB Output is correct
49 Correct 5 ms 17244 KB Output is correct
50 Correct 6 ms 17240 KB Output is correct
51 Correct 157 ms 36936 KB Output is correct
52 Correct 95 ms 47920 KB Output is correct
53 Correct 130 ms 41552 KB Output is correct
54 Correct 98 ms 35912 KB Output is correct
55 Correct 111 ms 36352 KB Output is correct
56 Correct 92 ms 47976 KB Output is correct
57 Correct 124 ms 42344 KB Output is correct
58 Correct 102 ms 35844 KB Output is correct
59 Correct 114 ms 37616 KB Output is correct
60 Correct 93 ms 48124 KB Output is correct
61 Correct 133 ms 42380 KB Output is correct
62 Correct 114 ms 35932 KB Output is correct
63 Correct 121 ms 37056 KB Output is correct
64 Correct 90 ms 48052 KB Output is correct
65 Correct 147 ms 42544 KB Output is correct
66 Correct 125 ms 36224 KB Output is correct
67 Correct 115 ms 36260 KB Output is correct
68 Correct 88 ms 47956 KB Output is correct
69 Correct 169 ms 40736 KB Output is correct
70 Correct 106 ms 35760 KB Output is correct