답안 #187683

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
187683 2020-01-13T04:08:55 Z dndhk Election Campaign (JOI15_election_campaign) C++14
100 / 100
676 ms 61896 KB
#include <bits/stdc++.h>

#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 100000;
const ll INFLL = 10000;
const int MAX_K = 20;
int N, M;
vector<int> gp[MAX_N+1];
int lv[MAX_N+1], p[MAX_N+1], up[MAX_N+1][MAX_K+1];
vector<pii> vt[MAX_N+1];

void dfs(int x){
	up[x][0] = p[x];
	for(int i=1; i<MAX_K; i++)	up[x][i] = up[up[x][i-1]][i-1];
	for(int i=0; i<MAX_K; i++)	vt[up[x][i]].pb({x, i});
	for(int i : gp[x]){
		if(i==p[x])	continue;
		lv[i] = lv[x]+1;
		p[i] = x;
		dfs(i);
	}
}

int lca(int x, int y){
	for(int i=MAX_K-1; i>=0; i--){
		if(lv[up[x][i]]>=lv[y])	x = up[x][i];
		if(lv[up[y][i]]>=lv[x])	y = up[y][i];
	}
	if(x==y)	return x;
	for(int i=MAX_K-1; i>=0; i--){
		if(up[x][i]!=up[y][i])	{
			x = up[x][i];
			y = up[y][i];
		}
	}
	return up[x][0];
}

int dist(int x, int y){
	int ret = 0;
	for(int i=MAX_K-1; i>=0; i--){
		if(lv[up[x][i]]>=lv[y])	{
			x = up[x][i];
			ret+=(1<<i);
		}
		if(lv[up[y][i]]>=lv[x])	{
			y = up[y][i];
			ret+=(1<<i);
		}
	}
	if(x==y)	return ret;
	for(int i=MAX_K-1; i>=0; i--){
		if(up[x][i]!=up[y][i])	{
			x = up[x][i];
			y = up[y][i];
			ret+=(1<<(i+1));
		}
	}
	return ret+2;
}

int upk(int x, int y){
	for(int i=MAX_K-1; i>=0; i--){
		if(y>=(1<<i)){
			x = up[x][i];
			y-=(1<<i);
		}
	}
	return x;
}

struct Q{
	int a, b, c;
};
vector<Q> query[MAX_N+1];
int dp1[MAX_N+1], dp2[MAX_N+1], dp3[MAX_N+1];
int cost[MAX_N+1][MAX_K+1];

void calc(int x, int y){
	if(y==0){
		cost[x][y] = dp2[x]-dp1[x];
	}else{
		cost[x][y] = cost[x][y-1] + cost[up[x][y-1]][y-1];
	}
}

int calc2(int x, int y){
	int ret = 0;
	for(int i=MAX_K-1; i>=0; i--){
		if(lv[up[x][i]]>=lv[y]){
			ret += cost[x][i];
			x = up[x][i];
		}
	}
	return ret;
}

void dfs2(int x){
	for(int i : gp[x]){
		if(i==p[x])	continue;
		dfs2(i);
		dp2[x]+=dp1[i];
	}
	for(pii p : vt[x]){
		calc(p.first, p.second);
	}
	dp3[x] = 0;
	for(Q q : query[x]){
		int sum = dp2[x];
		if(q.a!=x)	sum+=calc2(q.a, x);
		if(q.b!=x)	sum+=calc2(q.b, x);
		dp3[x] = max(dp3[x], sum+q.c);
	}
	dp1[x] = max(dp2[x], dp3[x]);

}

int main(){
	scanf("%d", &N);
	for(int i=1; i<N; i++){
		int a, b; scanf("%d%d", &a, &b);
		gp[a].pb(b); gp[b].pb(a);
	} 
	lv[1] = 1;dfs(1);
	scanf("%d", &M);
	for(int i=1; i<=M; i++){
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		query[lca(a, b)].pb({a, b, c});
	}
	dfs2(1);
	cout<<dp1[1];
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:128:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b; scanf("%d%d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:132:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:134:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int a, b, c; scanf("%d%d%d", &a, &b, &c);
                ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7416 KB Output is correct
4 Correct 10 ms 7928 KB Output is correct
5 Correct 254 ms 47788 KB Output is correct
6 Correct 237 ms 58068 KB Output is correct
7 Correct 371 ms 57044 KB Output is correct
8 Correct 160 ms 46692 KB Output is correct
9 Correct 348 ms 55600 KB Output is correct
10 Correct 160 ms 46584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7928 KB Output is correct
4 Correct 375 ms 61464 KB Output is correct
5 Correct 339 ms 61564 KB Output is correct
6 Correct 330 ms 61496 KB Output is correct
7 Correct 355 ms 61492 KB Output is correct
8 Correct 361 ms 61380 KB Output is correct
9 Correct 407 ms 61512 KB Output is correct
10 Correct 444 ms 61532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7928 KB Output is correct
4 Correct 375 ms 61464 KB Output is correct
5 Correct 339 ms 61564 KB Output is correct
6 Correct 330 ms 61496 KB Output is correct
7 Correct 355 ms 61492 KB Output is correct
8 Correct 361 ms 61380 KB Output is correct
9 Correct 407 ms 61512 KB Output is correct
10 Correct 444 ms 61532 KB Output is correct
11 Correct 31 ms 8824 KB Output is correct
12 Correct 359 ms 61788 KB Output is correct
13 Correct 398 ms 61896 KB Output is correct
14 Correct 340 ms 61788 KB Output is correct
15 Correct 367 ms 61696 KB Output is correct
16 Correct 331 ms 61736 KB Output is correct
17 Correct 358 ms 61788 KB Output is correct
18 Correct 353 ms 61828 KB Output is correct
19 Correct 329 ms 61744 KB Output is correct
20 Correct 360 ms 61880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 334 ms 50760 KB Output is correct
2 Correct 330 ms 61424 KB Output is correct
3 Correct 574 ms 58960 KB Output is correct
4 Correct 250 ms 49356 KB Output is correct
5 Correct 524 ms 58712 KB Output is correct
6 Correct 238 ms 49472 KB Output is correct
7 Correct 587 ms 58728 KB Output is correct
8 Correct 334 ms 51164 KB Output is correct
9 Correct 327 ms 61608 KB Output is correct
10 Correct 589 ms 58244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7416 KB Output is correct
4 Correct 10 ms 7928 KB Output is correct
5 Correct 254 ms 47788 KB Output is correct
6 Correct 237 ms 58068 KB Output is correct
7 Correct 371 ms 57044 KB Output is correct
8 Correct 160 ms 46692 KB Output is correct
9 Correct 348 ms 55600 KB Output is correct
10 Correct 160 ms 46584 KB Output is correct
11 Correct 10 ms 7928 KB Output is correct
12 Correct 11 ms 7928 KB Output is correct
13 Correct 15 ms 7928 KB Output is correct
14 Correct 11 ms 8056 KB Output is correct
15 Correct 9 ms 7932 KB Output is correct
16 Correct 16 ms 7928 KB Output is correct
17 Correct 20 ms 8056 KB Output is correct
18 Correct 11 ms 7928 KB Output is correct
19 Correct 11 ms 7928 KB Output is correct
20 Correct 11 ms 7928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 8 ms 7416 KB Output is correct
4 Correct 10 ms 7928 KB Output is correct
5 Correct 254 ms 47788 KB Output is correct
6 Correct 237 ms 58068 KB Output is correct
7 Correct 371 ms 57044 KB Output is correct
8 Correct 160 ms 46692 KB Output is correct
9 Correct 348 ms 55600 KB Output is correct
10 Correct 160 ms 46584 KB Output is correct
11 Correct 8 ms 7416 KB Output is correct
12 Correct 8 ms 7416 KB Output is correct
13 Correct 8 ms 7928 KB Output is correct
14 Correct 375 ms 61464 KB Output is correct
15 Correct 339 ms 61564 KB Output is correct
16 Correct 330 ms 61496 KB Output is correct
17 Correct 355 ms 61492 KB Output is correct
18 Correct 361 ms 61380 KB Output is correct
19 Correct 407 ms 61512 KB Output is correct
20 Correct 444 ms 61532 KB Output is correct
21 Correct 31 ms 8824 KB Output is correct
22 Correct 359 ms 61788 KB Output is correct
23 Correct 398 ms 61896 KB Output is correct
24 Correct 340 ms 61788 KB Output is correct
25 Correct 367 ms 61696 KB Output is correct
26 Correct 331 ms 61736 KB Output is correct
27 Correct 358 ms 61788 KB Output is correct
28 Correct 353 ms 61828 KB Output is correct
29 Correct 329 ms 61744 KB Output is correct
30 Correct 360 ms 61880 KB Output is correct
31 Correct 334 ms 50760 KB Output is correct
32 Correct 330 ms 61424 KB Output is correct
33 Correct 574 ms 58960 KB Output is correct
34 Correct 250 ms 49356 KB Output is correct
35 Correct 524 ms 58712 KB Output is correct
36 Correct 238 ms 49472 KB Output is correct
37 Correct 587 ms 58728 KB Output is correct
38 Correct 334 ms 51164 KB Output is correct
39 Correct 327 ms 61608 KB Output is correct
40 Correct 589 ms 58244 KB Output is correct
41 Correct 10 ms 7928 KB Output is correct
42 Correct 11 ms 7928 KB Output is correct
43 Correct 15 ms 7928 KB Output is correct
44 Correct 11 ms 8056 KB Output is correct
45 Correct 9 ms 7932 KB Output is correct
46 Correct 16 ms 7928 KB Output is correct
47 Correct 20 ms 8056 KB Output is correct
48 Correct 11 ms 7928 KB Output is correct
49 Correct 11 ms 7928 KB Output is correct
50 Correct 11 ms 7928 KB Output is correct
51 Correct 337 ms 51364 KB Output is correct
52 Correct 357 ms 61788 KB Output is correct
53 Correct 589 ms 58464 KB Output is correct
54 Correct 237 ms 49476 KB Output is correct
55 Correct 328 ms 51160 KB Output is correct
56 Correct 361 ms 61832 KB Output is correct
57 Correct 533 ms 59116 KB Output is correct
58 Correct 245 ms 49468 KB Output is correct
59 Correct 417 ms 51264 KB Output is correct
60 Correct 402 ms 61676 KB Output is correct
61 Correct 676 ms 59088 KB Output is correct
62 Correct 246 ms 50556 KB Output is correct
63 Correct 337 ms 51388 KB Output is correct
64 Correct 399 ms 61712 KB Output is correct
65 Correct 591 ms 59136 KB Output is correct
66 Correct 243 ms 49528 KB Output is correct
67 Correct 437 ms 51136 KB Output is correct
68 Correct 366 ms 61760 KB Output is correct
69 Correct 520 ms 58244 KB Output is correct
70 Correct 266 ms 49836 KB Output is correct