Submission #157698

# Submission time Handle Problem Language Result Execution time Memory
157698 2019-10-12T18:08:17 Z nvmdava Election Campaign (JOI15_election_campaign) C++17
100 / 100
250 ms 29612 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("fma")
#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 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 105 ms 19704 KB Output is correct
6 Correct 74 ms 27384 KB Output is correct
7 Correct 116 ms 24664 KB Output is correct
8 Correct 89 ms 20044 KB Output is correct
9 Correct 95 ms 23008 KB Output is correct
10 Correct 86 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 5368 KB Output is correct
4 Correct 140 ms 29480 KB Output is correct
5 Correct 151 ms 29432 KB Output is correct
6 Correct 132 ms 29364 KB Output is correct
7 Correct 144 ms 29276 KB Output is correct
8 Correct 154 ms 29356 KB Output is correct
9 Correct 131 ms 29304 KB Output is correct
10 Correct 145 ms 29356 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 5368 KB Output is correct
4 Correct 140 ms 29480 KB Output is correct
5 Correct 151 ms 29432 KB Output is correct
6 Correct 132 ms 29364 KB Output is correct
7 Correct 144 ms 29276 KB Output is correct
8 Correct 154 ms 29356 KB Output is correct
9 Correct 131 ms 29304 KB Output is correct
10 Correct 145 ms 29356 KB Output is correct
11 Correct 17 ms 5752 KB Output is correct
12 Correct 155 ms 29404 KB Output is correct
13 Correct 179 ms 29304 KB Output is correct
14 Correct 134 ms 29336 KB Output is correct
15 Correct 151 ms 29328 KB Output is correct
16 Correct 133 ms 29560 KB Output is correct
17 Correct 172 ms 29328 KB Output is correct
18 Correct 184 ms 29304 KB Output is correct
19 Correct 135 ms 29436 KB Output is correct
20 Correct 161 ms 29488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 21300 KB Output is correct
2 Correct 141 ms 29376 KB Output is correct
3 Correct 220 ms 26332 KB Output is correct
4 Correct 125 ms 21552 KB Output is correct
5 Correct 246 ms 25928 KB Output is correct
6 Correct 184 ms 21484 KB Output is correct
7 Correct 238 ms 25632 KB Output is correct
8 Correct 212 ms 21352 KB Output is correct
9 Correct 142 ms 29508 KB Output is correct
10 Correct 223 ms 24756 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 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 105 ms 19704 KB Output is correct
6 Correct 74 ms 27384 KB Output is correct
7 Correct 116 ms 24664 KB Output is correct
8 Correct 89 ms 20044 KB Output is correct
9 Correct 95 ms 23008 KB Output is correct
10 Correct 86 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 7 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 5288 KB Output is correct
19 Correct 7 ms 5240 KB Output is correct
20 Correct 7 ms 5368 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 6 ms 5112 KB Output is correct
4 Correct 7 ms 5240 KB Output is correct
5 Correct 105 ms 19704 KB Output is correct
6 Correct 74 ms 27384 KB Output is correct
7 Correct 116 ms 24664 KB Output is correct
8 Correct 89 ms 20044 KB Output is correct
9 Correct 95 ms 23008 KB Output is correct
10 Correct 86 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 5368 KB Output is correct
14 Correct 140 ms 29480 KB Output is correct
15 Correct 151 ms 29432 KB Output is correct
16 Correct 132 ms 29364 KB Output is correct
17 Correct 144 ms 29276 KB Output is correct
18 Correct 154 ms 29356 KB Output is correct
19 Correct 131 ms 29304 KB Output is correct
20 Correct 145 ms 29356 KB Output is correct
21 Correct 17 ms 5752 KB Output is correct
22 Correct 155 ms 29404 KB Output is correct
23 Correct 179 ms 29304 KB Output is correct
24 Correct 134 ms 29336 KB Output is correct
25 Correct 151 ms 29328 KB Output is correct
26 Correct 133 ms 29560 KB Output is correct
27 Correct 172 ms 29328 KB Output is correct
28 Correct 184 ms 29304 KB Output is correct
29 Correct 135 ms 29436 KB Output is correct
30 Correct 161 ms 29488 KB Output is correct
31 Correct 160 ms 21300 KB Output is correct
32 Correct 141 ms 29376 KB Output is correct
33 Correct 220 ms 26332 KB Output is correct
34 Correct 125 ms 21552 KB Output is correct
35 Correct 246 ms 25928 KB Output is correct
36 Correct 184 ms 21484 KB Output is correct
37 Correct 238 ms 25632 KB Output is correct
38 Correct 212 ms 21352 KB Output is correct
39 Correct 142 ms 29508 KB Output is correct
40 Correct 223 ms 24756 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 7 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 5288 KB Output is correct
49 Correct 7 ms 5240 KB Output is correct
50 Correct 7 ms 5368 KB Output is correct
51 Correct 185 ms 21396 KB Output is correct
52 Correct 146 ms 29612 KB Output is correct
53 Correct 240 ms 24580 KB Output is correct
54 Correct 150 ms 21552 KB Output is correct
55 Correct 173 ms 21140 KB Output is correct
56 Correct 177 ms 29352 KB Output is correct
57 Correct 207 ms 25428 KB Output is correct
58 Correct 138 ms 21548 KB Output is correct
59 Correct 179 ms 21432 KB Output is correct
60 Correct 158 ms 29432 KB Output is correct
61 Correct 226 ms 25372 KB Output is correct
62 Correct 136 ms 21328 KB Output is correct
63 Correct 176 ms 21080 KB Output is correct
64 Correct 157 ms 29396 KB Output is correct
65 Correct 250 ms 25260 KB Output is correct
66 Correct 142 ms 21684 KB Output is correct
67 Correct 158 ms 21100 KB Output is correct
68 Correct 154 ms 29304 KB Output is correct
69 Correct 210 ms 24240 KB Output is correct
70 Correct 136 ms 21724 KB Output is correct