Submission #251697

# Submission time Handle Problem Language Result Execution time Memory
251697 2020-07-22T07:46:36 Z LifeHappen__ Election Campaign (JOI15_election_campaign) C++14
100 / 100
367 ms 53884 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define forinc(a, b, c) for(int a=b, _c=c; a<=_c; ++a)
#define fordec(a, b, c) for(int a=b, _c=c; a>=_c; --a)
#define forv(a, b) for(auto &a : b)
#define rep(i, n) forinc(i, 1, n)
#define repv(i, n) forinc(i, 0, n - 1)

#define pb push_back
#define eb emplace_back
#define all(a) begin(a), end(a)
#define fi first
#define se second

const int N=1e5+5;
int n,m,st[N],en[N],pd[N][32],dep[N],id;
struct pack {int u,v,c;};
vector<int> ad[N];
vector<pack> que[N];
int f[N], g[N], it[N<<2], lz[N<<2];
void push(int i){
    if(lz[i]){
        int val=lz[i];
        lz[i<<1]+=val; lz[i<<1|1]+=val;
        it[i<<1]+=val; it[i<<1|1]+=val;
        lz[i]=0;
    }
}
void upd(int i,int l,int r,int u,int v,int val){
    if(v<l || u>r) return;
    if(u<=l && r<=v) {
        it[i]+=val; lz[i]+=val;
        return;
    }
    int mid=(l+r)>>1;
    push(i);
    upd(i<<1, l, mid, u, v, val); upd(i<<1|1, mid+1, r, u, v, val);
    it[i] = it[i<<1] + it[i<<1|1];
}
int get(int i,int l,int r,int u,int v){
    if(v<l || u>r) return 0;
    if(u<=l && r<=v) return it[i];
    int mid=(l+r)>>1;
    push(i);
    return get(i<<1, l, mid, u, v) + get(i<<1|1, mid+1, r, u, v);
}
void dfs(int u,int par=-1){
    rep(i,20) pd[u][i]=pd[pd[u][i-1]][i-1];
    st[u]=++id;
    forv(v,ad[u]) if(v!=par){
        dep[v]=dep[u]+1;
        pd[v][0]=u;
        dfs(v,u);
    }
    en[u]=id;
}
int lca(int u,int v){
    if(dep[u]<dep[v]) swap(u,v);
    int unx=dep[u]-dep[v];
    fordec(i,20,0) if(unx>>i&1) u=pd[u][i];
    if(u==v) return u;
    fordec(i,20,0) if(pd[u][i]!=pd[v][i]) u=pd[u][i], v=pd[v][i];
    return pd[u][0];
}
bool in(int u,int v){return st[u]<=st[v] && en[u]>=en[v];}
void dfs1(int u,int par){
    forv(v,ad[u]) if(v!=par) {
        dfs1(v,u);
        g[u]+=f[v];
    }
    forv(v,ad[u]) if(v!=par){
        upd(1,1,n,st[v],en[v],g[u]-f[v]);
    }
    f[u]=g[u];
    forv(v,que[u]){
        int x=v.u, y=v.v, c=v.c;
        int hx=get(1,1,n,st[x],st[x]);
        int hy=get(1,1,n,st[y],st[y]);
        int cur=hx+hy+g[x]+g[y]-g[u]+c;
        f[u]=max(f[u],cur);
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    #define task "election"
    if(fopen(task".inp", "r")) freopen(task".inp", "r", stdin);
    cin>>n;
    rep(i,n-1){
        int u,v; cin>>u>>v;
        ad[u].pb(v); ad[v].pb(u);
    }
    dfs(1,0);
    cin>>m;
    forinc(i,1,m){
        int u,v,c; cin>>u>>v>>c;
        int p=lca(u,v);
        que[p].pb({u, v, c});
    }
    dfs1(1,0);
    cout<<f[1];
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:91:39: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     if(fopen(task".inp", "r")) freopen(task".inp", "r", stdin);
                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 5120 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 129 ms 43256 KB Output is correct
6 Correct 98 ms 48632 KB Output is correct
7 Correct 168 ms 46840 KB Output is correct
8 Correct 111 ms 42232 KB Output is correct
9 Correct 166 ms 45688 KB Output is correct
10 Correct 95 ms 42364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 4 ms 5504 KB Output is correct
4 Correct 222 ms 53608 KB Output is correct
5 Correct 223 ms 53500 KB Output is correct
6 Correct 207 ms 53496 KB Output is correct
7 Correct 243 ms 53752 KB Output is correct
8 Correct 245 ms 53496 KB Output is correct
9 Correct 221 ms 53540 KB Output is correct
10 Correct 255 ms 53496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 4 ms 5504 KB Output is correct
4 Correct 222 ms 53608 KB Output is correct
5 Correct 223 ms 53500 KB Output is correct
6 Correct 207 ms 53496 KB Output is correct
7 Correct 243 ms 53752 KB Output is correct
8 Correct 245 ms 53496 KB Output is correct
9 Correct 221 ms 53540 KB Output is correct
10 Correct 255 ms 53496 KB Output is correct
11 Correct 16 ms 6656 KB Output is correct
12 Correct 234 ms 53880 KB Output is correct
13 Correct 230 ms 53752 KB Output is correct
14 Correct 193 ms 53880 KB Output is correct
15 Correct 228 ms 53832 KB Output is correct
16 Correct 196 ms 53752 KB Output is correct
17 Correct 225 ms 53752 KB Output is correct
18 Correct 226 ms 53788 KB Output is correct
19 Correct 203 ms 53812 KB Output is correct
20 Correct 228 ms 53752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 244 ms 47532 KB Output is correct
2 Correct 200 ms 53500 KB Output is correct
3 Correct 336 ms 51508 KB Output is correct
4 Correct 196 ms 47020 KB Output is correct
5 Correct 329 ms 50928 KB Output is correct
6 Correct 224 ms 46948 KB Output is correct
7 Correct 367 ms 50608 KB Output is correct
8 Correct 294 ms 47608 KB Output is correct
9 Correct 214 ms 53496 KB Output is correct
10 Correct 326 ms 49708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 5120 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 129 ms 43256 KB Output is correct
6 Correct 98 ms 48632 KB Output is correct
7 Correct 168 ms 46840 KB Output is correct
8 Correct 111 ms 42232 KB Output is correct
9 Correct 166 ms 45688 KB Output is correct
10 Correct 95 ms 42364 KB Output is correct
11 Correct 4 ms 5504 KB Output is correct
12 Correct 5 ms 5504 KB Output is correct
13 Correct 4 ms 5504 KB Output is correct
14 Correct 6 ms 5504 KB Output is correct
15 Correct 4 ms 5504 KB Output is correct
16 Correct 5 ms 5504 KB Output is correct
17 Correct 4 ms 5504 KB Output is correct
18 Correct 6 ms 5504 KB Output is correct
19 Correct 6 ms 5504 KB Output is correct
20 Correct 4 ms 5504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 5120 KB Output is correct
4 Correct 4 ms 5504 KB Output is correct
5 Correct 129 ms 43256 KB Output is correct
6 Correct 98 ms 48632 KB Output is correct
7 Correct 168 ms 46840 KB Output is correct
8 Correct 111 ms 42232 KB Output is correct
9 Correct 166 ms 45688 KB Output is correct
10 Correct 95 ms 42364 KB Output is correct
11 Correct 3 ms 5120 KB Output is correct
12 Correct 3 ms 5120 KB Output is correct
13 Correct 4 ms 5504 KB Output is correct
14 Correct 222 ms 53608 KB Output is correct
15 Correct 223 ms 53500 KB Output is correct
16 Correct 207 ms 53496 KB Output is correct
17 Correct 243 ms 53752 KB Output is correct
18 Correct 245 ms 53496 KB Output is correct
19 Correct 221 ms 53540 KB Output is correct
20 Correct 255 ms 53496 KB Output is correct
21 Correct 16 ms 6656 KB Output is correct
22 Correct 234 ms 53880 KB Output is correct
23 Correct 230 ms 53752 KB Output is correct
24 Correct 193 ms 53880 KB Output is correct
25 Correct 228 ms 53832 KB Output is correct
26 Correct 196 ms 53752 KB Output is correct
27 Correct 225 ms 53752 KB Output is correct
28 Correct 226 ms 53788 KB Output is correct
29 Correct 203 ms 53812 KB Output is correct
30 Correct 228 ms 53752 KB Output is correct
31 Correct 244 ms 47532 KB Output is correct
32 Correct 200 ms 53500 KB Output is correct
33 Correct 336 ms 51508 KB Output is correct
34 Correct 196 ms 47020 KB Output is correct
35 Correct 329 ms 50928 KB Output is correct
36 Correct 224 ms 46948 KB Output is correct
37 Correct 367 ms 50608 KB Output is correct
38 Correct 294 ms 47608 KB Output is correct
39 Correct 214 ms 53496 KB Output is correct
40 Correct 326 ms 49708 KB Output is correct
41 Correct 4 ms 5504 KB Output is correct
42 Correct 5 ms 5504 KB Output is correct
43 Correct 4 ms 5504 KB Output is correct
44 Correct 6 ms 5504 KB Output is correct
45 Correct 4 ms 5504 KB Output is correct
46 Correct 5 ms 5504 KB Output is correct
47 Correct 4 ms 5504 KB Output is correct
48 Correct 6 ms 5504 KB Output is correct
49 Correct 6 ms 5504 KB Output is correct
50 Correct 4 ms 5504 KB Output is correct
51 Correct 266 ms 48092 KB Output is correct
52 Correct 257 ms 53752 KB Output is correct
53 Correct 358 ms 50348 KB Output is correct
54 Correct 181 ms 47084 KB Output is correct
55 Correct 235 ms 47912 KB Output is correct
56 Correct 230 ms 53880 KB Output is correct
57 Correct 280 ms 50996 KB Output is correct
58 Correct 184 ms 47148 KB Output is correct
59 Correct 236 ms 47856 KB Output is correct
60 Correct 225 ms 53756 KB Output is correct
61 Correct 288 ms 51000 KB Output is correct
62 Correct 188 ms 46792 KB Output is correct
63 Correct 244 ms 47368 KB Output is correct
64 Correct 229 ms 53884 KB Output is correct
65 Correct 338 ms 50604 KB Output is correct
66 Correct 198 ms 47016 KB Output is correct
67 Correct 237 ms 47596 KB Output is correct
68 Correct 222 ms 53752 KB Output is correct
69 Correct 281 ms 49844 KB Output is correct
70 Correct 200 ms 46732 KB Output is correct