답안 #901657

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
901657 2024-01-09T21:41:32 Z Hakiers Election Campaign (JOI15_election_campaign) C++17
100 / 100
181 ms 39736 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int BASE = 1 << 18;
constexpr int MAXN = 1e5+7;
vector<int> G[MAXN];
ll TREE[BASE<<1][2];
vector<tuple<int, int, int>> event[MAXN];
int dp[MAXN][2];
int pre[MAXN], post[MAXN];
int depth[MAXN];
int jmp[MAXN][18];
int tajm;
int n, m;

void update(int v, ll val, bool x){
	v += BASE;
	TREE[v][x] = val;
	v/=2;
	while(v>0){
		TREE[v][x] = TREE[2*v][x] + TREE[2*v+1][x];
		v/=2;
	}
}

ll query(int a, int b, bool x){
	a += BASE-1;
	b += BASE+1;
	ll out = 0;
	while(a/2 != b/2){
		if(a%2 == 0) out += TREE[a+1][x];
		if(b%2 == 1) out += TREE[b-1][x];
		a/=2; b/=2;
	}
	return out;
} 

int lca(int v, int u){
	if(depth[u] < depth[v]) swap(u, v);
	
	for(int k = 17; k >= 0; k--)
		if(depth[jmp[u][k]] >= depth[v]) u = jmp[u][k];
	
	if(u == v) return v;
	
	for(int k = 17; k >= 0; k--){
		if(jmp[u][k] != jmp[v][k]){
			u = jmp[u][k];
			v = jmp[v][k];
		}
	}
	
	return jmp[v][0];
}

void dfs(int v, int p){
	depth[v] = depth[p]+1;
	pre[v] = ++tajm;
	jmp[v][0] = p;
	for(int k = 1; k <= 17; k++)
		jmp[v][k] = jmp[jmp[v][k-1]][k-1];
	
	for(auto u : G[v]){
		if(u == p) continue;
		dfs(u, v);
	}
	
	post[v] = ++tajm;
}

void dfs2(int v, int p){
	
	for(auto u : G[v]){
		if(u == p) continue;
		dfs2(u, v);
		dp[v][0] += dp[u][1];
	}
	dp[v][1] = dp[v][0];
	
	for(auto [a, b, c] : event[v]){
		ll path1 = query(pre[v], pre[a], 0) - query(pre[v], pre[a], 1);
		ll path2 = query(pre[v], pre[b], 0) - query(pre[v], pre[b], 1);
		dp[v][1] = max((ll)dp[v][1], (ll)dp[v][0] + c + path1 + path2);
	}
	
	update(pre[v], dp[v][0], 0);
	update(post[v], -dp[v][0], 0);
	update(pre[v], dp[v][1], 1);
	update(post[v], -dp[v][1], 1);
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for(int i = 1; i < n; i++){
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(1, 1);
	cin >> m;
	for(int i = 1; i <= m; i++){
		int a, b, c;
		cin >> a >> b >> c;
		event[lca(a, b)].push_back({a, b, c});
	}
	dfs2(1, 1);
	cout << dp[1][1] << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11868 KB Output is correct
2 Correct 2 ms 11868 KB Output is correct
3 Correct 2 ms 11864 KB Output is correct
4 Correct 2 ms 11868 KB Output is correct
5 Correct 63 ms 26572 KB Output is correct
6 Correct 48 ms 35928 KB Output is correct
7 Correct 63 ms 32608 KB Output is correct
8 Correct 49 ms 27040 KB Output is correct
9 Correct 66 ms 30908 KB Output is correct
10 Correct 56 ms 27144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11868 KB Output is correct
2 Correct 3 ms 11868 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 120 ms 39436 KB Output is correct
5 Correct 114 ms 39252 KB Output is correct
6 Correct 97 ms 39256 KB Output is correct
7 Correct 125 ms 39308 KB Output is correct
8 Correct 114 ms 39172 KB Output is correct
9 Correct 98 ms 39252 KB Output is correct
10 Correct 119 ms 39084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 11868 KB Output is correct
2 Correct 3 ms 11868 KB Output is correct
3 Correct 3 ms 12124 KB Output is correct
4 Correct 120 ms 39436 KB Output is correct
5 Correct 114 ms 39252 KB Output is correct
6 Correct 97 ms 39256 KB Output is correct
7 Correct 125 ms 39308 KB Output is correct
8 Correct 114 ms 39172 KB Output is correct
9 Correct 98 ms 39252 KB Output is correct
10 Correct 119 ms 39084 KB Output is correct
11 Correct 14 ms 12888 KB Output is correct
12 Correct 115 ms 39564 KB Output is correct
13 Correct 118 ms 39524 KB Output is correct
14 Correct 123 ms 39628 KB Output is correct
15 Correct 117 ms 39384 KB Output is correct
16 Correct 104 ms 39736 KB Output is correct
17 Correct 115 ms 39600 KB Output is correct
18 Correct 117 ms 39520 KB Output is correct
19 Correct 101 ms 39508 KB Output is correct
20 Correct 116 ms 39472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 29512 KB Output is correct
2 Correct 96 ms 39292 KB Output is correct
3 Correct 154 ms 35568 KB Output is correct
4 Correct 122 ms 29960 KB Output is correct
5 Correct 129 ms 34976 KB Output is correct
6 Correct 120 ms 29772 KB Output is correct
7 Correct 171 ms 34672 KB Output is correct
8 Correct 134 ms 29776 KB Output is correct
9 Correct 102 ms 39248 KB Output is correct
10 Correct 164 ms 33588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11868 KB Output is correct
2 Correct 2 ms 11868 KB Output is correct
3 Correct 2 ms 11864 KB Output is correct
4 Correct 2 ms 11868 KB Output is correct
5 Correct 63 ms 26572 KB Output is correct
6 Correct 48 ms 35928 KB Output is correct
7 Correct 63 ms 32608 KB Output is correct
8 Correct 49 ms 27040 KB Output is correct
9 Correct 66 ms 30908 KB Output is correct
10 Correct 56 ms 27144 KB Output is correct
11 Correct 3 ms 11868 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 3 ms 11936 KB Output is correct
15 Correct 3 ms 11872 KB Output is correct
16 Correct 3 ms 12124 KB Output is correct
17 Correct 3 ms 11868 KB Output is correct
18 Correct 3 ms 12124 KB Output is correct
19 Correct 3 ms 12124 KB Output is correct
20 Correct 3 ms 12120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11868 KB Output is correct
2 Correct 2 ms 11868 KB Output is correct
3 Correct 2 ms 11864 KB Output is correct
4 Correct 2 ms 11868 KB Output is correct
5 Correct 63 ms 26572 KB Output is correct
6 Correct 48 ms 35928 KB Output is correct
7 Correct 63 ms 32608 KB Output is correct
8 Correct 49 ms 27040 KB Output is correct
9 Correct 66 ms 30908 KB Output is correct
10 Correct 56 ms 27144 KB Output is correct
11 Correct 2 ms 11868 KB Output is correct
12 Correct 3 ms 11868 KB Output is correct
13 Correct 3 ms 12124 KB Output is correct
14 Correct 120 ms 39436 KB Output is correct
15 Correct 114 ms 39252 KB Output is correct
16 Correct 97 ms 39256 KB Output is correct
17 Correct 125 ms 39308 KB Output is correct
18 Correct 114 ms 39172 KB Output is correct
19 Correct 98 ms 39252 KB Output is correct
20 Correct 119 ms 39084 KB Output is correct
21 Correct 14 ms 12888 KB Output is correct
22 Correct 115 ms 39564 KB Output is correct
23 Correct 118 ms 39524 KB Output is correct
24 Correct 123 ms 39628 KB Output is correct
25 Correct 117 ms 39384 KB Output is correct
26 Correct 104 ms 39736 KB Output is correct
27 Correct 115 ms 39600 KB Output is correct
28 Correct 117 ms 39520 KB Output is correct
29 Correct 101 ms 39508 KB Output is correct
30 Correct 116 ms 39472 KB Output is correct
31 Correct 138 ms 29512 KB Output is correct
32 Correct 96 ms 39292 KB Output is correct
33 Correct 154 ms 35568 KB Output is correct
34 Correct 122 ms 29960 KB Output is correct
35 Correct 129 ms 34976 KB Output is correct
36 Correct 120 ms 29772 KB Output is correct
37 Correct 171 ms 34672 KB Output is correct
38 Correct 134 ms 29776 KB Output is correct
39 Correct 102 ms 39248 KB Output is correct
40 Correct 164 ms 33588 KB Output is correct
41 Correct 3 ms 11868 KB Output is correct
42 Correct 3 ms 12124 KB Output is correct
43 Correct 3 ms 12124 KB Output is correct
44 Correct 3 ms 11936 KB Output is correct
45 Correct 3 ms 11872 KB Output is correct
46 Correct 3 ms 12124 KB Output is correct
47 Correct 3 ms 11868 KB Output is correct
48 Correct 3 ms 12124 KB Output is correct
49 Correct 3 ms 12124 KB Output is correct
50 Correct 3 ms 12120 KB Output is correct
51 Correct 145 ms 30032 KB Output is correct
52 Correct 123 ms 39448 KB Output is correct
53 Correct 161 ms 33796 KB Output is correct
54 Correct 114 ms 30204 KB Output is correct
55 Correct 163 ms 29884 KB Output is correct
56 Correct 116 ms 39536 KB Output is correct
57 Correct 141 ms 34596 KB Output is correct
58 Correct 123 ms 29984 KB Output is correct
59 Correct 145 ms 30004 KB Output is correct
60 Correct 124 ms 39516 KB Output is correct
61 Correct 140 ms 34904 KB Output is correct
62 Correct 168 ms 29996 KB Output is correct
63 Correct 138 ms 29548 KB Output is correct
64 Correct 125 ms 39504 KB Output is correct
65 Correct 171 ms 34696 KB Output is correct
66 Correct 112 ms 30096 KB Output is correct
67 Correct 181 ms 29768 KB Output is correct
68 Correct 116 ms 39508 KB Output is correct
69 Correct 142 ms 33368 KB Output is correct
70 Correct 124 ms 30396 KB Output is correct