답안 #70917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
70917 2018-08-23T16:56:26 Z zadrga Election Campaign (JOI15_election_campaign) C++14
100 / 100
428 ms 113904 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (1LL << 55)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 100111
#define logn 20

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;

vector<int> adj[maxn];
vector<vector<int> > query[maxn];

int in[maxn], out[maxn], dep[maxn];
int par[maxn][logn];
ll dp[maxn];
int cnt;

struct BIT{
	ll bit[maxn];

	int query(int x){
		ll ret = 0;
		while(x){
			ret += bit[x];
			x -= (x & -x);
		}
		return ret;
	}

	void update(int x, ll val){
		while(x < maxn){
			bit[x] += val;
			x += (x & -x);
		}
	}
} bit;

void DFS(int x, int p){
	in[x] = ++cnt;
	par[x][0] = p;

	for(int v : adj[x]){
		if(v == p)
			continue;

		dep[v] = dep[x] + 1;
		DFS(v, x);
	}

	out[x] = cnt;
}

int LCA(int a, int b){
	if(dep[a] > dep[b])
		swap(a, b);

	int raz = dep[b] - dep[a];
	for(int i = 0; i < logn; i++){
		if((raz >> i) & 1)
			b = par[b][i]; 
	}

	if(a == b)
		return a;

	for(int i = logn - 1; i >= 0; i--){
		if(par[a][i] != par[b][i]){
			a = par[a][i];
			b = par[b][i];
		}
	}
	return par[a][0];
}

void solve(int x, int p){
	dp[x] = 0;
	ll sum = 0;
	for(int v : adj[x]){
		if(v == p)
			continue;

		solve(v, x);
		sum += dp[v];
	}

	dp[x] = sum;
	for(vector<int> a : query[x]){
		int u = a[0], v = a[1], cost = a[2];
		ll cur = 0;
		if(u != x)
			cur += bit.query(in[u]);
		if(v != x)
			cur += bit.query(in[v]);
		dp[x] = max(dp[x], sum + cur + cost);
	}

	ll cur = sum - dp[x];
	bit.update(in[x], cur);
	bit.update(out[x] + 1, -cur);
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n - 1; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		adj[a].pb(b);
		adj[b].pb(a);
	}

	DFS(1, 0);

	for(int j = 1; j < logn; j++)
		for(int i = 1; i <= n; i++)
			par[i][j] = par[par[i][j - 1]][j - 1];

	int m;
	scanf("%d", &m);
	for(int i = 0; i < m; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		int lca = LCA(a, b);
		query[lca].pb({a, b, c});
	}

	solve(1, 0);

	printf("%lld\n", dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:112:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:127:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &a, &b, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 8 ms 5228 KB Output is correct
4 Correct 7 ms 5420 KB Output is correct
5 Correct 210 ms 20268 KB Output is correct
6 Correct 93 ms 29356 KB Output is correct
7 Correct 160 ms 29356 KB Output is correct
8 Correct 134 ms 29356 KB Output is correct
9 Correct 201 ms 29356 KB Output is correct
10 Correct 149 ms 29356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29356 KB Output is correct
2 Correct 9 ms 29356 KB Output is correct
3 Correct 8 ms 29356 KB Output is correct
4 Correct 317 ms 41956 KB Output is correct
5 Correct 332 ms 41956 KB Output is correct
6 Correct 261 ms 41956 KB Output is correct
7 Correct 293 ms 41956 KB Output is correct
8 Correct 277 ms 41996 KB Output is correct
9 Correct 235 ms 41996 KB Output is correct
10 Correct 244 ms 41996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 29356 KB Output is correct
2 Correct 9 ms 29356 KB Output is correct
3 Correct 8 ms 29356 KB Output is correct
4 Correct 317 ms 41956 KB Output is correct
5 Correct 332 ms 41956 KB Output is correct
6 Correct 261 ms 41956 KB Output is correct
7 Correct 293 ms 41956 KB Output is correct
8 Correct 277 ms 41996 KB Output is correct
9 Correct 235 ms 41996 KB Output is correct
10 Correct 244 ms 41996 KB Output is correct
11 Correct 23 ms 41996 KB Output is correct
12 Correct 266 ms 41996 KB Output is correct
13 Correct 222 ms 41996 KB Output is correct
14 Correct 218 ms 41996 KB Output is correct
15 Correct 243 ms 41996 KB Output is correct
16 Correct 229 ms 41996 KB Output is correct
17 Correct 257 ms 41996 KB Output is correct
18 Correct 270 ms 41996 KB Output is correct
19 Correct 225 ms 41996 KB Output is correct
20 Correct 235 ms 41996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 41996 KB Output is correct
2 Correct 252 ms 44412 KB Output is correct
3 Correct 408 ms 44412 KB Output is correct
4 Correct 238 ms 44412 KB Output is correct
5 Correct 317 ms 48188 KB Output is correct
6 Correct 207 ms 48188 KB Output is correct
7 Correct 388 ms 52880 KB Output is correct
8 Correct 374 ms 52880 KB Output is correct
9 Correct 254 ms 61760 KB Output is correct
10 Correct 368 ms 61760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 8 ms 5228 KB Output is correct
4 Correct 7 ms 5420 KB Output is correct
5 Correct 210 ms 20268 KB Output is correct
6 Correct 93 ms 29356 KB Output is correct
7 Correct 160 ms 29356 KB Output is correct
8 Correct 134 ms 29356 KB Output is correct
9 Correct 201 ms 29356 KB Output is correct
10 Correct 149 ms 29356 KB Output is correct
11 Correct 9 ms 61760 KB Output is correct
12 Correct 9 ms 61760 KB Output is correct
13 Correct 8 ms 61760 KB Output is correct
14 Correct 8 ms 61760 KB Output is correct
15 Correct 11 ms 61760 KB Output is correct
16 Correct 8 ms 61760 KB Output is correct
17 Correct 8 ms 61760 KB Output is correct
18 Correct 9 ms 61760 KB Output is correct
19 Correct 11 ms 61760 KB Output is correct
20 Correct 9 ms 61760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5112 KB Output is correct
2 Correct 8 ms 5228 KB Output is correct
3 Correct 8 ms 5228 KB Output is correct
4 Correct 7 ms 5420 KB Output is correct
5 Correct 210 ms 20268 KB Output is correct
6 Correct 93 ms 29356 KB Output is correct
7 Correct 160 ms 29356 KB Output is correct
8 Correct 134 ms 29356 KB Output is correct
9 Correct 201 ms 29356 KB Output is correct
10 Correct 149 ms 29356 KB Output is correct
11 Correct 8 ms 29356 KB Output is correct
12 Correct 9 ms 29356 KB Output is correct
13 Correct 8 ms 29356 KB Output is correct
14 Correct 317 ms 41956 KB Output is correct
15 Correct 332 ms 41956 KB Output is correct
16 Correct 261 ms 41956 KB Output is correct
17 Correct 293 ms 41956 KB Output is correct
18 Correct 277 ms 41996 KB Output is correct
19 Correct 235 ms 41996 KB Output is correct
20 Correct 244 ms 41996 KB Output is correct
21 Correct 23 ms 41996 KB Output is correct
22 Correct 266 ms 41996 KB Output is correct
23 Correct 222 ms 41996 KB Output is correct
24 Correct 218 ms 41996 KB Output is correct
25 Correct 243 ms 41996 KB Output is correct
26 Correct 229 ms 41996 KB Output is correct
27 Correct 257 ms 41996 KB Output is correct
28 Correct 270 ms 41996 KB Output is correct
29 Correct 225 ms 41996 KB Output is correct
30 Correct 235 ms 41996 KB Output is correct
31 Correct 321 ms 41996 KB Output is correct
32 Correct 252 ms 44412 KB Output is correct
33 Correct 408 ms 44412 KB Output is correct
34 Correct 238 ms 44412 KB Output is correct
35 Correct 317 ms 48188 KB Output is correct
36 Correct 207 ms 48188 KB Output is correct
37 Correct 388 ms 52880 KB Output is correct
38 Correct 374 ms 52880 KB Output is correct
39 Correct 254 ms 61760 KB Output is correct
40 Correct 368 ms 61760 KB Output is correct
41 Correct 9 ms 61760 KB Output is correct
42 Correct 9 ms 61760 KB Output is correct
43 Correct 8 ms 61760 KB Output is correct
44 Correct 8 ms 61760 KB Output is correct
45 Correct 11 ms 61760 KB Output is correct
46 Correct 8 ms 61760 KB Output is correct
47 Correct 8 ms 61760 KB Output is correct
48 Correct 9 ms 61760 KB Output is correct
49 Correct 11 ms 61760 KB Output is correct
50 Correct 9 ms 61760 KB Output is correct
51 Correct 350 ms 61760 KB Output is correct
52 Correct 313 ms 69808 KB Output is correct
53 Correct 353 ms 69808 KB Output is correct
54 Correct 236 ms 69808 KB Output is correct
55 Correct 278 ms 69808 KB Output is correct
56 Correct 296 ms 81024 KB Output is correct
57 Correct 350 ms 81024 KB Output is correct
58 Correct 268 ms 81024 KB Output is correct
59 Correct 299 ms 81156 KB Output is correct
60 Correct 259 ms 91940 KB Output is correct
61 Correct 331 ms 91940 KB Output is correct
62 Correct 216 ms 91940 KB Output is correct
63 Correct 326 ms 91940 KB Output is correct
64 Correct 317 ms 102868 KB Output is correct
65 Correct 428 ms 102868 KB Output is correct
66 Correct 342 ms 102868 KB Output is correct
67 Correct 309 ms 102868 KB Output is correct
68 Correct 314 ms 113904 KB Output is correct
69 Correct 384 ms 113904 KB Output is correct
70 Correct 256 ms 113904 KB Output is correct