답안 #56841

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56841 2018-07-12T20:08:25 Z Bruteforceman Election Campaign (JOI15_election_campaign) C++11
100 / 100
622 ms 143952 KB
#include <bits/stdc++.h>
using namespace std;
vector <int> g[100010];
int anc[20][100010];
const int logn = 19;
int dep[100010];
vector <int> path[100010];
int a[100010], b[100010], c[100010];

void dfs(int x, int par) {
	anc[0][x] = par;
	for(int i = 1; i <= logn; i++) {
		anc[i][x] = anc[i - 1][anc[i - 1][x]];
	} 
	for(auto i : g[x]) {
		if(i - par) {
			dep[i] = 1 + dep[x];
			dfs(i, x);
		}
	}
}
int lca(int p, int q) {
	if(dep[p] > dep[q]) swap(p, q);
	for(int i = logn; i >= 0; i--) {
		if(dep[q] - (1 << i) >= dep[p]) {
			q = anc[i][q];
		}
	}
	if(p == q) return p;
	for(int i = logn; i >= 0; i--) {
		if(anc[i][p] != anc[i][q]) {
			p = anc[i][p];
			q = anc[i][q];
		}
	}
	return anc[0][p];
}

long long dp[100010];
long long sum[100010];
long long sib[100010];

long long l[100010], r[100010];
int tym;
int n;

void order(int x, int par) {
	l[x] = ++tym;
	for(int i : g[x]) {
		if(i - par) {
			order(i, x);
		}
	}
	r[x] = tym;
}
long long t[100010];
void update(int x, long long val) {
	for(int i = x; i <= n; i += i & (-i)) {
		t[i] += val;
	}
}
long long query(int x) {
	long long ans = 0;
	for(int i = x; i > 0; i -= i & (-i)) {
		ans += t[i];
	}
	return ans;
}
long long query(int l, int r) {
	return query(r) - query(l - 1);
}
int get(int x, int p) {
	for(int i = logn; i >= 0; i--) {
		if(dep[x] - (1 << i) >= p) {
			x = anc[i][x];
		}
	}
	return x;
}
void calc(int x, int par) {
	dp[x] = 0;
	for(auto i : g[x]) {
		if (i - par) {
			calc(i, x);
			dp[x] += dp[i];
		}
	}
	sum[x] = dp[x];
	for(auto i : g[x]) {
		if(i - par) {
			sib[i] = sum[x] - dp[i];
		}
	}
	for(auto i : path[x]) {
		int node = get(a[i], dep[x] + 1);
		long long res = c[i];
		res += query(l[a[i]]);
		if(node != x) res -= dp[node];
		node = get(b[i], dep[x] + 1);
		res += query(l[b[i]]);
		if(node != x) res -= dp[node];
		res += sum[x];
		if(a[i] != x) res += sum[a[i]];
		if(b[i] != x) res += sum[b[i]];
		dp[x] = max(dp[x], res);
	}
	for(auto i : g[x]) {
		if(i - par) {
			update(l[i], sib[i]);
			update(r[i] + 1, -sib[i]);
		}
	}
}
	
int main(int argc, char const *argv[])
{
	int m;
	scanf("%d", &n);
	for(int i = 1; i < n; i++) {
		int p, q;
		scanf("%d %d", &p, &q);
		g[p].emplace_back(q);
		g[q].emplace_back(p);
	}
	dfs(1, 0);
	order(1, 0);
	scanf("%d", &m);
	for(int i = 1; i <= m; i++) {
		scanf("%d %d %d", &a[i], &b[i], &c[i]);
		path[lca(a[i], b[i])].emplace_back(i);
	}
	calc(1, 0);
	printf("%lld\n", dp[1]);
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int main(int, const char**)':
election_campaign.cpp:118:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election_campaign.cpp:121:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
   ~~~~~^~~~~~~~~~~~~~~~~
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:129:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &a[i], &b[i], &c[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5356 KB Output is correct
3 Correct 6 ms 5356 KB Output is correct
4 Correct 7 ms 5484 KB Output is correct
5 Correct 185 ms 21432 KB Output is correct
6 Correct 107 ms 32256 KB Output is correct
7 Correct 203 ms 32256 KB Output is correct
8 Correct 157 ms 32256 KB Output is correct
9 Correct 222 ms 32256 KB Output is correct
10 Correct 173 ms 32256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 32256 KB Output is correct
2 Correct 7 ms 32256 KB Output is correct
3 Correct 8 ms 32256 KB Output is correct
4 Correct 266 ms 34532 KB Output is correct
5 Correct 266 ms 37092 KB Output is correct
6 Correct 287 ms 39608 KB Output is correct
7 Correct 248 ms 42076 KB Output is correct
8 Correct 236 ms 44452 KB Output is correct
9 Correct 189 ms 47068 KB Output is correct
10 Correct 239 ms 49372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 32256 KB Output is correct
2 Correct 7 ms 32256 KB Output is correct
3 Correct 8 ms 32256 KB Output is correct
4 Correct 266 ms 34532 KB Output is correct
5 Correct 266 ms 37092 KB Output is correct
6 Correct 287 ms 39608 KB Output is correct
7 Correct 248 ms 42076 KB Output is correct
8 Correct 236 ms 44452 KB Output is correct
9 Correct 189 ms 47068 KB Output is correct
10 Correct 239 ms 49372 KB Output is correct
11 Correct 25 ms 49372 KB Output is correct
12 Correct 263 ms 52384 KB Output is correct
13 Correct 267 ms 55276 KB Output is correct
14 Correct 160 ms 57992 KB Output is correct
15 Correct 188 ms 60780 KB Output is correct
16 Correct 214 ms 63488 KB Output is correct
17 Correct 190 ms 66108 KB Output is correct
18 Correct 209 ms 68912 KB Output is correct
19 Correct 206 ms 71776 KB Output is correct
20 Correct 285 ms 74500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 352 ms 74500 KB Output is correct
2 Correct 228 ms 74500 KB Output is correct
3 Correct 586 ms 74500 KB Output is correct
4 Correct 281 ms 74500 KB Output is correct
5 Correct 420 ms 76932 KB Output is correct
6 Correct 253 ms 76932 KB Output is correct
7 Correct 563 ms 81552 KB Output is correct
8 Correct 360 ms 81552 KB Output is correct
9 Correct 214 ms 91756 KB Output is correct
10 Correct 584 ms 91756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5356 KB Output is correct
3 Correct 6 ms 5356 KB Output is correct
4 Correct 7 ms 5484 KB Output is correct
5 Correct 185 ms 21432 KB Output is correct
6 Correct 107 ms 32256 KB Output is correct
7 Correct 203 ms 32256 KB Output is correct
8 Correct 157 ms 32256 KB Output is correct
9 Correct 222 ms 32256 KB Output is correct
10 Correct 173 ms 32256 KB Output is correct
11 Correct 7 ms 91756 KB Output is correct
12 Correct 9 ms 91756 KB Output is correct
13 Correct 9 ms 91756 KB Output is correct
14 Correct 9 ms 91756 KB Output is correct
15 Correct 10 ms 91756 KB Output is correct
16 Correct 9 ms 91756 KB Output is correct
17 Correct 8 ms 91756 KB Output is correct
18 Correct 8 ms 91756 KB Output is correct
19 Correct 8 ms 91756 KB Output is correct
20 Correct 8 ms 91756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 6 ms 5356 KB Output is correct
3 Correct 6 ms 5356 KB Output is correct
4 Correct 7 ms 5484 KB Output is correct
5 Correct 185 ms 21432 KB Output is correct
6 Correct 107 ms 32256 KB Output is correct
7 Correct 203 ms 32256 KB Output is correct
8 Correct 157 ms 32256 KB Output is correct
9 Correct 222 ms 32256 KB Output is correct
10 Correct 173 ms 32256 KB Output is correct
11 Correct 6 ms 32256 KB Output is correct
12 Correct 7 ms 32256 KB Output is correct
13 Correct 8 ms 32256 KB Output is correct
14 Correct 266 ms 34532 KB Output is correct
15 Correct 266 ms 37092 KB Output is correct
16 Correct 287 ms 39608 KB Output is correct
17 Correct 248 ms 42076 KB Output is correct
18 Correct 236 ms 44452 KB Output is correct
19 Correct 189 ms 47068 KB Output is correct
20 Correct 239 ms 49372 KB Output is correct
21 Correct 25 ms 49372 KB Output is correct
22 Correct 263 ms 52384 KB Output is correct
23 Correct 267 ms 55276 KB Output is correct
24 Correct 160 ms 57992 KB Output is correct
25 Correct 188 ms 60780 KB Output is correct
26 Correct 214 ms 63488 KB Output is correct
27 Correct 190 ms 66108 KB Output is correct
28 Correct 209 ms 68912 KB Output is correct
29 Correct 206 ms 71776 KB Output is correct
30 Correct 285 ms 74500 KB Output is correct
31 Correct 352 ms 74500 KB Output is correct
32 Correct 228 ms 74500 KB Output is correct
33 Correct 586 ms 74500 KB Output is correct
34 Correct 281 ms 74500 KB Output is correct
35 Correct 420 ms 76932 KB Output is correct
36 Correct 253 ms 76932 KB Output is correct
37 Correct 563 ms 81552 KB Output is correct
38 Correct 360 ms 81552 KB Output is correct
39 Correct 214 ms 91756 KB Output is correct
40 Correct 584 ms 91756 KB Output is correct
41 Correct 7 ms 91756 KB Output is correct
42 Correct 9 ms 91756 KB Output is correct
43 Correct 9 ms 91756 KB Output is correct
44 Correct 9 ms 91756 KB Output is correct
45 Correct 10 ms 91756 KB Output is correct
46 Correct 9 ms 91756 KB Output is correct
47 Correct 8 ms 91756 KB Output is correct
48 Correct 8 ms 91756 KB Output is correct
49 Correct 8 ms 91756 KB Output is correct
50 Correct 8 ms 91756 KB Output is correct
51 Correct 352 ms 91756 KB Output is correct
52 Correct 215 ms 99712 KB Output is correct
53 Correct 546 ms 99712 KB Output is correct
54 Correct 297 ms 99712 KB Output is correct
55 Correct 439 ms 99712 KB Output is correct
56 Correct 320 ms 110768 KB Output is correct
57 Correct 476 ms 110768 KB Output is correct
58 Correct 301 ms 110768 KB Output is correct
59 Correct 381 ms 110768 KB Output is correct
60 Correct 278 ms 121864 KB Output is correct
61 Correct 423 ms 121864 KB Output is correct
62 Correct 259 ms 121864 KB Output is correct
63 Correct 389 ms 121864 KB Output is correct
64 Correct 298 ms 132892 KB Output is correct
65 Correct 622 ms 132892 KB Output is correct
66 Correct 314 ms 132892 KB Output is correct
67 Correct 444 ms 132892 KB Output is correct
68 Correct 292 ms 143952 KB Output is correct
69 Correct 472 ms 143952 KB Output is correct
70 Correct 343 ms 143952 KB Output is correct