답안 #399213

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
399213 2021-05-05T12:27:59 Z keta_tsimakuridze Election Campaign (JOI15_election_campaign) C++14
100 / 100
429 ms 43196 KB
#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=1e5+5,mod=1e9+7;
int t,tmin[N],tmout[N],tree[4*N],g[2],timer,dp[N][2],n,m,ans,par[N][20],Lg;
vector<int>V[N];
vector<pair<int,pair<int,int> > >q[N];
string s;
void dfs(int u,int p) {
	timer++;
	tmin[u]=timer;	
	par[u][0]=p;
	for(int i=1;i<=Lg;i++) par[u][i]=par[par[u][i-1]][i-1];
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]==p) continue;
		dfs(V[u][i],u);
	}
	tmout[u]=timer;
}
bool check(int u,int v){
	if(tmin[u]<=tmin[v] && tmout[u]>=tmout[v]) return 1;
	return 0;
}
int find_lca(int u,int v){
	if(check(u,v)) return u;
	if(check(v,u)) return v;
	for(int i=Lg;i>=0;i--){
		if(par[u][i] && !check(par[u][i],v)) u=par[u][i];
	}
	return par[u][0];
}
int up(int u,int v){
	for(int i=Lg;i>=0;i--){
		if(par[v][i] && !check(par[v][i],u)) v=par[v][i];
	}
	return v;
}
void update(int u,int start,int end,int l,int r,int val){
	if(l>end || r<start) return;
	if(start<=l && r<=end) {
		tree[u]+=val;
		return;
	}
	int mid=(l+r)/2;
	update(2*u,start,end,l,mid,val);
	update(2*u+1,start,end,mid+1,r,val);
}
void getans(int u,int ind,int l,int r,int b){
	if(l>ind || r<ind) return;
	g[b]+=tree[u];
	if(l==r) return;
	int mid=(l+r)/2;
	getans(2*u,ind,l,mid,b);
	getans(2*u+1,ind,mid+1,r,b);
}
void solve(int u,int p){ 
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]==p) continue;
		solve(V[u][i],u);
		dp[u][0]+=dp[V[u][i]][1];
	}
	dp[u][1]=dp[u][0];
	for(int i=0;i<q[u].size();i++){
		int par1,par2,c=0;	
		g[1]=g[0]= 0; 	
		getans(1,tmin[q[u][i].s.f],1,n,0);  
		getans(1,tmin[q[u][i].s.s],1,n,1);
		if(q[u][i].s.f==u) par1 = 0;
		else c+=g[0],par1 = up(u,q[u][i].s.f); 
		if(q[u][i].s.s==u) par2 = 0;
		else c+=g[1],par2 = up(u,q[u][i].s.s);
		dp[u][1] = max(dp[u][1],q[u][i].f+c+dp[u][0] - dp[par1][1] - dp[par2][1]);
		ans=max(ans,dp[u][1]);
	} 
	for(int i=0;i<V[u].size();i++){
		if(V[u][i]==p) continue;
		update(1,tmin[V[u][i]],tmout[V[u][i]],1,n,dp[u][0]-dp[V[u][i]][1]);
	} 
	update(1,tmin[u],tmin[u],1,n,dp[u][0]);
}
 main(){
	// t=1;
	cin >> n;
	Lg=log2(n)+1;
	for(int i=2;i<=n;i++){
		int u,v;
		cin>>u>>v;
		V[u].push_back(v);
		V[v].push_back(u);
	}
	dfs(1,0);
	cin>>m;
	for(int i=1;i<=m;i++){
		int u,v,c;
		cin>>u>>v>>c; 
		q[find_lca(u,v)].push_back({c,{u,v}});
	}
	solve(1,0);
	cout<<ans;
}

Compilation message

election_campaign.cpp: In function 'void dfs(long long int, long long int)':
election_campaign.cpp:16:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
election_campaign.cpp: In function 'void solve(long long int, long long int)':
election_campaign.cpp:59:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
election_campaign.cpp:65:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i=0;i<q[u].size();i++){
      |              ~^~~~~~~~~~~~
election_campaign.cpp:77:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
election_campaign.cpp: At global scope:
election_campaign.cpp:83:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   83 |  main(){
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 177 ms 30836 KB Output is correct
6 Correct 118 ms 37828 KB Output is correct
7 Correct 194 ms 35568 KB Output is correct
8 Correct 147 ms 30656 KB Output is correct
9 Correct 181 ms 34016 KB Output is correct
10 Correct 150 ms 30744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 268 ms 42720 KB Output is correct
5 Correct 273 ms 42824 KB Output is correct
6 Correct 248 ms 42784 KB Output is correct
7 Correct 274 ms 42696 KB Output is correct
8 Correct 266 ms 42688 KB Output is correct
9 Correct 245 ms 42780 KB Output is correct
10 Correct 275 ms 42712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 268 ms 42720 KB Output is correct
5 Correct 273 ms 42824 KB Output is correct
6 Correct 248 ms 42784 KB Output is correct
7 Correct 274 ms 42696 KB Output is correct
8 Correct 266 ms 42688 KB Output is correct
9 Correct 245 ms 42780 KB Output is correct
10 Correct 275 ms 42712 KB Output is correct
11 Correct 28 ms 6480 KB Output is correct
12 Correct 281 ms 42996 KB Output is correct
13 Correct 280 ms 43016 KB Output is correct
14 Correct 265 ms 43080 KB Output is correct
15 Correct 282 ms 43196 KB Output is correct
16 Correct 262 ms 43076 KB Output is correct
17 Correct 273 ms 43024 KB Output is correct
18 Correct 285 ms 43008 KB Output is correct
19 Correct 259 ms 43068 KB Output is correct
20 Correct 281 ms 43076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 332 ms 35120 KB Output is correct
2 Correct 251 ms 42756 KB Output is correct
3 Correct 418 ms 40048 KB Output is correct
4 Correct 290 ms 35196 KB Output is correct
5 Correct 364 ms 39476 KB Output is correct
6 Correct 285 ms 34868 KB Output is correct
7 Correct 428 ms 39164 KB Output is correct
8 Correct 317 ms 35140 KB Output is correct
9 Correct 252 ms 42960 KB Output is correct
10 Correct 413 ms 38012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 177 ms 30836 KB Output is correct
6 Correct 118 ms 37828 KB Output is correct
7 Correct 194 ms 35568 KB Output is correct
8 Correct 147 ms 30656 KB Output is correct
9 Correct 181 ms 34016 KB Output is correct
10 Correct 150 ms 30744 KB Output is correct
11 Correct 5 ms 5272 KB Output is correct
12 Correct 5 ms 5276 KB Output is correct
13 Correct 5 ms 5324 KB Output is correct
14 Correct 5 ms 5324 KB Output is correct
15 Correct 6 ms 5272 KB Output is correct
16 Correct 6 ms 5324 KB Output is correct
17 Correct 6 ms 5276 KB Output is correct
18 Correct 6 ms 5324 KB Output is correct
19 Correct 6 ms 5324 KB Output is correct
20 Correct 5 ms 5280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 5196 KB Output is correct
5 Correct 177 ms 30836 KB Output is correct
6 Correct 118 ms 37828 KB Output is correct
7 Correct 194 ms 35568 KB Output is correct
8 Correct 147 ms 30656 KB Output is correct
9 Correct 181 ms 34016 KB Output is correct
10 Correct 150 ms 30744 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 4 ms 5324 KB Output is correct
14 Correct 268 ms 42720 KB Output is correct
15 Correct 273 ms 42824 KB Output is correct
16 Correct 248 ms 42784 KB Output is correct
17 Correct 274 ms 42696 KB Output is correct
18 Correct 266 ms 42688 KB Output is correct
19 Correct 245 ms 42780 KB Output is correct
20 Correct 275 ms 42712 KB Output is correct
21 Correct 28 ms 6480 KB Output is correct
22 Correct 281 ms 42996 KB Output is correct
23 Correct 280 ms 43016 KB Output is correct
24 Correct 265 ms 43080 KB Output is correct
25 Correct 282 ms 43196 KB Output is correct
26 Correct 262 ms 43076 KB Output is correct
27 Correct 273 ms 43024 KB Output is correct
28 Correct 285 ms 43008 KB Output is correct
29 Correct 259 ms 43068 KB Output is correct
30 Correct 281 ms 43076 KB Output is correct
31 Correct 332 ms 35120 KB Output is correct
32 Correct 251 ms 42756 KB Output is correct
33 Correct 418 ms 40048 KB Output is correct
34 Correct 290 ms 35196 KB Output is correct
35 Correct 364 ms 39476 KB Output is correct
36 Correct 285 ms 34868 KB Output is correct
37 Correct 428 ms 39164 KB Output is correct
38 Correct 317 ms 35140 KB Output is correct
39 Correct 252 ms 42960 KB Output is correct
40 Correct 413 ms 38012 KB Output is correct
41 Correct 5 ms 5272 KB Output is correct
42 Correct 5 ms 5276 KB Output is correct
43 Correct 5 ms 5324 KB Output is correct
44 Correct 5 ms 5324 KB Output is correct
45 Correct 6 ms 5272 KB Output is correct
46 Correct 6 ms 5324 KB Output is correct
47 Correct 6 ms 5276 KB Output is correct
48 Correct 6 ms 5324 KB Output is correct
49 Correct 6 ms 5324 KB Output is correct
50 Correct 5 ms 5280 KB Output is correct
51 Correct 328 ms 35528 KB Output is correct
52 Correct 283 ms 42984 KB Output is correct
53 Correct 421 ms 38460 KB Output is correct
54 Correct 303 ms 35408 KB Output is correct
55 Correct 355 ms 35260 KB Output is correct
56 Correct 280 ms 42988 KB Output is correct
57 Correct 382 ms 39152 KB Output is correct
58 Correct 305 ms 35320 KB Output is correct
59 Correct 329 ms 35448 KB Output is correct
60 Correct 276 ms 42944 KB Output is correct
61 Correct 372 ms 39296 KB Output is correct
62 Correct 296 ms 34832 KB Output is correct
63 Correct 334 ms 35036 KB Output is correct
64 Correct 280 ms 43076 KB Output is correct
65 Correct 429 ms 39064 KB Output is correct
66 Correct 287 ms 35060 KB Output is correct
67 Correct 327 ms 35180 KB Output is correct
68 Correct 277 ms 43028 KB Output is correct
69 Correct 367 ms 38052 KB Output is correct
70 Correct 302 ms 35180 KB Output is correct