Submission #872218

# Submission time Handle Problem Language Result Execution time Memory
872218 2023-11-12T14:16:54 Z serifefedartar Election Campaign (JOI15_election_campaign) C++17
100 / 100
146 ms 32596 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9+9;
const ll LOGN = 20; 
const ll MAXN = 1e5 + 100;

vector<vector<int>> graph;
vector<pair<pair<int,int>, int>> plan[MAXN];
int tin[MAXN], tout[MAXN], T = 0;
int up[LOGN][MAXN], depth[MAXN], dp[MAXN];
int fen_node[MAXN], fen_subs[MAXN];
int get_node(int k) {
	int res = 0;
	while (k > 0) {
		res += fen_node[k];
		k -= k & -k;
	}
	return res;
}

int get_subs(int k) {
	int res = 0;
	while (k > 0) {
		res += fen_subs[k];
		k -= k & -k;
	}
	return res;
}

void add_node(int k, int val) {
	while (k < MAXN) {
		fen_node[k] += val;
		k += k & -k;
	}
}

void add_subs(int k, int val) {
	while (k < MAXN) {
		fen_subs[k] += val;
		k += k & -k;
	}
}

void dfs(int node, int parent) {
	tin[node] = ++T;
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		depth[u] = depth[node] + 1;
		up[0][u] = node;
		for (int i = 1; i < LOGN; i++)
			up[i][u] = up[i-1][up[i-1][u]];
		dfs(u, node);
	}
	tout[node] = T;
}

int find(int node, int k) {
	for (int i = 0; i < LOGN; i++) {
		if ((1<<i) & k)
			node = up[i][node];
	}
	return node;
}

int lca(int a, int b) {
	if (depth[b] > depth[a])
		swap(a, b);
	a = find(a, depth[a] - depth[b]);

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

void dfs2(int node, int parent) {
	int sum = 0;
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		dfs2(u, node);
		sum += dp[u];
	}

	dp[node] = sum;
	for (auto u : plan[node]) {
		int now = get_subs(tin[u.f.f]) + get_subs(tin[u.f.s]) - 2 * get_subs(tin[node]) + get_subs(tin[node]) - (node == 1 ? 0 : get_subs(tin[parent]));
		now -= get_node(tin[u.f.f]) + get_node(tin[u.f.s]) - 2 * get_node(tin[node]);
		dp[node] = max(dp[node], now + u.s);
	}
	add_node(tin[node], dp[node]);
	add_node(tout[node]+1, -dp[node]);
	if (node != 1) {
		add_subs(tin[parent], dp[node]);
		add_subs(tout[parent]+1, -dp[node]);
	}
}

int main() {
	fast
	int N, M, a, b, c;
	cin >> N;

	graph = vector<vector<int>>(N+1, vector<int>());
	for (int i = 1; i < N; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	for (int i = 0; i < LOGN; i++)
		up[i][1] = 1;
	dfs(1, 1);

	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> a >> b >> c;
		plan[lca(a, b)].push_back({{a, b}, c});
	}
	dfs2(1, 1);
	cout << dp[1] << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 53 ms 19796 KB Output is correct
6 Correct 35 ms 28992 KB Output is correct
7 Correct 56 ms 25684 KB Output is correct
8 Correct 44 ms 19804 KB Output is correct
9 Correct 51 ms 23772 KB Output is correct
10 Correct 43 ms 20052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12980 KB Output is correct
4 Correct 79 ms 32084 KB Output is correct
5 Correct 80 ms 32288 KB Output is correct
6 Correct 73 ms 32340 KB Output is correct
7 Correct 76 ms 32088 KB Output is correct
8 Correct 82 ms 32084 KB Output is correct
9 Correct 71 ms 32192 KB Output is correct
10 Correct 80 ms 32236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12980 KB Output is correct
4 Correct 79 ms 32084 KB Output is correct
5 Correct 80 ms 32288 KB Output is correct
6 Correct 73 ms 32340 KB Output is correct
7 Correct 76 ms 32088 KB Output is correct
8 Correct 82 ms 32084 KB Output is correct
9 Correct 71 ms 32192 KB Output is correct
10 Correct 80 ms 32236 KB Output is correct
11 Correct 9 ms 13660 KB Output is correct
12 Correct 81 ms 32344 KB Output is correct
13 Correct 84 ms 32380 KB Output is correct
14 Correct 75 ms 32596 KB Output is correct
15 Correct 79 ms 32396 KB Output is correct
16 Correct 74 ms 32596 KB Output is correct
17 Correct 84 ms 32400 KB Output is correct
18 Correct 80 ms 32400 KB Output is correct
19 Correct 74 ms 32596 KB Output is correct
20 Correct 81 ms 32596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 19980 KB Output is correct
2 Correct 71 ms 32252 KB Output is correct
3 Correct 106 ms 28500 KB Output is correct
4 Correct 75 ms 22804 KB Output is correct
5 Correct 105 ms 28252 KB Output is correct
6 Correct 73 ms 22844 KB Output is correct
7 Correct 105 ms 27672 KB Output is correct
8 Correct 88 ms 22680 KB Output is correct
9 Correct 73 ms 32340 KB Output is correct
10 Correct 104 ms 26380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 53 ms 19796 KB Output is correct
6 Correct 35 ms 28992 KB Output is correct
7 Correct 56 ms 25684 KB Output is correct
8 Correct 44 ms 19804 KB Output is correct
9 Correct 51 ms 23772 KB Output is correct
10 Correct 43 ms 20052 KB Output is correct
11 Correct 2 ms 12892 KB Output is correct
12 Correct 2 ms 12892 KB Output is correct
13 Correct 2 ms 12892 KB Output is correct
14 Correct 2 ms 12816 KB Output is correct
15 Correct 2 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 3 ms 12892 KB Output is correct
18 Correct 3 ms 12892 KB Output is correct
19 Correct 2 ms 12892 KB Output is correct
20 Correct 2 ms 13092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 53 ms 19796 KB Output is correct
6 Correct 35 ms 28992 KB Output is correct
7 Correct 56 ms 25684 KB Output is correct
8 Correct 44 ms 19804 KB Output is correct
9 Correct 51 ms 23772 KB Output is correct
10 Correct 43 ms 20052 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12980 KB Output is correct
14 Correct 79 ms 32084 KB Output is correct
15 Correct 80 ms 32288 KB Output is correct
16 Correct 73 ms 32340 KB Output is correct
17 Correct 76 ms 32088 KB Output is correct
18 Correct 82 ms 32084 KB Output is correct
19 Correct 71 ms 32192 KB Output is correct
20 Correct 80 ms 32236 KB Output is correct
21 Correct 9 ms 13660 KB Output is correct
22 Correct 81 ms 32344 KB Output is correct
23 Correct 84 ms 32380 KB Output is correct
24 Correct 75 ms 32596 KB Output is correct
25 Correct 79 ms 32396 KB Output is correct
26 Correct 74 ms 32596 KB Output is correct
27 Correct 84 ms 32400 KB Output is correct
28 Correct 80 ms 32400 KB Output is correct
29 Correct 74 ms 32596 KB Output is correct
30 Correct 81 ms 32596 KB Output is correct
31 Correct 99 ms 19980 KB Output is correct
32 Correct 71 ms 32252 KB Output is correct
33 Correct 106 ms 28500 KB Output is correct
34 Correct 75 ms 22804 KB Output is correct
35 Correct 105 ms 28252 KB Output is correct
36 Correct 73 ms 22844 KB Output is correct
37 Correct 105 ms 27672 KB Output is correct
38 Correct 88 ms 22680 KB Output is correct
39 Correct 73 ms 32340 KB Output is correct
40 Correct 104 ms 26380 KB Output is correct
41 Correct 2 ms 12892 KB Output is correct
42 Correct 2 ms 12892 KB Output is correct
43 Correct 2 ms 12892 KB Output is correct
44 Correct 2 ms 12816 KB Output is correct
45 Correct 2 ms 12892 KB Output is correct
46 Correct 3 ms 12892 KB Output is correct
47 Correct 3 ms 12892 KB Output is correct
48 Correct 3 ms 12892 KB Output is correct
49 Correct 2 ms 12892 KB Output is correct
50 Correct 2 ms 13092 KB Output is correct
51 Correct 104 ms 23080 KB Output is correct
52 Correct 79 ms 32556 KB Output is correct
53 Correct 126 ms 26996 KB Output is correct
54 Correct 98 ms 22944 KB Output is correct
55 Correct 110 ms 22880 KB Output is correct
56 Correct 84 ms 32572 KB Output is correct
57 Correct 131 ms 27732 KB Output is correct
58 Correct 100 ms 23052 KB Output is correct
59 Correct 131 ms 22940 KB Output is correct
60 Correct 88 ms 32340 KB Output is correct
61 Correct 123 ms 27736 KB Output is correct
62 Correct 103 ms 23092 KB Output is correct
63 Correct 123 ms 22592 KB Output is correct
64 Correct 85 ms 32504 KB Output is correct
65 Correct 134 ms 27672 KB Output is correct
66 Correct 85 ms 23036 KB Output is correct
67 Correct 136 ms 22804 KB Output is correct
68 Correct 106 ms 32392 KB Output is correct
69 Correct 146 ms 26312 KB Output is correct
70 Correct 93 ms 23204 KB Output is correct