Submission #157694

# Submission time Handle Problem Language Result Execution time Memory
157694 2019-10-12T18:03:17 Z nvmdava Election Campaign (JOI15_election_campaign) C++17
100 / 100
244 ms 29552 KB
#include <bits/stdc++.h>
#define N 100005
using namespace std;
#define ll long long
#define ff first
#define ss second

ll bit[N];

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

inline 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 6 ms 5240 KB Output is correct
5 Correct 98 ms 19632 KB Output is correct
6 Correct 71 ms 27384 KB Output is correct
7 Correct 106 ms 24568 KB Output is correct
8 Correct 76 ms 20096 KB Output is correct
9 Correct 99 ms 23124 KB Output is correct
10 Correct 79 ms 20044 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 143 ms 29436 KB Output is correct
5 Correct 152 ms 29400 KB Output is correct
6 Correct 131 ms 29416 KB Output is correct
7 Correct 144 ms 29468 KB Output is correct
8 Correct 142 ms 29304 KB Output is correct
9 Correct 127 ms 29432 KB Output is correct
10 Correct 144 ms 29412 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 143 ms 29436 KB Output is correct
5 Correct 152 ms 29400 KB Output is correct
6 Correct 131 ms 29416 KB Output is correct
7 Correct 144 ms 29468 KB Output is correct
8 Correct 142 ms 29304 KB Output is correct
9 Correct 127 ms 29432 KB Output is correct
10 Correct 144 ms 29412 KB Output is correct
11 Correct 17 ms 5752 KB Output is correct
12 Correct 154 ms 29316 KB Output is correct
13 Correct 149 ms 29432 KB Output is correct
14 Correct 130 ms 29304 KB Output is correct
15 Correct 161 ms 29552 KB Output is correct
16 Correct 135 ms 29380 KB Output is correct
17 Correct 149 ms 29348 KB Output is correct
18 Correct 146 ms 29372 KB Output is correct
19 Correct 130 ms 29304 KB Output is correct
20 Correct 148 ms 29432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 21188 KB Output is correct
2 Correct 134 ms 29364 KB Output is correct
3 Correct 227 ms 26232 KB Output is correct
4 Correct 126 ms 21524 KB Output is correct
5 Correct 197 ms 25932 KB Output is correct
6 Correct 131 ms 21480 KB Output is correct
7 Correct 244 ms 25572 KB Output is correct
8 Correct 172 ms 21468 KB Output is correct
9 Correct 133 ms 29432 KB Output is correct
10 Correct 216 ms 24640 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 6 ms 5240 KB Output is correct
5 Correct 98 ms 19632 KB Output is correct
6 Correct 71 ms 27384 KB Output is correct
7 Correct 106 ms 24568 KB Output is correct
8 Correct 76 ms 20096 KB Output is correct
9 Correct 99 ms 23124 KB Output is correct
10 Correct 79 ms 20044 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 5368 KB Output is correct
14 Correct 7 ms 5240 KB Output is correct
15 Correct 7 ms 5244 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 5240 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 6 ms 5240 KB Output is correct
5 Correct 98 ms 19632 KB Output is correct
6 Correct 71 ms 27384 KB Output is correct
7 Correct 106 ms 24568 KB Output is correct
8 Correct 76 ms 20096 KB Output is correct
9 Correct 99 ms 23124 KB Output is correct
10 Correct 79 ms 20044 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 143 ms 29436 KB Output is correct
15 Correct 152 ms 29400 KB Output is correct
16 Correct 131 ms 29416 KB Output is correct
17 Correct 144 ms 29468 KB Output is correct
18 Correct 142 ms 29304 KB Output is correct
19 Correct 127 ms 29432 KB Output is correct
20 Correct 144 ms 29412 KB Output is correct
21 Correct 17 ms 5752 KB Output is correct
22 Correct 154 ms 29316 KB Output is correct
23 Correct 149 ms 29432 KB Output is correct
24 Correct 130 ms 29304 KB Output is correct
25 Correct 161 ms 29552 KB Output is correct
26 Correct 135 ms 29380 KB Output is correct
27 Correct 149 ms 29348 KB Output is correct
28 Correct 146 ms 29372 KB Output is correct
29 Correct 130 ms 29304 KB Output is correct
30 Correct 148 ms 29432 KB Output is correct
31 Correct 170 ms 21188 KB Output is correct
32 Correct 134 ms 29364 KB Output is correct
33 Correct 227 ms 26232 KB Output is correct
34 Correct 126 ms 21524 KB Output is correct
35 Correct 197 ms 25932 KB Output is correct
36 Correct 131 ms 21480 KB Output is correct
37 Correct 244 ms 25572 KB Output is correct
38 Correct 172 ms 21468 KB Output is correct
39 Correct 133 ms 29432 KB Output is correct
40 Correct 216 ms 24640 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 5368 KB Output is correct
44 Correct 7 ms 5240 KB Output is correct
45 Correct 7 ms 5244 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 5240 KB Output is correct
49 Correct 7 ms 5240 KB Output is correct
50 Correct 7 ms 5368 KB Output is correct
51 Correct 161 ms 21368 KB Output is correct
52 Correct 148 ms 29308 KB Output is correct
53 Correct 219 ms 24624 KB Output is correct
54 Correct 128 ms 21724 KB Output is correct
55 Correct 155 ms 21296 KB Output is correct
56 Correct 148 ms 29368 KB Output is correct
57 Correct 189 ms 25308 KB Output is correct
58 Correct 126 ms 21608 KB Output is correct
59 Correct 184 ms 21348 KB Output is correct
60 Correct 147 ms 29308 KB Output is correct
61 Correct 186 ms 25600 KB Output is correct
62 Correct 123 ms 21300 KB Output is correct
63 Correct 162 ms 21124 KB Output is correct
64 Correct 143 ms 29304 KB Output is correct
65 Correct 219 ms 25268 KB Output is correct
66 Correct 126 ms 21688 KB Output is correct
67 Correct 160 ms 21100 KB Output is correct
68 Correct 147 ms 29304 KB Output is correct
69 Correct 191 ms 24140 KB Output is correct
70 Correct 129 ms 21724 KB Output is correct