Submission #527851

# Submission time Handle Problem Language Result Execution time Memory
527851 2022-02-18T13:56:53 Z Koosha_mv Election Campaign (JOI15_election_campaign) C++14
100 / 100
425 ms 64196 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll

const int N=4e5+99,lg=20;

int n,m,cnt=1,st[N],ft[N],h[N],a[N],dp[N],pd[N],par[lg][N],fenwik[N];
vector<int> emp,g[N];
vector<pair<int,int>> solo[N];
vector<pair<pair<int,int>,int>> duo[N];

void add(int x,int val){
	for(x++;x<N;x+=x&-x) fenwik[x]+=val;
}
void add(int l,int r,int val){
	add(l,val);
	add(r,-val);
}
int get(int x){
	int res=0;
	for(x++;x>0;x-=x&-x) res+=fenwik[x];
	return res;
}
int LCA(int u,int v){
	f_(l,lg-1,0){
		if(h[u]<=h[par[l][v]]){
			v=par[l][v];
		}
	}
	if(u==v) return u;
	f_(l,lg-1,0){
		if(par[l][u]!=par[l][v]){
			u=par[l][u],v=par[l][v];
		}
	}
	return par[0][u];
}
void dfs1(int u,int p){
	par[0][u]=p; f(l,1,lg) par[l][u]=par[l-1][par[l-1][u]];
	st[u]=cnt++;
	for(auto v : g[u]){
		if(v==p) continue ;
		h[v]=h[u]+1;
		dfs1(v,u);
	}
	ft[u]=cnt;
}
void dfs2(int u,int p){
	for(auto v : g[u]){
		if(v==p) continue ;
		dfs2(v,u);
		dp[u]+=dp[v];
		pd[u]+=dp[v];
		add(st[u]+1,ft[u],dp[v]);
		add(st[v],ft[v],-dp[v]);
	}
	for(auto v : solo[u]){
		maxm(dp[u],get(st[v.F])+pd[v.F]+v.S-get(st[u]));
	}
	for(auto v : duo[u]){
		int sum=pd[v.F.F]+pd[v.F.S];
		maxm(dp[u],get(st[v.F.F])+get(st[v.F.S])-get(st[u])-get(st[u])-pd[u]+sum+v.S);
	}
}
main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	cin>>n;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs1(1,1);
	cin>>m;
	f(i,0,m){
		int u,v,x=0,y=0,cost;
		cin>>u>>v>>cost;
		if(h[u]>h[v]) swap(u,v);
		int lca=LCA(u,v);
		if(lca==u){
			solo[u].pb({v,cost});
			continue ;
		}
		duo[lca].pb({{u,v},cost});
	}
	dfs2(1,1);
	cout<<dp[1];
}

Compilation message

election_campaign.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   84 | main(){
      | ^~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:96:11: warning: unused variable 'x' [-Wunused-variable]
   96 |   int u,v,x=0,y=0,cost;
      |           ^
election_campaign.cpp:96:15: warning: unused variable 'y' [-Wunused-variable]
   96 |   int u,v,x=0,y=0,cost;
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28748 KB Output is correct
2 Correct 15 ms 28748 KB Output is correct
3 Correct 14 ms 28748 KB Output is correct
4 Correct 15 ms 28928 KB Output is correct
5 Correct 114 ms 52932 KB Output is correct
6 Correct 60 ms 61488 KB Output is correct
7 Correct 127 ms 58740 KB Output is correct
8 Correct 93 ms 51556 KB Output is correct
9 Correct 112 ms 56820 KB Output is correct
10 Correct 100 ms 51588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28748 KB Output is correct
2 Correct 14 ms 28748 KB Output is correct
3 Correct 15 ms 29072 KB Output is correct
4 Correct 177 ms 64056 KB Output is correct
5 Correct 160 ms 64064 KB Output is correct
6 Correct 166 ms 64072 KB Output is correct
7 Correct 198 ms 64080 KB Output is correct
8 Correct 173 ms 64164 KB Output is correct
9 Correct 169 ms 64116 KB Output is correct
10 Correct 162 ms 64124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28748 KB Output is correct
2 Correct 14 ms 28748 KB Output is correct
3 Correct 15 ms 29072 KB Output is correct
4 Correct 177 ms 64056 KB Output is correct
5 Correct 160 ms 64064 KB Output is correct
6 Correct 166 ms 64072 KB Output is correct
7 Correct 198 ms 64080 KB Output is correct
8 Correct 173 ms 64164 KB Output is correct
9 Correct 169 ms 64116 KB Output is correct
10 Correct 162 ms 64124 KB Output is correct
11 Correct 25 ms 29644 KB Output is correct
12 Correct 163 ms 64096 KB Output is correct
13 Correct 165 ms 64076 KB Output is correct
14 Correct 201 ms 64168 KB Output is correct
15 Correct 163 ms 64100 KB Output is correct
16 Correct 179 ms 64156 KB Output is correct
17 Correct 170 ms 64140 KB Output is correct
18 Correct 186 ms 64148 KB Output is correct
19 Correct 170 ms 64196 KB Output is correct
20 Correct 169 ms 64068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 390 ms 55820 KB Output is correct
2 Correct 178 ms 64192 KB Output is correct
3 Correct 403 ms 61168 KB Output is correct
4 Correct 243 ms 54776 KB Output is correct
5 Correct 365 ms 60236 KB Output is correct
6 Correct 230 ms 54728 KB Output is correct
7 Correct 387 ms 60280 KB Output is correct
8 Correct 373 ms 55408 KB Output is correct
9 Correct 167 ms 64180 KB Output is correct
10 Correct 402 ms 59248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28748 KB Output is correct
2 Correct 15 ms 28748 KB Output is correct
3 Correct 14 ms 28748 KB Output is correct
4 Correct 15 ms 28928 KB Output is correct
5 Correct 114 ms 52932 KB Output is correct
6 Correct 60 ms 61488 KB Output is correct
7 Correct 127 ms 58740 KB Output is correct
8 Correct 93 ms 51556 KB Output is correct
9 Correct 112 ms 56820 KB Output is correct
10 Correct 100 ms 51588 KB Output is correct
11 Correct 16 ms 29004 KB Output is correct
12 Correct 16 ms 29068 KB Output is correct
13 Correct 16 ms 29004 KB Output is correct
14 Correct 15 ms 28948 KB Output is correct
15 Correct 16 ms 29020 KB Output is correct
16 Correct 15 ms 29004 KB Output is correct
17 Correct 15 ms 28940 KB Output is correct
18 Correct 15 ms 29004 KB Output is correct
19 Correct 16 ms 28976 KB Output is correct
20 Correct 15 ms 29004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28748 KB Output is correct
2 Correct 15 ms 28748 KB Output is correct
3 Correct 14 ms 28748 KB Output is correct
4 Correct 15 ms 28928 KB Output is correct
5 Correct 114 ms 52932 KB Output is correct
6 Correct 60 ms 61488 KB Output is correct
7 Correct 127 ms 58740 KB Output is correct
8 Correct 93 ms 51556 KB Output is correct
9 Correct 112 ms 56820 KB Output is correct
10 Correct 100 ms 51588 KB Output is correct
11 Correct 14 ms 28748 KB Output is correct
12 Correct 14 ms 28748 KB Output is correct
13 Correct 15 ms 29072 KB Output is correct
14 Correct 177 ms 64056 KB Output is correct
15 Correct 160 ms 64064 KB Output is correct
16 Correct 166 ms 64072 KB Output is correct
17 Correct 198 ms 64080 KB Output is correct
18 Correct 173 ms 64164 KB Output is correct
19 Correct 169 ms 64116 KB Output is correct
20 Correct 162 ms 64124 KB Output is correct
21 Correct 25 ms 29644 KB Output is correct
22 Correct 163 ms 64096 KB Output is correct
23 Correct 165 ms 64076 KB Output is correct
24 Correct 201 ms 64168 KB Output is correct
25 Correct 163 ms 64100 KB Output is correct
26 Correct 179 ms 64156 KB Output is correct
27 Correct 170 ms 64140 KB Output is correct
28 Correct 186 ms 64148 KB Output is correct
29 Correct 170 ms 64196 KB Output is correct
30 Correct 169 ms 64068 KB Output is correct
31 Correct 390 ms 55820 KB Output is correct
32 Correct 178 ms 64192 KB Output is correct
33 Correct 403 ms 61168 KB Output is correct
34 Correct 243 ms 54776 KB Output is correct
35 Correct 365 ms 60236 KB Output is correct
36 Correct 230 ms 54728 KB Output is correct
37 Correct 387 ms 60280 KB Output is correct
38 Correct 373 ms 55408 KB Output is correct
39 Correct 167 ms 64180 KB Output is correct
40 Correct 402 ms 59248 KB Output is correct
41 Correct 16 ms 29004 KB Output is correct
42 Correct 16 ms 29068 KB Output is correct
43 Correct 16 ms 29004 KB Output is correct
44 Correct 15 ms 28948 KB Output is correct
45 Correct 16 ms 29020 KB Output is correct
46 Correct 15 ms 29004 KB Output is correct
47 Correct 15 ms 28940 KB Output is correct
48 Correct 15 ms 29004 KB Output is correct
49 Correct 16 ms 28976 KB Output is correct
50 Correct 15 ms 29004 KB Output is correct
51 Correct 367 ms 55392 KB Output is correct
52 Correct 187 ms 64140 KB Output is correct
53 Correct 404 ms 59348 KB Output is correct
54 Correct 311 ms 53920 KB Output is correct
55 Correct 353 ms 55764 KB Output is correct
56 Correct 168 ms 64164 KB Output is correct
57 Correct 376 ms 59700 KB Output is correct
58 Correct 212 ms 54624 KB Output is correct
59 Correct 380 ms 55500 KB Output is correct
60 Correct 164 ms 64032 KB Output is correct
61 Correct 373 ms 60060 KB Output is correct
62 Correct 225 ms 54140 KB Output is correct
63 Correct 355 ms 55596 KB Output is correct
64 Correct 198 ms 64088 KB Output is correct
65 Correct 425 ms 59988 KB Output is correct
66 Correct 280 ms 53940 KB Output is correct
67 Correct 359 ms 55740 KB Output is correct
68 Correct 164 ms 64068 KB Output is correct
69 Correct 420 ms 58436 KB Output is correct
70 Correct 221 ms 54508 KB Output is correct