답안 #113814

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
113814 2019-05-28T13:15:12 Z maruii Election Campaign (JOI15_election_campaign) C++14
100 / 100
353 ms 31120 KB
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ff first
#define ss second

const int SIZE = 1 << 17;
struct ST {
	int V[2 * SIZE];
	void update(int s, int e, int v) {
		for (s += SIZE, e += SIZE; s <= e; s >>= 1, e >>= 1) {
			if ( s & 1) V[s++] += v;
			if (~e & 1) V[e--] += v;
		}
	}
	int query(int x) {
		int ret = 0;
		for (x += SIZE; x; x >>= 1) ret += V[x];
		return ret;
	}
} seg;

int dep[100005], par[17][100005], dfn[100005], efn[100005], dfnn, A[100005], B[100005], X[100005], Y[100005];
vector<pair<int, pii> > path[100005];
vector<int> edge[100005];

void dfs0(int x, int p) {
	dep[x] = dep[p] + 1;
	dfn[x] = ++dfnn;
	par[0][x] = p;
	for (int i = 1; i < 17; ++i) par[i][x] = par[i-1][par[i-1][x]];
	for (auto i: edge[x]) {
		if (i != p) dfs0(i, x);
	}
	efn[x] = dfnn;
}

int lca(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	for (int i = 0; i < 17; ++i)
		if ((dep[x] - dep[y] >> i) & 1) x = par[i][x];
	if (x == y) return x;
	for (int i = 16; i >= 0; --i)
		if (par[i][x] != par[i][y]) x = par[i][x], y = par[i][y];
	return par[0][x];
}

void dfs(int x, int p) {
	for (auto i: edge[x]) {
		if (i == p) continue;
		dfs(i, x);
		B[x] += A[i];
	}
	A[x] = B[x];

	for (auto i: edge[x]) {
		if (i == p) continue;
		seg.update(dfn[i], efn[i], B[x] - A[i]);
	}

	for (auto i: path[x]) {
		A[x] = max(A[x], i.ff + B[i.ss.ff] + seg.query(dfn[i.ss.ff]) + B[i.ss.ss] + seg.query(dfn[i.ss.ss]) - B[x]);
	}
}


int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int N, M; cin >> N;
	for (int i = 1; i < N; ++i) {
		cin >> X[i] >> Y[i];
		edge[X[i]].push_back(Y[i]);
		edge[Y[i]].push_back(X[i]);
	}

	dfs0(1, 1);

	cin >> M;
	for (int i = 0; i < M; ++i) {
		int x, y, z; cin >> x >> y >> z;
		path[lca(x, y)].push_back({z, {x, y}});
	}

	dfs(1, 1);

	cout << A[1];
	return 0;
}

Compilation message

election_campaign.cpp: In function 'int lca(int, int)':
election_campaign.cpp:41:15: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   if ((dep[x] - dep[y] >> i) & 1) x = par[i][x];
        ~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 162 ms 19716 KB Output is correct
6 Correct 64 ms 27384 KB Output is correct
7 Correct 160 ms 24664 KB Output is correct
8 Correct 126 ms 19548 KB Output is correct
9 Correct 160 ms 23172 KB Output is correct
10 Correct 116 ms 19364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 35 ms 5120 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 161 ms 30684 KB Output is correct
5 Correct 171 ms 30840 KB Output is correct
6 Correct 138 ms 30848 KB Output is correct
7 Correct 170 ms 30664 KB Output is correct
8 Correct 165 ms 30636 KB Output is correct
9 Correct 126 ms 30804 KB Output is correct
10 Correct 144 ms 30712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 35 ms 5120 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 161 ms 30684 KB Output is correct
5 Correct 171 ms 30840 KB Output is correct
6 Correct 138 ms 30848 KB Output is correct
7 Correct 170 ms 30664 KB Output is correct
8 Correct 165 ms 30636 KB Output is correct
9 Correct 126 ms 30804 KB Output is correct
10 Correct 144 ms 30712 KB Output is correct
11 Correct 16 ms 6144 KB Output is correct
12 Correct 165 ms 30940 KB Output is correct
13 Correct 183 ms 31084 KB Output is correct
14 Correct 144 ms 31048 KB Output is correct
15 Correct 170 ms 31004 KB Output is correct
16 Correct 141 ms 31120 KB Output is correct
17 Correct 181 ms 30968 KB Output is correct
18 Correct 171 ms 30968 KB Output is correct
19 Correct 154 ms 31080 KB Output is correct
20 Correct 172 ms 31060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 22448 KB Output is correct
2 Correct 146 ms 30712 KB Output is correct
3 Correct 353 ms 27668 KB Output is correct
4 Correct 169 ms 22320 KB Output is correct
5 Correct 261 ms 27100 KB Output is correct
6 Correct 159 ms 22380 KB Output is correct
7 Correct 319 ms 26836 KB Output is correct
8 Correct 224 ms 22776 KB Output is correct
9 Correct 145 ms 30712 KB Output is correct
10 Correct 318 ms 25772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 162 ms 19716 KB Output is correct
6 Correct 64 ms 27384 KB Output is correct
7 Correct 160 ms 24664 KB Output is correct
8 Correct 126 ms 19548 KB Output is correct
9 Correct 160 ms 23172 KB Output is correct
10 Correct 116 ms 19364 KB Output is correct
11 Correct 7 ms 5348 KB Output is correct
12 Correct 7 ms 5504 KB Output is correct
13 Correct 7 ms 5452 KB Output is correct
14 Correct 7 ms 5376 KB Output is correct
15 Correct 7 ms 5376 KB Output is correct
16 Correct 7 ms 5376 KB Output is correct
17 Correct 7 ms 5376 KB Output is correct
18 Correct 7 ms 5376 KB Output is correct
19 Correct 8 ms 5376 KB Output is correct
20 Correct 7 ms 5504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5120 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5248 KB Output is correct
4 Correct 7 ms 5376 KB Output is correct
5 Correct 162 ms 19716 KB Output is correct
6 Correct 64 ms 27384 KB Output is correct
7 Correct 160 ms 24664 KB Output is correct
8 Correct 126 ms 19548 KB Output is correct
9 Correct 160 ms 23172 KB Output is correct
10 Correct 116 ms 19364 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 35 ms 5120 KB Output is correct
13 Correct 6 ms 5376 KB Output is correct
14 Correct 161 ms 30684 KB Output is correct
15 Correct 171 ms 30840 KB Output is correct
16 Correct 138 ms 30848 KB Output is correct
17 Correct 170 ms 30664 KB Output is correct
18 Correct 165 ms 30636 KB Output is correct
19 Correct 126 ms 30804 KB Output is correct
20 Correct 144 ms 30712 KB Output is correct
21 Correct 16 ms 6144 KB Output is correct
22 Correct 165 ms 30940 KB Output is correct
23 Correct 183 ms 31084 KB Output is correct
24 Correct 144 ms 31048 KB Output is correct
25 Correct 170 ms 31004 KB Output is correct
26 Correct 141 ms 31120 KB Output is correct
27 Correct 181 ms 30968 KB Output is correct
28 Correct 171 ms 30968 KB Output is correct
29 Correct 154 ms 31080 KB Output is correct
30 Correct 172 ms 31060 KB Output is correct
31 Correct 206 ms 22448 KB Output is correct
32 Correct 146 ms 30712 KB Output is correct
33 Correct 353 ms 27668 KB Output is correct
34 Correct 169 ms 22320 KB Output is correct
35 Correct 261 ms 27100 KB Output is correct
36 Correct 159 ms 22380 KB Output is correct
37 Correct 319 ms 26836 KB Output is correct
38 Correct 224 ms 22776 KB Output is correct
39 Correct 145 ms 30712 KB Output is correct
40 Correct 318 ms 25772 KB Output is correct
41 Correct 7 ms 5348 KB Output is correct
42 Correct 7 ms 5504 KB Output is correct
43 Correct 7 ms 5452 KB Output is correct
44 Correct 7 ms 5376 KB Output is correct
45 Correct 7 ms 5376 KB Output is correct
46 Correct 7 ms 5376 KB Output is correct
47 Correct 7 ms 5376 KB Output is correct
48 Correct 7 ms 5376 KB Output is correct
49 Correct 8 ms 5376 KB Output is correct
50 Correct 7 ms 5504 KB Output is correct
51 Correct 215 ms 23064 KB Output is correct
52 Correct 152 ms 30968 KB Output is correct
53 Correct 296 ms 26404 KB Output is correct
54 Correct 141 ms 22552 KB Output is correct
55 Correct 194 ms 22832 KB Output is correct
56 Correct 155 ms 31044 KB Output is correct
57 Correct 238 ms 27012 KB Output is correct
58 Correct 145 ms 22552 KB Output is correct
59 Correct 205 ms 23080 KB Output is correct
60 Correct 194 ms 31060 KB Output is correct
61 Correct 244 ms 27128 KB Output is correct
62 Correct 151 ms 22220 KB Output is correct
63 Correct 229 ms 22744 KB Output is correct
64 Correct 205 ms 30960 KB Output is correct
65 Correct 346 ms 26928 KB Output is correct
66 Correct 164 ms 22680 KB Output is correct
67 Correct 234 ms 22900 KB Output is correct
68 Correct 151 ms 30968 KB Output is correct
69 Correct 247 ms 25996 KB Output is correct
70 Correct 152 ms 22748 KB Output is correct