Submission #660811

# Submission time Handle Problem Language Result Execution time Memory
660811 2022-11-23T09:17:25 Z Danilo21 Election Campaign (JOI15_election_campaign) C++14
100 / 100
289 ms 51128 KB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) (__builtin_popcountll(a))
#define bithigh(a) (63-__builtin_clzll(a))
#define lg bithigh
#define highpow(a) (1LL << (ll)lg(a))

using namespace std;

class Tree{

private:
    int n, root;
    vector<int> depth, in, out;
    vector<vector<int> > g, par;
    int _edges_ = 0;
    bool _initialized_ = 0;

    bool Valid(int s) const { return s >= 0 && s < n; }

    int InitDfs(int s, int p = -1, int d = 0, int t = 0){
        par[s][0] = p;
        depth[s] = d;
        in[s] = t;
        for (int u : g[s])
            if (u^p)
                t = InitDfs(u, s, d+1, t+1);
        return out[s] = ++t;
    }

    void Dfs(int s, int p, void bf(int, int), void af(int, int)) const {
        bf(s, p);
        for (int u : g[s])
            if (u^p)
                Dfs(u, s, bf, af);
        af(s, p);
    }

public:
    Tree(int n = 0){ Assign(n); }

    void Assign(int n = 0){
        this->n = n;
        depth.assign(n, 0);
        in.assign(n, 0);
        out.assign(n, 0);
        g.assign(n, vector<int>());
        par.assign(n, vector<int>(lg(n)+1, -1));
    }

    void Edge(int u, int v){
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        g[u].pb(v);
        g[v].pb(u);
        _edges_++;
    }

    void Read(int d = 1){
        for (int i = 0; i < n-1; i++){
            ri(u); ri(v);
            u -= d; v -= d;
            Edge(u, v);
        }
    }

    void Initialize(int s = 0){
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (_edges_ < n-1){
            cerr << "Tree is not connected" << endl;
            exit(1);
        }
        if (_edges_ > n-1){
            cerr << "Tree has cycle(s)" << endl;
            exit(1);
        }
        root = s;
        InitDfs(s, -1);
        for (int d = 1; d <= lg(n); d++)
            for (int i = 0; i < n; i++)
                if (depth[i] >= (1<<d))
                    par[i][d] = par[par[i][d-1]][d-1];
        _initialized_ = 1;
    }

    int Size() const { return n; }

    int Depth(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return depth[s];
    }

    int InTime(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return in[s];
    }

    int OutTime(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return out[s];
    }

    vector<int> GetAdjecent(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        return g[s];
    }

    vector<int> GetChilds(int s) const {
        if (!Valid(s)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        vector<int> ch;
        for (int u : g[s])
            if (u^par[s][0])
                ch.pb(u);
        return ch;
    }

    int Par(int s, int d = 1) const {
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        if (d < 0 || d > depth[s]) return -1;
        if (!d) return s;
        return Par(par[s][lg(d)], d - highpow(d));
    }

    bool Ancestor(int s, int p) const {
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        return in[s] > in[p] && out[s] < out[p];
    }

    int Lca(int u, int v) const {
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        if (depth[u] > depth[v]) swap(u, v);
        if (Ancestor(v, u)) return u;
        v = Par(v, depth[v] - depth[u]);
        for (int d = lg(n); ~d; d--){
            if (par[u][d] != par[v][d]){
                u = par[u][d];
                v = par[v][d];
            }
        }
        return par[u][0];
    }

    int Dist(int u, int v) const {
        if (!Valid(u) || !Valid(v)){
            cerr << "Node index out of range" << endl;
            exit(1);
        }
        if (!_initialized_){
            cerr << "Tree has not been initialized yet" << endl;
            exit(1);
        }
        int lca = Lca(u, v);
        return 2*depth[lca] - depth[u] - depth[v];
    }

    void Dfs(void bf(int, int), void af(int, int)) const { Dfs(root, -1, bf, af); }
};
#define Empty [](int, int){}

class BIT{

    int n;
    vector<ll> tree;
    ll query(int pos) const {
        smin(pos, n);
        ll sum = 0;
        for (; pos; pos -= (pos & (-pos)))
            sum += tree[pos];
        return sum;
    }
public:
    void Assign(int s){ n = s; tree.assign(s+1, 0); }
    ll query(int l, int r) const {
        l++; r++;
        if (l>r) return 0;
        return query(r) - query(l-1);
    }
    void update(int pos, ll x){
        pos++;
        if (pos < 1) return;
        for (; pos <= n; pos += (pos & (-pos)))
            tree[pos] += x;
    }
};

const ll LINF = 4e18;
const int mxN = 1e6+10, INF = 2e9;
ll n, m, a[mxN], dp[mxN];
vector<array<int, 3> > path[mxN];
Tree tree;
BIT fen;

ll sumPath(int p, int s){ return fen.query(tree.InTime(p), tree.InTime(s)); }

void dfsF(int s, int p){

    for (int u : tree.GetChilds(s))
        dp[s] += dp[u];
    for (auto [u, v, w] : path[s]){
        ll cur = sumPath(s, u);
        cur += sumPath(tree.Par(v, tree.Depth(v) - tree.Depth(s) - 1), v);
        smax(dp[s], cur + w);
    }
    fen.update(tree.InTime(s), -dp[s]);
    fen.update(tree.OutTime(s), dp[s]);
    if (~p){
        fen.update(tree.InTime(p), dp[s]);
        fen.update(tree.OutTime(p), -dp[s]);
    }
}

void Solve(){

    cin >> n;
    tree.Assign(n);
    tree.Read();
    tree.Initialize();
    cin >> m;
    for (int i = 0; i < m; i++){
        ri(u); ri(v); rl(w);
        u--; v--;
        int lca = tree.Lca(u, v);
        if (tree.Depth(u) > tree.Depth(v)) swap(u, v);
        path[lca].pb({u, v, w});
    }
    fen.Assign(2*n);
    tree.Dfs(Empty, &dfsF);
    cout << dp[0] << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}

Compilation message

election_campaign.cpp: In function 'void dfsF(int, int)':
election_campaign.cpp:254:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  254 |     for (auto [u, v, w] : path[s]){
      |               ^
election_campaign.cpp: In function 'void Solve()':
election_campaign.cpp:279:29: warning: narrowing conversion of 'w' from 'long long int' to 'int' [-Wnarrowing]
  279 |         path[lca].pb({u, v, w});
      |                             ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 16 ms 23932 KB Output is correct
5 Correct 133 ms 44256 KB Output is correct
6 Correct 69 ms 47432 KB Output is correct
7 Correct 121 ms 46216 KB Output is correct
8 Correct 89 ms 44036 KB Output is correct
9 Correct 158 ms 45732 KB Output is correct
10 Correct 84 ms 43980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 13 ms 23900 KB Output is correct
4 Correct 137 ms 50708 KB Output is correct
5 Correct 120 ms 50700 KB Output is correct
6 Correct 116 ms 50712 KB Output is correct
7 Correct 122 ms 50692 KB Output is correct
8 Correct 127 ms 50768 KB Output is correct
9 Correct 119 ms 50680 KB Output is correct
10 Correct 117 ms 50712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 13 ms 23900 KB Output is correct
4 Correct 137 ms 50708 KB Output is correct
5 Correct 120 ms 50700 KB Output is correct
6 Correct 116 ms 50712 KB Output is correct
7 Correct 122 ms 50692 KB Output is correct
8 Correct 127 ms 50768 KB Output is correct
9 Correct 119 ms 50680 KB Output is correct
10 Correct 117 ms 50712 KB Output is correct
11 Correct 20 ms 24732 KB Output is correct
12 Correct 132 ms 51128 KB Output is correct
13 Correct 128 ms 50964 KB Output is correct
14 Correct 113 ms 50956 KB Output is correct
15 Correct 140 ms 50972 KB Output is correct
16 Correct 124 ms 51040 KB Output is correct
17 Correct 123 ms 50944 KB Output is correct
18 Correct 118 ms 50980 KB Output is correct
19 Correct 128 ms 51040 KB Output is correct
20 Correct 126 ms 51016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 47016 KB Output is correct
2 Correct 112 ms 50800 KB Output is correct
3 Correct 279 ms 49392 KB Output is correct
4 Correct 135 ms 46796 KB Output is correct
5 Correct 240 ms 49220 KB Output is correct
6 Correct 142 ms 46748 KB Output is correct
7 Correct 261 ms 48980 KB Output is correct
8 Correct 199 ms 47372 KB Output is correct
9 Correct 125 ms 50764 KB Output is correct
10 Correct 289 ms 48448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 16 ms 23932 KB Output is correct
5 Correct 133 ms 44256 KB Output is correct
6 Correct 69 ms 47432 KB Output is correct
7 Correct 121 ms 46216 KB Output is correct
8 Correct 89 ms 44036 KB Output is correct
9 Correct 158 ms 45732 KB Output is correct
10 Correct 84 ms 43980 KB Output is correct
11 Correct 14 ms 24020 KB Output is correct
12 Correct 13 ms 24012 KB Output is correct
13 Correct 14 ms 23980 KB Output is correct
14 Correct 25 ms 24020 KB Output is correct
15 Correct 14 ms 23972 KB Output is correct
16 Correct 16 ms 23912 KB Output is correct
17 Correct 13 ms 23980 KB Output is correct
18 Correct 14 ms 24020 KB Output is correct
19 Correct 14 ms 24092 KB Output is correct
20 Correct 16 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 16 ms 23816 KB Output is correct
4 Correct 16 ms 23932 KB Output is correct
5 Correct 133 ms 44256 KB Output is correct
6 Correct 69 ms 47432 KB Output is correct
7 Correct 121 ms 46216 KB Output is correct
8 Correct 89 ms 44036 KB Output is correct
9 Correct 158 ms 45732 KB Output is correct
10 Correct 84 ms 43980 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 13 ms 23900 KB Output is correct
14 Correct 137 ms 50708 KB Output is correct
15 Correct 120 ms 50700 KB Output is correct
16 Correct 116 ms 50712 KB Output is correct
17 Correct 122 ms 50692 KB Output is correct
18 Correct 127 ms 50768 KB Output is correct
19 Correct 119 ms 50680 KB Output is correct
20 Correct 117 ms 50712 KB Output is correct
21 Correct 20 ms 24732 KB Output is correct
22 Correct 132 ms 51128 KB Output is correct
23 Correct 128 ms 50964 KB Output is correct
24 Correct 113 ms 50956 KB Output is correct
25 Correct 140 ms 50972 KB Output is correct
26 Correct 124 ms 51040 KB Output is correct
27 Correct 123 ms 50944 KB Output is correct
28 Correct 118 ms 50980 KB Output is correct
29 Correct 128 ms 51040 KB Output is correct
30 Correct 126 ms 51016 KB Output is correct
31 Correct 224 ms 47016 KB Output is correct
32 Correct 112 ms 50800 KB Output is correct
33 Correct 279 ms 49392 KB Output is correct
34 Correct 135 ms 46796 KB Output is correct
35 Correct 240 ms 49220 KB Output is correct
36 Correct 142 ms 46748 KB Output is correct
37 Correct 261 ms 48980 KB Output is correct
38 Correct 199 ms 47372 KB Output is correct
39 Correct 125 ms 50764 KB Output is correct
40 Correct 289 ms 48448 KB Output is correct
41 Correct 14 ms 24020 KB Output is correct
42 Correct 13 ms 24012 KB Output is correct
43 Correct 14 ms 23980 KB Output is correct
44 Correct 25 ms 24020 KB Output is correct
45 Correct 14 ms 23972 KB Output is correct
46 Correct 16 ms 23912 KB Output is correct
47 Correct 13 ms 23980 KB Output is correct
48 Correct 14 ms 24020 KB Output is correct
49 Correct 14 ms 24092 KB Output is correct
50 Correct 16 ms 24020 KB Output is correct
51 Correct 202 ms 47660 KB Output is correct
52 Correct 125 ms 51024 KB Output is correct
53 Correct 278 ms 48816 KB Output is correct
54 Correct 124 ms 47048 KB Output is correct
55 Correct 187 ms 47400 KB Output is correct
56 Correct 127 ms 50984 KB Output is correct
57 Correct 225 ms 49352 KB Output is correct
58 Correct 134 ms 46980 KB Output is correct
59 Correct 176 ms 47704 KB Output is correct
60 Correct 124 ms 51000 KB Output is correct
61 Correct 246 ms 49256 KB Output is correct
62 Correct 147 ms 46812 KB Output is correct
63 Correct 234 ms 47236 KB Output is correct
64 Correct 120 ms 51020 KB Output is correct
65 Correct 280 ms 49024 KB Output is correct
66 Correct 164 ms 47072 KB Output is correct
67 Correct 183 ms 47304 KB Output is correct
68 Correct 125 ms 51012 KB Output is correct
69 Correct 253 ms 48704 KB Output is correct
70 Correct 136 ms 47148 KB Output is correct