#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
const int N = int(2e5) + 7;
const int logN = 17;
int n, m, u, v, cur, res, fr[N], to[N], c[N], f[N], g[N];
int h[N], p[N][logN], Time = 0, in[N], out[N], t[N];
vector<int> q[N], a[N];
void BuildTree(int u, int par) {
p[u][0] = par; in[u] = ++Time;
for(int i = 1; i < logN; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
for(int v: a[u]) {
if(v == par) continue;
h[v] = h[u] + 1;
BuildTree(v, u);
}
out[u] = Time;
}
int LCA(int u, int v) {
if(h[u] < h[v]) swap(u, v);
for(int i = logN - 1; i >= 0; --i)
if(h[u] - (1 << i) >= h[v]) u = p[u][i];
if(u == v) return u;
for(int i = logN - 1; i >= 0; --i)
if(p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return p[u][0];
}
void Update(int x, int val) {for(; x <= n; x += (x & -x)) t[x] += val;}
int Query(int x) {int res = 0; for(; x > 0; x -= (x & -x)) res += t[x]; return res;}
void GetAns(int u) {
for(int v: a[u]) {
if(v == p[u][0]) continue;
GetAns(v); g[u] += f[v];
}
for(int v: a[u]) {
if(v == p[u][0]) continue;
Update(in[v], g[u] - f[v]);
Update(out[v] + 1, f[v] - g[u]);
}
f[u] = g[u];
for(int i: q[u]) {
cur = g[fr[i]] + g[to[i]] - g[u] + Query(in[fr[i]]) + Query(in[to[i]]) + c[i];
f[u] = max(f[u], cur);
}
res = max(res, f[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n;
for(int i = 1; i < n; ++i) {
cin >> u >> v;
a[u].pb(v), a[v].pb(u);
}
BuildTree(1, 1);
cin >> m;
for(int i = 0; i < m; ++i) {
cin >> fr[i] >> to[i] >> c[i];
q[LCA(fr[i], to[i])].pb(i);
}
res = 0;
GetAns(1);
cout << res;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
123 ms |
23164 KB |
Output is correct |
6 |
Correct |
74 ms |
30840 KB |
Output is correct |
7 |
Correct |
130 ms |
28128 KB |
Output is correct |
8 |
Correct |
96 ms |
23164 KB |
Output is correct |
9 |
Correct |
126 ms |
26716 KB |
Output is correct |
10 |
Correct |
99 ms |
23292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9852 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
12 ms |
9980 KB |
Output is correct |
4 |
Correct |
168 ms |
34436 KB |
Output is correct |
5 |
Correct |
171 ms |
34424 KB |
Output is correct |
6 |
Correct |
149 ms |
34480 KB |
Output is correct |
7 |
Correct |
167 ms |
34424 KB |
Output is correct |
8 |
Correct |
186 ms |
34656 KB |
Output is correct |
9 |
Correct |
153 ms |
34552 KB |
Output is correct |
10 |
Correct |
284 ms |
34404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9852 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
12 ms |
9980 KB |
Output is correct |
4 |
Correct |
168 ms |
34436 KB |
Output is correct |
5 |
Correct |
171 ms |
34424 KB |
Output is correct |
6 |
Correct |
149 ms |
34480 KB |
Output is correct |
7 |
Correct |
167 ms |
34424 KB |
Output is correct |
8 |
Correct |
186 ms |
34656 KB |
Output is correct |
9 |
Correct |
153 ms |
34552 KB |
Output is correct |
10 |
Correct |
284 ms |
34404 KB |
Output is correct |
11 |
Correct |
23 ms |
10744 KB |
Output is correct |
12 |
Correct |
169 ms |
34740 KB |
Output is correct |
13 |
Correct |
212 ms |
34772 KB |
Output is correct |
14 |
Correct |
176 ms |
34936 KB |
Output is correct |
15 |
Correct |
219 ms |
34936 KB |
Output is correct |
16 |
Correct |
162 ms |
34808 KB |
Output is correct |
17 |
Correct |
183 ms |
34744 KB |
Output is correct |
18 |
Correct |
175 ms |
34768 KB |
Output is correct |
19 |
Correct |
160 ms |
34884 KB |
Output is correct |
20 |
Correct |
173 ms |
34784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
25988 KB |
Output is correct |
2 |
Correct |
157 ms |
34552 KB |
Output is correct |
3 |
Correct |
297 ms |
31476 KB |
Output is correct |
4 |
Correct |
158 ms |
26228 KB |
Output is correct |
5 |
Correct |
240 ms |
31068 KB |
Output is correct |
6 |
Correct |
151 ms |
26488 KB |
Output is correct |
7 |
Correct |
263 ms |
30784 KB |
Output is correct |
8 |
Correct |
211 ms |
26744 KB |
Output is correct |
9 |
Correct |
160 ms |
34476 KB |
Output is correct |
10 |
Correct |
265 ms |
29852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
123 ms |
23164 KB |
Output is correct |
6 |
Correct |
74 ms |
30840 KB |
Output is correct |
7 |
Correct |
130 ms |
28128 KB |
Output is correct |
8 |
Correct |
96 ms |
23164 KB |
Output is correct |
9 |
Correct |
126 ms |
26716 KB |
Output is correct |
10 |
Correct |
99 ms |
23292 KB |
Output is correct |
11 |
Correct |
12 ms |
9976 KB |
Output is correct |
12 |
Correct |
12 ms |
10104 KB |
Output is correct |
13 |
Correct |
12 ms |
9976 KB |
Output is correct |
14 |
Correct |
12 ms |
9976 KB |
Output is correct |
15 |
Correct |
12 ms |
9976 KB |
Output is correct |
16 |
Correct |
12 ms |
9976 KB |
Output is correct |
17 |
Correct |
12 ms |
9976 KB |
Output is correct |
18 |
Correct |
12 ms |
9976 KB |
Output is correct |
19 |
Correct |
12 ms |
9976 KB |
Output is correct |
20 |
Correct |
12 ms |
9976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
123 ms |
23164 KB |
Output is correct |
6 |
Correct |
74 ms |
30840 KB |
Output is correct |
7 |
Correct |
130 ms |
28128 KB |
Output is correct |
8 |
Correct |
96 ms |
23164 KB |
Output is correct |
9 |
Correct |
126 ms |
26716 KB |
Output is correct |
10 |
Correct |
99 ms |
23292 KB |
Output is correct |
11 |
Correct |
11 ms |
9852 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
12 ms |
9980 KB |
Output is correct |
14 |
Correct |
168 ms |
34436 KB |
Output is correct |
15 |
Correct |
171 ms |
34424 KB |
Output is correct |
16 |
Correct |
149 ms |
34480 KB |
Output is correct |
17 |
Correct |
167 ms |
34424 KB |
Output is correct |
18 |
Correct |
186 ms |
34656 KB |
Output is correct |
19 |
Correct |
153 ms |
34552 KB |
Output is correct |
20 |
Correct |
284 ms |
34404 KB |
Output is correct |
21 |
Correct |
23 ms |
10744 KB |
Output is correct |
22 |
Correct |
169 ms |
34740 KB |
Output is correct |
23 |
Correct |
212 ms |
34772 KB |
Output is correct |
24 |
Correct |
176 ms |
34936 KB |
Output is correct |
25 |
Correct |
219 ms |
34936 KB |
Output is correct |
26 |
Correct |
162 ms |
34808 KB |
Output is correct |
27 |
Correct |
183 ms |
34744 KB |
Output is correct |
28 |
Correct |
175 ms |
34768 KB |
Output is correct |
29 |
Correct |
160 ms |
34884 KB |
Output is correct |
30 |
Correct |
173 ms |
34784 KB |
Output is correct |
31 |
Correct |
207 ms |
25988 KB |
Output is correct |
32 |
Correct |
157 ms |
34552 KB |
Output is correct |
33 |
Correct |
297 ms |
31476 KB |
Output is correct |
34 |
Correct |
158 ms |
26228 KB |
Output is correct |
35 |
Correct |
240 ms |
31068 KB |
Output is correct |
36 |
Correct |
151 ms |
26488 KB |
Output is correct |
37 |
Correct |
263 ms |
30784 KB |
Output is correct |
38 |
Correct |
211 ms |
26744 KB |
Output is correct |
39 |
Correct |
160 ms |
34476 KB |
Output is correct |
40 |
Correct |
265 ms |
29852 KB |
Output is correct |
41 |
Correct |
12 ms |
9976 KB |
Output is correct |
42 |
Correct |
12 ms |
10104 KB |
Output is correct |
43 |
Correct |
12 ms |
9976 KB |
Output is correct |
44 |
Correct |
12 ms |
9976 KB |
Output is correct |
45 |
Correct |
12 ms |
9976 KB |
Output is correct |
46 |
Correct |
12 ms |
9976 KB |
Output is correct |
47 |
Correct |
12 ms |
9976 KB |
Output is correct |
48 |
Correct |
12 ms |
9976 KB |
Output is correct |
49 |
Correct |
12 ms |
9976 KB |
Output is correct |
50 |
Correct |
12 ms |
9976 KB |
Output is correct |
51 |
Correct |
217 ms |
27000 KB |
Output is correct |
52 |
Correct |
229 ms |
34792 KB |
Output is correct |
53 |
Correct |
364 ms |
30400 KB |
Output is correct |
54 |
Correct |
188 ms |
26640 KB |
Output is correct |
55 |
Correct |
191 ms |
26484 KB |
Output is correct |
56 |
Correct |
182 ms |
34684 KB |
Output is correct |
57 |
Correct |
244 ms |
30840 KB |
Output is correct |
58 |
Correct |
154 ms |
26488 KB |
Output is correct |
59 |
Correct |
214 ms |
26872 KB |
Output is correct |
60 |
Correct |
175 ms |
34708 KB |
Output is correct |
61 |
Correct |
272 ms |
31020 KB |
Output is correct |
62 |
Correct |
151 ms |
26416 KB |
Output is correct |
63 |
Correct |
192 ms |
26496 KB |
Output is correct |
64 |
Correct |
170 ms |
34808 KB |
Output is correct |
65 |
Correct |
306 ms |
30700 KB |
Output is correct |
66 |
Correct |
154 ms |
26744 KB |
Output is correct |
67 |
Correct |
201 ms |
26716 KB |
Output is correct |
68 |
Correct |
173 ms |
34780 KB |
Output is correct |
69 |
Correct |
277 ms |
29820 KB |
Output is correct |
70 |
Correct |
170 ms |
26856 KB |
Output is correct |