답안 #34838

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
34838 2017-11-16T06:11:13 Z sinhriv Election Campaign (JOI15_election_campaign) C++14
100 / 100
663 ms 54716 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

struct Query{
	int u;
	int v;
	int w;
};

/// tree

int n, m, cnt;
int L[N];
int R[N];
int depth[N];
int jump[N][22];
vector < int > adj[N];

void preDfs(int u, int p = 0){
	L[u] = ++cnt;
	jump[u][0] = p;
	depth[u] = depth[p] + 1;
	for(int i = 1; i < 20; ++i){
		jump[u][i] = jump[jump[u][i - 1]][i - 1];
	}
	for(int v : adj[u]){
		if(v == p) continue;
		preDfs(v, u);
	}
	R[u] = cnt;
}

int lca(int u, int v){
	if(depth[u] < depth[v]) swap(u, v);
	for(int i = 19; i >= 0; --i){
		if(depth[jump[u][i]] >= depth[v]) u = jump[u][i];
	}
	if(u == v) return u;

	for(int i = 19; i >= 0; --i){
		if(jump[u][i] != jump[v][i]){
			u = jump[u][i];
			v = jump[v][i];
		}
	}
	return jump[u][0];
}

int subTree(int u, int x){
	for(int i = 19; i >= 0; --i){
		if(depth[jump[x][i]] > depth[u]){
			x = jump[x][i];
		}
	}
	return x;
}

/// Dp


int f[N];
vector < Query > solve[N];

struct SegmentTree{
	int lazy[N << 3];
	int value[N << 3];

	void push(int x){
		value[x] += lazy[x];
		lazy[x + x] += lazy[x];
		lazy[x + x + 1] += lazy[x];
		lazy[x] = 0;
	}

	#define mid ((l + r) >> 1)

	void update(int x, int l, int r, int u, int v, int val){
		push(x);
		if(l > v || r < u) return;
		if(l >= u && r <= v){
			lazy[x] += val;
			push(x);
			return;
		}
		update(x + x, l, mid, u, v, val);
		update(x + x + 1, mid + 1, r, u, v, val);
		value[x] = value[x + x] + value[x + x + 1];
	}

	int query(int x, int l, int r, int pos){
		if(pos > r || pos < l) return 0;
		if(l == r) return value[x] + lazy[x];
		return lazy[x] + query(x + x, l, mid, pos) + query(x + x + 1, mid + 1, r, pos);
	}
}Smt;

#define value(x) Smt.query(1, 1, n, L[x])

void dfs(int u, int p = 0){
	int tot = 0;
	for(int v : adj[u]){
		if(v == p) continue;
		dfs(v, u);
		tot += f[v];
	}

	f[u] = max(f[u], tot);

	for(auto t : solve[u]){
		int a = t.u, b = t.v;
		if(a == b && a == u){
			f[u] = max(f[u], tot + t.w);
			continue;
		}
		if(b == u) swap(a, b);
		if(a == u){
			int v = subTree(u, b);
			f[u] = max(f[u], tot - f[v] + value(b) + t.w);
			continue;
		}

		int i = subTree(u, a), j = subTree(u, b);
		f[u] = max(f[u], tot - f[i] - f[j] + value(a) + value(b) + t.w);
	}

	for(int v : adj[u]){
		if(v == p) continue;
		Smt.update(1, 1, n, L[v], R[v], tot - f[v]);
	}
	Smt.update(1, 1, n, L[u], L[u], tot);
}

int main(){
	if(fopen("1.inp", "r")){
		freopen("1.inp", "r", stdin);
	}


	scanf("%d", &n);
	for(int i = 1; i < n; ++i){
		int u, v;
		scanf("%d%d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	preDfs(1);

	scanf("%d", &m);
	for(int i = 1; i <= m; ++i){
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		int p = lca(u, v);
		solve[p].push_back({u, v, w});
	}

	dfs(1);
	cout << f[1];

	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:138:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen("1.inp", "r", stdin);
                               ^
election_campaign.cpp:142:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
election_campaign.cpp:145:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &u, &v);
                        ^
election_campaign.cpp:152:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &m);
                 ^
election_campaign.cpp:155:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &u, &v, &w);
                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23128 KB Output is correct
2 Correct 0 ms 23128 KB Output is correct
3 Correct 0 ms 23128 KB Output is correct
4 Correct 0 ms 23128 KB Output is correct
5 Correct 236 ms 26428 KB Output is correct
6 Correct 116 ms 52736 KB Output is correct
7 Correct 236 ms 43556 KB Output is correct
8 Correct 166 ms 26948 KB Output is correct
9 Correct 226 ms 38288 KB Output is correct
10 Correct 163 ms 26888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 23128 KB Output is correct
2 Correct 0 ms 23128 KB Output is correct
3 Correct 0 ms 23268 KB Output is correct
4 Correct 313 ms 54712 KB Output is correct
5 Correct 309 ms 54712 KB Output is correct
6 Correct 279 ms 54712 KB Output is correct
7 Correct 283 ms 54712 KB Output is correct
8 Correct 323 ms 54716 KB Output is correct
9 Correct 283 ms 54716 KB Output is correct
10 Correct 316 ms 54712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 23128 KB Output is correct
2 Correct 0 ms 23128 KB Output is correct
3 Correct 0 ms 23268 KB Output is correct
4 Correct 313 ms 54712 KB Output is correct
5 Correct 309 ms 54712 KB Output is correct
6 Correct 279 ms 54712 KB Output is correct
7 Correct 283 ms 54712 KB Output is correct
8 Correct 323 ms 54716 KB Output is correct
9 Correct 283 ms 54716 KB Output is correct
10 Correct 316 ms 54712 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 346 ms 54712 KB Output is correct
13 Correct 313 ms 54716 KB Output is correct
14 Correct 276 ms 54716 KB Output is correct
15 Correct 313 ms 54716 KB Output is correct
16 Correct 256 ms 54716 KB Output is correct
17 Correct 316 ms 54712 KB Output is correct
18 Correct 339 ms 54712 KB Output is correct
19 Correct 299 ms 54716 KB Output is correct
20 Correct 329 ms 54716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 413 ms 28168 KB Output is correct
2 Correct 296 ms 54716 KB Output is correct
3 Correct 629 ms 44660 KB Output is correct
4 Correct 343 ms 28888 KB Output is correct
5 Correct 466 ms 42860 KB Output is correct
6 Correct 333 ms 29224 KB Output is correct
7 Correct 546 ms 42300 KB Output is correct
8 Correct 373 ms 28144 KB Output is correct
9 Correct 259 ms 54712 KB Output is correct
10 Correct 609 ms 39484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23128 KB Output is correct
2 Correct 0 ms 23128 KB Output is correct
3 Correct 0 ms 23128 KB Output is correct
4 Correct 0 ms 23128 KB Output is correct
5 Correct 236 ms 26428 KB Output is correct
6 Correct 116 ms 52736 KB Output is correct
7 Correct 236 ms 43556 KB Output is correct
8 Correct 166 ms 26948 KB Output is correct
9 Correct 226 ms 38288 KB Output is correct
10 Correct 163 ms 26888 KB Output is correct
11 Correct 6 ms 23260 KB Output is correct
12 Correct 0 ms 23400 KB Output is correct
13 Correct 0 ms 23304 KB Output is correct
14 Correct 0 ms 23260 KB Output is correct
15 Correct 3 ms 23260 KB Output is correct
16 Correct 3 ms 23260 KB Output is correct
17 Correct 0 ms 23264 KB Output is correct
18 Correct 0 ms 23308 KB Output is correct
19 Correct 0 ms 23260 KB Output is correct
20 Correct 0 ms 23404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 23128 KB Output is correct
2 Correct 0 ms 23128 KB Output is correct
3 Correct 0 ms 23128 KB Output is correct
4 Correct 0 ms 23128 KB Output is correct
5 Correct 236 ms 26428 KB Output is correct
6 Correct 116 ms 52736 KB Output is correct
7 Correct 236 ms 43556 KB Output is correct
8 Correct 166 ms 26948 KB Output is correct
9 Correct 226 ms 38288 KB Output is correct
10 Correct 163 ms 26888 KB Output is correct
11 Correct 0 ms 23128 KB Output is correct
12 Correct 0 ms 23128 KB Output is correct
13 Correct 0 ms 23268 KB Output is correct
14 Correct 313 ms 54712 KB Output is correct
15 Correct 309 ms 54712 KB Output is correct
16 Correct 279 ms 54712 KB Output is correct
17 Correct 283 ms 54712 KB Output is correct
18 Correct 323 ms 54716 KB Output is correct
19 Correct 283 ms 54716 KB Output is correct
20 Correct 316 ms 54712 KB Output is correct
21 Correct 23 ms 23800 KB Output is correct
22 Correct 346 ms 54712 KB Output is correct
23 Correct 313 ms 54716 KB Output is correct
24 Correct 276 ms 54716 KB Output is correct
25 Correct 313 ms 54716 KB Output is correct
26 Correct 256 ms 54716 KB Output is correct
27 Correct 316 ms 54712 KB Output is correct
28 Correct 339 ms 54712 KB Output is correct
29 Correct 299 ms 54716 KB Output is correct
30 Correct 329 ms 54716 KB Output is correct
31 Correct 413 ms 28168 KB Output is correct
32 Correct 296 ms 54716 KB Output is correct
33 Correct 629 ms 44660 KB Output is correct
34 Correct 343 ms 28888 KB Output is correct
35 Correct 466 ms 42860 KB Output is correct
36 Correct 333 ms 29224 KB Output is correct
37 Correct 546 ms 42300 KB Output is correct
38 Correct 373 ms 28144 KB Output is correct
39 Correct 259 ms 54712 KB Output is correct
40 Correct 609 ms 39484 KB Output is correct
41 Correct 6 ms 23260 KB Output is correct
42 Correct 0 ms 23400 KB Output is correct
43 Correct 0 ms 23304 KB Output is correct
44 Correct 0 ms 23260 KB Output is correct
45 Correct 3 ms 23260 KB Output is correct
46 Correct 3 ms 23260 KB Output is correct
47 Correct 0 ms 23264 KB Output is correct
48 Correct 0 ms 23308 KB Output is correct
49 Correct 0 ms 23260 KB Output is correct
50 Correct 0 ms 23404 KB Output is correct
51 Correct 403 ms 28164 KB Output is correct
52 Correct 323 ms 54712 KB Output is correct
53 Correct 576 ms 39888 KB Output is correct
54 Correct 306 ms 28692 KB Output is correct
55 Correct 396 ms 28396 KB Output is correct
56 Correct 319 ms 54716 KB Output is correct
57 Correct 513 ms 41276 KB Output is correct
58 Correct 343 ms 28720 KB Output is correct
59 Correct 379 ms 28228 KB Output is correct
60 Correct 356 ms 54716 KB Output is correct
61 Correct 519 ms 41640 KB Output is correct
62 Correct 326 ms 29316 KB Output is correct
63 Correct 433 ms 28468 KB Output is correct
64 Correct 333 ms 54716 KB Output is correct
65 Correct 663 ms 41960 KB Output is correct
66 Correct 316 ms 28788 KB Output is correct
67 Correct 419 ms 28900 KB Output is correct
68 Correct 343 ms 54712 KB Output is correct
69 Correct 526 ms 37340 KB Output is correct
70 Correct 349 ms 28988 KB Output is correct