#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, M, idx, a[100005], b[100005], c[100005], sz[100005], pre[100005], ft1[100005], ft2[100005], anc[100005][25], dep[100005];
vector<int> adj[100005], vec[100005];
int dfs(int n, int e = -1) {
anc[n][0] = e;
pre[n] = ++idx;
for (int i = 1; i <= 20; i++)
if (anc[n][i - 1] != -1)
anc[n][i] = anc[anc[n][i - 1]][i - 1];
for (auto u : adj[n])
if (u != e) {
dep[u] = dep[n] + 1;
sz[n] += dfs(u, n);
}
return ++sz[n];
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 20; i >= 0; i--)
if (dep[u] - (1 << i) >= dep[v]) u = anc[u][i];
if (u == v) return u;
for (int i = 20; i >= 0; i--)
if (anc[u][i] != anc[v][i]) {
u = anc[u][i];
v = anc[v][i];
}
return anc[u][0];
}
inline int ls(int x) { return x & -x; }
void upd1(int l, int r, int v) {
for (; l <= N; l += ls(l)) ft1[l] += v;
for (r++; r <= N; r += ls(r)) ft1[r] -= v;
}
void upd2(int l, int r, int v) {
for (; l <= N; l += ls(l)) ft2[l] += v;
for (r++; r <= N; r += ls(r)) ft2[r] -= v;
}
int qry1(int p) {
p = pre[p];
int r = 0;
for (; p; p -= ls(p)) r += ft1[p];
return r;
}
int qry2(int p) {
p = pre[p];
int r = 0;
for (; p; p -= ls(p)) r += ft2[p];
return r;
}
int dp(int n, int e = -1) {
int ret = 0;
for (auto u : adj[n])
if (u != e) ret += dp(u, n);
upd1(pre[n], pre[n] + sz[n] - 1, ret);
for (auto i : vec[n])
ret = max(ret, qry1(a[i]) + qry1(b[i]) - qry1(n) - (qry2(a[i]) + qry2(b[i]) - qry2(n)) + c[i]);
upd2(pre[n], pre[n] + sz[n] - 1, ret);
return ret;
}
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1, u, v; i < N; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
cin >> M;
for (int i = 1; i <= M; i++) {
cin >> a[i] >> b[i] >> c[i];
vec[lca(a[i], b[i])].pb(i);
}
cout << dp(1) << '\n';
}
Compilation message
election_campaign.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
95 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24532 KB |
Output is correct |
2 |
Correct |
11 ms |
24532 KB |
Output is correct |
3 |
Correct |
12 ms |
24532 KB |
Output is correct |
4 |
Correct |
13 ms |
24616 KB |
Output is correct |
5 |
Correct |
119 ms |
33560 KB |
Output is correct |
6 |
Correct |
60 ms |
40564 KB |
Output is correct |
7 |
Correct |
104 ms |
38272 KB |
Output is correct |
8 |
Correct |
82 ms |
33668 KB |
Output is correct |
9 |
Correct |
135 ms |
36716 KB |
Output is correct |
10 |
Correct |
78 ms |
33692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24612 KB |
Output is correct |
2 |
Correct |
12 ms |
24640 KB |
Output is correct |
3 |
Correct |
12 ms |
24748 KB |
Output is correct |
4 |
Correct |
173 ms |
45836 KB |
Output is correct |
5 |
Correct |
173 ms |
45828 KB |
Output is correct |
6 |
Correct |
132 ms |
45892 KB |
Output is correct |
7 |
Correct |
185 ms |
45836 KB |
Output is correct |
8 |
Correct |
175 ms |
45716 KB |
Output is correct |
9 |
Correct |
125 ms |
46028 KB |
Output is correct |
10 |
Correct |
150 ms |
45840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24612 KB |
Output is correct |
2 |
Correct |
12 ms |
24640 KB |
Output is correct |
3 |
Correct |
12 ms |
24748 KB |
Output is correct |
4 |
Correct |
173 ms |
45836 KB |
Output is correct |
5 |
Correct |
173 ms |
45828 KB |
Output is correct |
6 |
Correct |
132 ms |
45892 KB |
Output is correct |
7 |
Correct |
185 ms |
45836 KB |
Output is correct |
8 |
Correct |
175 ms |
45716 KB |
Output is correct |
9 |
Correct |
125 ms |
46028 KB |
Output is correct |
10 |
Correct |
150 ms |
45840 KB |
Output is correct |
11 |
Correct |
22 ms |
25900 KB |
Output is correct |
12 |
Correct |
177 ms |
46076 KB |
Output is correct |
13 |
Correct |
187 ms |
46120 KB |
Output is correct |
14 |
Correct |
153 ms |
46080 KB |
Output is correct |
15 |
Correct |
168 ms |
46116 KB |
Output is correct |
16 |
Correct |
130 ms |
46168 KB |
Output is correct |
17 |
Correct |
205 ms |
46108 KB |
Output is correct |
18 |
Correct |
149 ms |
46056 KB |
Output is correct |
19 |
Correct |
145 ms |
46112 KB |
Output is correct |
20 |
Correct |
199 ms |
46228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
38188 KB |
Output is correct |
2 |
Correct |
141 ms |
45872 KB |
Output is correct |
3 |
Correct |
300 ms |
43192 KB |
Output is correct |
4 |
Correct |
135 ms |
38372 KB |
Output is correct |
5 |
Correct |
254 ms |
42664 KB |
Output is correct |
6 |
Correct |
128 ms |
38304 KB |
Output is correct |
7 |
Correct |
304 ms |
42580 KB |
Output is correct |
8 |
Correct |
211 ms |
38460 KB |
Output is correct |
9 |
Correct |
149 ms |
45892 KB |
Output is correct |
10 |
Correct |
257 ms |
41484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24532 KB |
Output is correct |
2 |
Correct |
11 ms |
24532 KB |
Output is correct |
3 |
Correct |
12 ms |
24532 KB |
Output is correct |
4 |
Correct |
13 ms |
24616 KB |
Output is correct |
5 |
Correct |
119 ms |
33560 KB |
Output is correct |
6 |
Correct |
60 ms |
40564 KB |
Output is correct |
7 |
Correct |
104 ms |
38272 KB |
Output is correct |
8 |
Correct |
82 ms |
33668 KB |
Output is correct |
9 |
Correct |
135 ms |
36716 KB |
Output is correct |
10 |
Correct |
78 ms |
33692 KB |
Output is correct |
11 |
Correct |
12 ms |
24660 KB |
Output is correct |
12 |
Correct |
13 ms |
24740 KB |
Output is correct |
13 |
Correct |
13 ms |
24756 KB |
Output is correct |
14 |
Correct |
13 ms |
24756 KB |
Output is correct |
15 |
Correct |
13 ms |
24712 KB |
Output is correct |
16 |
Correct |
12 ms |
24752 KB |
Output is correct |
17 |
Correct |
13 ms |
24648 KB |
Output is correct |
18 |
Correct |
12 ms |
24788 KB |
Output is correct |
19 |
Correct |
12 ms |
24660 KB |
Output is correct |
20 |
Correct |
13 ms |
24772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24532 KB |
Output is correct |
2 |
Correct |
11 ms |
24532 KB |
Output is correct |
3 |
Correct |
12 ms |
24532 KB |
Output is correct |
4 |
Correct |
13 ms |
24616 KB |
Output is correct |
5 |
Correct |
119 ms |
33560 KB |
Output is correct |
6 |
Correct |
60 ms |
40564 KB |
Output is correct |
7 |
Correct |
104 ms |
38272 KB |
Output is correct |
8 |
Correct |
82 ms |
33668 KB |
Output is correct |
9 |
Correct |
135 ms |
36716 KB |
Output is correct |
10 |
Correct |
78 ms |
33692 KB |
Output is correct |
11 |
Correct |
11 ms |
24612 KB |
Output is correct |
12 |
Correct |
12 ms |
24640 KB |
Output is correct |
13 |
Correct |
12 ms |
24748 KB |
Output is correct |
14 |
Correct |
173 ms |
45836 KB |
Output is correct |
15 |
Correct |
173 ms |
45828 KB |
Output is correct |
16 |
Correct |
132 ms |
45892 KB |
Output is correct |
17 |
Correct |
185 ms |
45836 KB |
Output is correct |
18 |
Correct |
175 ms |
45716 KB |
Output is correct |
19 |
Correct |
125 ms |
46028 KB |
Output is correct |
20 |
Correct |
150 ms |
45840 KB |
Output is correct |
21 |
Correct |
22 ms |
25900 KB |
Output is correct |
22 |
Correct |
177 ms |
46076 KB |
Output is correct |
23 |
Correct |
187 ms |
46120 KB |
Output is correct |
24 |
Correct |
153 ms |
46080 KB |
Output is correct |
25 |
Correct |
168 ms |
46116 KB |
Output is correct |
26 |
Correct |
130 ms |
46168 KB |
Output is correct |
27 |
Correct |
205 ms |
46108 KB |
Output is correct |
28 |
Correct |
149 ms |
46056 KB |
Output is correct |
29 |
Correct |
145 ms |
46112 KB |
Output is correct |
30 |
Correct |
199 ms |
46228 KB |
Output is correct |
31 |
Correct |
175 ms |
38188 KB |
Output is correct |
32 |
Correct |
141 ms |
45872 KB |
Output is correct |
33 |
Correct |
300 ms |
43192 KB |
Output is correct |
34 |
Correct |
135 ms |
38372 KB |
Output is correct |
35 |
Correct |
254 ms |
42664 KB |
Output is correct |
36 |
Correct |
128 ms |
38304 KB |
Output is correct |
37 |
Correct |
304 ms |
42580 KB |
Output is correct |
38 |
Correct |
211 ms |
38460 KB |
Output is correct |
39 |
Correct |
149 ms |
45892 KB |
Output is correct |
40 |
Correct |
257 ms |
41484 KB |
Output is correct |
41 |
Correct |
12 ms |
24660 KB |
Output is correct |
42 |
Correct |
13 ms |
24740 KB |
Output is correct |
43 |
Correct |
13 ms |
24756 KB |
Output is correct |
44 |
Correct |
13 ms |
24756 KB |
Output is correct |
45 |
Correct |
13 ms |
24712 KB |
Output is correct |
46 |
Correct |
12 ms |
24752 KB |
Output is correct |
47 |
Correct |
13 ms |
24648 KB |
Output is correct |
48 |
Correct |
12 ms |
24788 KB |
Output is correct |
49 |
Correct |
12 ms |
24660 KB |
Output is correct |
50 |
Correct |
13 ms |
24772 KB |
Output is correct |
51 |
Correct |
223 ms |
38708 KB |
Output is correct |
52 |
Correct |
181 ms |
46132 KB |
Output is correct |
53 |
Correct |
301 ms |
42016 KB |
Output is correct |
54 |
Correct |
136 ms |
38584 KB |
Output is correct |
55 |
Correct |
191 ms |
38480 KB |
Output is correct |
56 |
Correct |
155 ms |
46020 KB |
Output is correct |
57 |
Correct |
232 ms |
42508 KB |
Output is correct |
58 |
Correct |
158 ms |
38524 KB |
Output is correct |
59 |
Correct |
212 ms |
38656 KB |
Output is correct |
60 |
Correct |
182 ms |
46048 KB |
Output is correct |
61 |
Correct |
278 ms |
42572 KB |
Output is correct |
62 |
Correct |
137 ms |
38500 KB |
Output is correct |
63 |
Correct |
204 ms |
38412 KB |
Output is correct |
64 |
Correct |
198 ms |
46060 KB |
Output is correct |
65 |
Correct |
266 ms |
42368 KB |
Output is correct |
66 |
Correct |
161 ms |
38688 KB |
Output is correct |
67 |
Correct |
175 ms |
38356 KB |
Output is correct |
68 |
Correct |
176 ms |
46004 KB |
Output is correct |
69 |
Correct |
229 ms |
41420 KB |
Output is correct |
70 |
Correct |
138 ms |
38740 KB |
Output is correct |