Submission #157697

# Submission time Handle Problem Language Result Execution time Memory
157697 2019-10-12T18:06:51 Z nvmdava Election Campaign (JOI15_election_campaign) C++17
100 / 100
279 ms 29504 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#define N 100005
using namespace std;
#define ll long long
#define ff first
#define ss second

ll bit[N];

ll get(int v){
    ll s = 0;
    while(v){
        s += bit[v];
        v -= v & -v;
    }
    return s;
}

void update(int x, ll v){
    while(x < N){
        bit[x] += v;
        x += x & -x;
    }
}
 
vector<int> adj[N];
 
int in[N], out[N], p[N][20];
int d[N];
 
int n;
int cnt;
 
void dfs2(int v, int par){
    in[v] = ++cnt;
    
    d[v] = d[par] + 1;
    p[v][0] = par;
    
    for(int i = 0; p[v][i] != 0; i++){
        p[v][i + 1] = p[p[v][i]][i];    
    }
    
    for(int& x : adj[v]){
        if(x == par) continue;
        dfs2(x, v);
    }
    
    out[v] = cnt;
}

inline int find(int v, int u){

    if(d[v] < d[u]) swap(v, u);
    
    for(int i = 0; d[v] - d[u]; i++)
        if((d[v] - d[u]) & (1 << i))
            v = p[v][i];    
    
    if(v == u) return v;
    for(int i = 16; i >= 0; i--){
        if(p[v][i] != p[u][i]){
            v = p[v][i];
            u = p[u][i];
        }
    }
    return p[v][0];
}
 
vector<pair<pair<int, int>, int> > obj[N];
 
ll is1[N], is0[N];
 
void dfs(int v, int p){

    ll res = 0;
    
    for(int& x : adj[v]){
        if(x == p) continue;
        dfs(x, v);
        res += is1[x];
    }
    
    is1[v] = is0[v] = res;
    
    
    for(auto& x : obj[v])
        is1[v] = max(is1[v], res + x.ss + get(in[x.ff.ff]) + get(in[x.ff.ss]));
    

    update(in[v], res - is1[v]);
    update(out[v] + 1, is1[v] - res);
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin>>n;
    
    for(int v, u, i = 1; i < n; i++){
        cin>>v>>u;
        adj[v].push_back(u);
        adj[u].push_back(v);
    }
    
    dfs2(1, 0);
    
    cin>>n;
    while(n--){
        int a, b, c;
        cin>>a>>b>>c;
        obj[find(a, b)].push_back({{a, b}, c});
    }
    
    dfs(1, 0);
    
    cout<<is1[1];
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5124 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5140 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 97 ms 19704 KB Output is correct
6 Correct 69 ms 27320 KB Output is correct
7 Correct 101 ms 24644 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 95 ms 23032 KB Output is correct
10 Correct 77 ms 19960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5372 KB Output is correct
4 Correct 153 ms 29356 KB Output is correct
5 Correct 140 ms 29304 KB Output is correct
6 Correct 127 ms 29304 KB Output is correct
7 Correct 145 ms 29304 KB Output is correct
8 Correct 147 ms 29300 KB Output is correct
9 Correct 128 ms 29404 KB Output is correct
10 Correct 143 ms 29416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 7 ms 5372 KB Output is correct
4 Correct 153 ms 29356 KB Output is correct
5 Correct 140 ms 29304 KB Output is correct
6 Correct 127 ms 29304 KB Output is correct
7 Correct 145 ms 29304 KB Output is correct
8 Correct 147 ms 29300 KB Output is correct
9 Correct 128 ms 29404 KB Output is correct
10 Correct 143 ms 29416 KB Output is correct
11 Correct 17 ms 5752 KB Output is correct
12 Correct 152 ms 29456 KB Output is correct
13 Correct 146 ms 29404 KB Output is correct
14 Correct 127 ms 29408 KB Output is correct
15 Correct 147 ms 29292 KB Output is correct
16 Correct 129 ms 29432 KB Output is correct
17 Correct 149 ms 29304 KB Output is correct
18 Correct 143 ms 29304 KB Output is correct
19 Correct 127 ms 29304 KB Output is correct
20 Correct 146 ms 29324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 21196 KB Output is correct
2 Correct 131 ms 29392 KB Output is correct
3 Correct 229 ms 26392 KB Output is correct
4 Correct 122 ms 21520 KB Output is correct
5 Correct 205 ms 25944 KB Output is correct
6 Correct 138 ms 21520 KB Output is correct
7 Correct 227 ms 25608 KB Output is correct
8 Correct 168 ms 21496 KB Output is correct
9 Correct 132 ms 29504 KB Output is correct
10 Correct 279 ms 24548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5124 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5140 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 97 ms 19704 KB Output is correct
6 Correct 69 ms 27320 KB Output is correct
7 Correct 101 ms 24644 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 95 ms 23032 KB Output is correct
10 Correct 77 ms 19960 KB Output is correct
11 Correct 7 ms 5240 KB Output is correct
12 Correct 7 ms 5368 KB Output is correct
13 Correct 7 ms 5240 KB Output is correct
14 Correct 9 ms 5240 KB Output is correct
15 Correct 7 ms 5240 KB Output is correct
16 Correct 7 ms 5240 KB Output is correct
17 Correct 7 ms 5240 KB Output is correct
18 Correct 7 ms 5368 KB Output is correct
19 Correct 7 ms 5244 KB Output is correct
20 Correct 7 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5124 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5140 KB Output is correct
4 Correct 6 ms 5240 KB Output is correct
5 Correct 97 ms 19704 KB Output is correct
6 Correct 69 ms 27320 KB Output is correct
7 Correct 101 ms 24644 KB Output is correct
8 Correct 68 ms 20088 KB Output is correct
9 Correct 95 ms 23032 KB Output is correct
10 Correct 77 ms 19960 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 7 ms 5372 KB Output is correct
14 Correct 153 ms 29356 KB Output is correct
15 Correct 140 ms 29304 KB Output is correct
16 Correct 127 ms 29304 KB Output is correct
17 Correct 145 ms 29304 KB Output is correct
18 Correct 147 ms 29300 KB Output is correct
19 Correct 128 ms 29404 KB Output is correct
20 Correct 143 ms 29416 KB Output is correct
21 Correct 17 ms 5752 KB Output is correct
22 Correct 152 ms 29456 KB Output is correct
23 Correct 146 ms 29404 KB Output is correct
24 Correct 127 ms 29408 KB Output is correct
25 Correct 147 ms 29292 KB Output is correct
26 Correct 129 ms 29432 KB Output is correct
27 Correct 149 ms 29304 KB Output is correct
28 Correct 143 ms 29304 KB Output is correct
29 Correct 127 ms 29304 KB Output is correct
30 Correct 146 ms 29324 KB Output is correct
31 Correct 152 ms 21196 KB Output is correct
32 Correct 131 ms 29392 KB Output is correct
33 Correct 229 ms 26392 KB Output is correct
34 Correct 122 ms 21520 KB Output is correct
35 Correct 205 ms 25944 KB Output is correct
36 Correct 138 ms 21520 KB Output is correct
37 Correct 227 ms 25608 KB Output is correct
38 Correct 168 ms 21496 KB Output is correct
39 Correct 132 ms 29504 KB Output is correct
40 Correct 279 ms 24548 KB Output is correct
41 Correct 7 ms 5240 KB Output is correct
42 Correct 7 ms 5368 KB Output is correct
43 Correct 7 ms 5240 KB Output is correct
44 Correct 9 ms 5240 KB Output is correct
45 Correct 7 ms 5240 KB Output is correct
46 Correct 7 ms 5240 KB Output is correct
47 Correct 7 ms 5240 KB Output is correct
48 Correct 7 ms 5368 KB Output is correct
49 Correct 7 ms 5244 KB Output is correct
50 Correct 7 ms 5368 KB Output is correct
51 Correct 173 ms 21408 KB Output is correct
52 Correct 148 ms 29340 KB Output is correct
53 Correct 244 ms 24596 KB Output is correct
54 Correct 130 ms 21556 KB Output is correct
55 Correct 163 ms 21292 KB Output is correct
56 Correct 145 ms 29304 KB Output is correct
57 Correct 198 ms 25364 KB Output is correct
58 Correct 132 ms 21580 KB Output is correct
59 Correct 163 ms 21368 KB Output is correct
60 Correct 144 ms 29316 KB Output is correct
61 Correct 190 ms 25468 KB Output is correct
62 Correct 131 ms 21580 KB Output is correct
63 Correct 166 ms 21052 KB Output is correct
64 Correct 149 ms 29280 KB Output is correct
65 Correct 227 ms 25392 KB Output is correct
66 Correct 122 ms 21812 KB Output is correct
67 Correct 159 ms 21052 KB Output is correct
68 Correct 150 ms 29484 KB Output is correct
69 Correct 195 ms 24184 KB Output is correct
70 Correct 127 ms 21636 KB Output is correct