Submission #300595

# Submission time Handle Problem Language Result Execution time Memory
300595 2020-09-17T10:03:42 Z hexan Election Campaign (JOI15_election_campaign) C++14
100 / 100
373 ms 28152 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define all(x) (x).begin(), (x).end()
#define size(x) (ll)x.size()
#define chkmax(x, y) x = max(x, y)
#define chkmin(x, y) x = min(x, y)

const int N = (1 << 17);
vector<int> g[N];
int sz[N], dth[N], pa[N];

void dfs(int v, int p) {
	sz[v] = 1;
	for(int to : g[v]) {
		if (to == p) continue;
		pa[to] = v;
		dth[to] = dth[v] + 1;
		dfs(to, v);
		sz[v] += sz[to];
	}
	for(int i = 1; i < size(g[v]); i++) {
		if (g[v][i] == p) continue;
		if (g[v][0] == p || sz[g[v][0]] < sz[g[v][i]]) {
			swap(g[v][0], g[v][i]);
		}
	}
}

int tt = 0;
int id[N], rid[N];
int head[N];

void dfs2(int v, int p, int h) {
	head[v] = h;
	id[v] = tt++;
	rid[id[v]] = v;
	for(int to : g[v]) {
		if (to != p) {
			if (to == g[v][0]) {
				dfs2(to, v, h);
			} else {
				dfs2(to, v, to);
			}
		}
	}
}

int n;
ll t[2 * N];

void upd(int v, int val) {
	v += N - 1;
	t[v] += val;
	while (v > 0) {
		v = (v - 1) >> 1;
		t[v] = t[v * 2 + 1] + t[v * 2 + 2];
	}
}

ll get(int v, int tl, int tr, int l, int r) {
	if (l >= r) return 0;
	if (tl == l && tr == r) return t[v];
	int tm = (tl + tr) / 2;
	return get(v * 2 + 1, tl, tm, l, min(r, tm)) + get(v * 2 + 2, tm, tr, max(l, tm), r);
}

ll get(int l, int r) {
	return get(0, 0, N, l, r + 1);
}

ll sum(int p, int x) {
	ll res = 0;
	while (head[p] != head[x]) {
		res += get(id[head[x]], id[x]);
		x = pa[head[x]];
	}
	res += get(id[p], id[x]);
	return res;
}


int lca(int a, int b) {
	while (1) {
		if (head[a] == head[b]) {
			if (dth[a] < dth[b]) return a;
			else return b;
		}
		if (dth[head[a]] < dth[head[b]]) swap(a, b);
		a = pa[head[a]];
	}
}

int a1[N], b1[N], c[N];
vector<int> e[N];
ll dp[N];
ll cur[N];

void Do(int v, int p) {
	for(int to : g[v]) {
		if (to != p) {
			Do(to, v);
		}
	}
	for(int to : g[v]) {
		if (to != p) {
			dp[v] += dp[to];
		}
	}
	for(int x : e[v]) {
		int a = a1[x], b = b1[x];
		chkmax(dp[v], sum(v, a) + sum(v, b) - cur[v] + c[x]);
	}
	if (p != -1) {
		cur[p] += dp[v];
		upd(id[p], dp[v]);
		upd(id[v], -dp[v]);
	}
}

void run() {
	cin >> n;
	for(int i = 0; i < n - 1; i++) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(0, -1);
	dfs2(0, -1, 0);
	int m;
	cin >> m;
	for(int i = 0; i < m; i++) {
		cin >> a1[i] >> b1[i] >> c[i];
		--a1[i]; --b1[i];
		e[lca(a1[i], b1[i])].push_back(i);
	}
	Do(0, -1);
	cout << dp[0];
}

int main() {
#ifdef LOCAL
	freopen("input", "r", stdin);
	freopen("output", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	run();
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 5 ms 6528 KB Output is correct
4 Correct 5 ms 6656 KB Output is correct
5 Correct 119 ms 16504 KB Output is correct
6 Correct 60 ms 24184 KB Output is correct
7 Correct 103 ms 21496 KB Output is correct
8 Correct 76 ms 15352 KB Output is correct
9 Correct 85 ms 19960 KB Output is correct
10 Correct 79 ms 15352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6784 KB Output is correct
4 Correct 142 ms 27768 KB Output is correct
5 Correct 147 ms 27768 KB Output is correct
6 Correct 136 ms 27768 KB Output is correct
7 Correct 146 ms 27768 KB Output is correct
8 Correct 162 ms 27768 KB Output is correct
9 Correct 132 ms 27896 KB Output is correct
10 Correct 152 ms 27768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6656 KB Output is correct
3 Correct 5 ms 6784 KB Output is correct
4 Correct 142 ms 27768 KB Output is correct
5 Correct 147 ms 27768 KB Output is correct
6 Correct 136 ms 27768 KB Output is correct
7 Correct 146 ms 27768 KB Output is correct
8 Correct 162 ms 27768 KB Output is correct
9 Correct 132 ms 27896 KB Output is correct
10 Correct 152 ms 27768 KB Output is correct
11 Correct 20 ms 7552 KB Output is correct
12 Correct 149 ms 28024 KB Output is correct
13 Correct 163 ms 28024 KB Output is correct
14 Correct 140 ms 28152 KB Output is correct
15 Correct 148 ms 28024 KB Output is correct
16 Correct 140 ms 28024 KB Output is correct
17 Correct 161 ms 28024 KB Output is correct
18 Correct 153 ms 28024 KB Output is correct
19 Correct 142 ms 28024 KB Output is correct
20 Correct 153 ms 28024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 373 ms 19572 KB Output is correct
2 Correct 139 ms 27768 KB Output is correct
3 Correct 226 ms 24696 KB Output is correct
4 Correct 196 ms 18296 KB Output is correct
5 Correct 202 ms 24312 KB Output is correct
6 Correct 187 ms 18472 KB Output is correct
7 Correct 229 ms 23928 KB Output is correct
8 Correct 267 ms 20088 KB Output is correct
9 Correct 132 ms 27768 KB Output is correct
10 Correct 210 ms 23028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 5 ms 6528 KB Output is correct
4 Correct 5 ms 6656 KB Output is correct
5 Correct 119 ms 16504 KB Output is correct
6 Correct 60 ms 24184 KB Output is correct
7 Correct 103 ms 21496 KB Output is correct
8 Correct 76 ms 15352 KB Output is correct
9 Correct 85 ms 19960 KB Output is correct
10 Correct 79 ms 15352 KB Output is correct
11 Correct 7 ms 6656 KB Output is correct
12 Correct 6 ms 6784 KB Output is correct
13 Correct 6 ms 6784 KB Output is correct
14 Correct 6 ms 6784 KB Output is correct
15 Correct 7 ms 6784 KB Output is correct
16 Correct 6 ms 6784 KB Output is correct
17 Correct 6 ms 6784 KB Output is correct
18 Correct 6 ms 6912 KB Output is correct
19 Correct 6 ms 6784 KB Output is correct
20 Correct 6 ms 6784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 5 ms 6528 KB Output is correct
4 Correct 5 ms 6656 KB Output is correct
5 Correct 119 ms 16504 KB Output is correct
6 Correct 60 ms 24184 KB Output is correct
7 Correct 103 ms 21496 KB Output is correct
8 Correct 76 ms 15352 KB Output is correct
9 Correct 85 ms 19960 KB Output is correct
10 Correct 79 ms 15352 KB Output is correct
11 Correct 5 ms 6528 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 5 ms 6784 KB Output is correct
14 Correct 142 ms 27768 KB Output is correct
15 Correct 147 ms 27768 KB Output is correct
16 Correct 136 ms 27768 KB Output is correct
17 Correct 146 ms 27768 KB Output is correct
18 Correct 162 ms 27768 KB Output is correct
19 Correct 132 ms 27896 KB Output is correct
20 Correct 152 ms 27768 KB Output is correct
21 Correct 20 ms 7552 KB Output is correct
22 Correct 149 ms 28024 KB Output is correct
23 Correct 163 ms 28024 KB Output is correct
24 Correct 140 ms 28152 KB Output is correct
25 Correct 148 ms 28024 KB Output is correct
26 Correct 140 ms 28024 KB Output is correct
27 Correct 161 ms 28024 KB Output is correct
28 Correct 153 ms 28024 KB Output is correct
29 Correct 142 ms 28024 KB Output is correct
30 Correct 153 ms 28024 KB Output is correct
31 Correct 373 ms 19572 KB Output is correct
32 Correct 139 ms 27768 KB Output is correct
33 Correct 226 ms 24696 KB Output is correct
34 Correct 196 ms 18296 KB Output is correct
35 Correct 202 ms 24312 KB Output is correct
36 Correct 187 ms 18472 KB Output is correct
37 Correct 229 ms 23928 KB Output is correct
38 Correct 267 ms 20088 KB Output is correct
39 Correct 132 ms 27768 KB Output is correct
40 Correct 210 ms 23028 KB Output is correct
41 Correct 7 ms 6656 KB Output is correct
42 Correct 6 ms 6784 KB Output is correct
43 Correct 6 ms 6784 KB Output is correct
44 Correct 6 ms 6784 KB Output is correct
45 Correct 7 ms 6784 KB Output is correct
46 Correct 6 ms 6784 KB Output is correct
47 Correct 6 ms 6784 KB Output is correct
48 Correct 6 ms 6912 KB Output is correct
49 Correct 6 ms 6784 KB Output is correct
50 Correct 6 ms 6784 KB Output is correct
51 Correct 269 ms 20344 KB Output is correct
52 Correct 151 ms 28024 KB Output is correct
53 Correct 228 ms 23412 KB Output is correct
54 Correct 175 ms 18936 KB Output is correct
55 Correct 347 ms 19824 KB Output is correct
56 Correct 152 ms 28028 KB Output is correct
57 Correct 203 ms 24184 KB Output is correct
58 Correct 195 ms 18680 KB Output is correct
59 Correct 270 ms 20216 KB Output is correct
60 Correct 151 ms 28024 KB Output is correct
61 Correct 206 ms 24296 KB Output is correct
62 Correct 193 ms 18736 KB Output is correct
63 Correct 362 ms 19872 KB Output is correct
64 Correct 147 ms 28024 KB Output is correct
65 Correct 247 ms 24052 KB Output is correct
66 Correct 175 ms 18808 KB Output is correct
67 Correct 351 ms 20088 KB Output is correct
68 Correct 150 ms 28024 KB Output is correct
69 Correct 216 ms 23044 KB Output is correct
70 Correct 186 ms 18940 KB Output is correct