#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 1e5 + 1;
int in[nmax];
vector <vector <int> > g(nmax);
vector <int> sz(nmax);
vector <ll> t(4 * nmax);
void update(int v, int l, int r, int pos, ll val){
if(l > pos || r < pos) return;
if(l == r){
t[v] = val;
return;
}
int m = (l + r) / 2;
update(2 * v, l, m, pos, val);
update(2 * v + 1, m + 1, r, pos, val);
t[v] = t[2 * v] + t[2 * v + 1];
}
int get(int v, int l, int r, int st, int fin){
if(l > fin || r < st) return 0;
if(l >= st && r <= fin) return t[v];
int m = (l + r) / 2;
return get(2 * v, l, m, st, fin) + get(2 * v + 1, m + 1, r, st, fin);
}
int lead[nmax];
int a[nmax][22];
void dfs(int v, int e = -1){
sz[v] = 1;
a[v][0] = e;
for(int j = 1; j <= 20; j++){
if(a[v][j - 1] != -1) a[v][j] = a[a[v][j - 1]][j - 1];
else a[v][j] = -1;
}
for(int to : g[v]){
if(to == e) continue;
dfs(to, v);
sz[v] += sz[to];
}
for(int j = 1; j < g[v].size(); j++){
int x = g[v][j];
if(x == e) continue;
if(sz[x] * 2 >= sz[v]) {
swap(g[v][j], g[v][0]);
}
}
}
int timer = 1;
int out[nmax];
int outtimer = 1;
void DFS(int v, int l, int e = -1){
in[v] = timer++;
lead[v] = l;
for(int to : g[v]){
if(to == e) continue;
if(sz[to] * 2 >= sz[v]) DFS(to, l, v);
else DFS(to, to, v);
}
out[v] = outtimer++;
}
bool is_ancestor(int a, int b){
return in[a] < in[b] && out[a] > out[b];
}
int lca(int u, int v){
if(is_ancestor(u, v)) return u;
if(is_ancestor(v, u)) return v;
for(int j =20; j >= 0; j--){
if(a[v][j] == -1) continue;
if(is_ancestor(a[v][j], u)) continue;
v = a[v][j];
}
return a[v][0];
}
int n;
ll sum(int A, int B){
ll ans = 0;
while(lead[A] != lead[B]){
int x = lead[A];
ans += get(1, 1, n, in[x], in[A]);
A = a[x][0];
}
ans += get(1, 1, n, in[B], in[A]);
return ans;
}
ll dp[nmax];
vector <vector <pair<ll, pii> > > vec(nmax);
void DP_DFS(int v, int e = -1){
for(int to : g[v]){
if(to == e) continue;
DP_DFS(to, v);
}
ll s = 0;
for(int to : g[v])
if(to != e) s += dp[to];
dp[v] = s;
for(auto ro : vec[v]){
int x = ro.s.f, y = ro.s.s;
ll val = ro.f;
dp[v] = max(dp[v], sum(x, v) + sum(y, v) + s + val);
}
update(1, 1, n, in[v], s - dp[v]);
}
int main(){
//ios_base::sync_with_stdio(false); cin.tie(0);
cin >>n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1);
DFS(1, 1);
int m; cin >> m;
//cout << lca(2, 6) << "\n";
for(int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
ll c; cin >> c;
ll x = lca(u, v);
// cout << x << ' ';
vec[x].pb({c, {u, v}});
}
DP_DFS(1);
cout << dp[1];
return 0;
}
Compilation message
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int j = 1; j < g[v].size(); j++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8484 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
146 ms |
22588 KB |
Output is correct |
6 |
Correct |
94 ms |
30120 KB |
Output is correct |
7 |
Correct |
148 ms |
27528 KB |
Output is correct |
8 |
Correct |
115 ms |
22832 KB |
Output is correct |
9 |
Correct |
133 ms |
25960 KB |
Output is correct |
10 |
Correct |
126 ms |
22928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8788 KB |
Output is correct |
4 |
Correct |
190 ms |
32740 KB |
Output is correct |
5 |
Correct |
205 ms |
32800 KB |
Output is correct |
6 |
Correct |
198 ms |
32784 KB |
Output is correct |
7 |
Correct |
193 ms |
32720 KB |
Output is correct |
8 |
Correct |
213 ms |
32900 KB |
Output is correct |
9 |
Correct |
187 ms |
32844 KB |
Output is correct |
10 |
Correct |
193 ms |
32704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
5 ms |
8788 KB |
Output is correct |
4 |
Correct |
190 ms |
32740 KB |
Output is correct |
5 |
Correct |
205 ms |
32800 KB |
Output is correct |
6 |
Correct |
198 ms |
32784 KB |
Output is correct |
7 |
Correct |
193 ms |
32720 KB |
Output is correct |
8 |
Correct |
213 ms |
32900 KB |
Output is correct |
9 |
Correct |
187 ms |
32844 KB |
Output is correct |
10 |
Correct |
193 ms |
32704 KB |
Output is correct |
11 |
Correct |
25 ms |
9532 KB |
Output is correct |
12 |
Correct |
223 ms |
32708 KB |
Output is correct |
13 |
Correct |
206 ms |
32716 KB |
Output is correct |
14 |
Correct |
194 ms |
32840 KB |
Output is correct |
15 |
Correct |
271 ms |
32816 KB |
Output is correct |
16 |
Correct |
192 ms |
32844 KB |
Output is correct |
17 |
Correct |
205 ms |
32764 KB |
Output is correct |
18 |
Correct |
242 ms |
32784 KB |
Output is correct |
19 |
Correct |
197 ms |
32772 KB |
Output is correct |
20 |
Correct |
205 ms |
32716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
390 ms |
24488 KB |
Output is correct |
2 |
Correct |
187 ms |
35084 KB |
Output is correct |
3 |
Correct |
334 ms |
31976 KB |
Output is correct |
4 |
Correct |
256 ms |
27200 KB |
Output is correct |
5 |
Correct |
319 ms |
31496 KB |
Output is correct |
6 |
Correct |
255 ms |
27140 KB |
Output is correct |
7 |
Correct |
285 ms |
31044 KB |
Output is correct |
8 |
Correct |
330 ms |
27120 KB |
Output is correct |
9 |
Correct |
204 ms |
35024 KB |
Output is correct |
10 |
Correct |
298 ms |
30016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8484 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
146 ms |
22588 KB |
Output is correct |
6 |
Correct |
94 ms |
30120 KB |
Output is correct |
7 |
Correct |
148 ms |
27528 KB |
Output is correct |
8 |
Correct |
115 ms |
22832 KB |
Output is correct |
9 |
Correct |
133 ms |
25960 KB |
Output is correct |
10 |
Correct |
126 ms |
22928 KB |
Output is correct |
11 |
Correct |
6 ms |
8660 KB |
Output is correct |
12 |
Correct |
6 ms |
8788 KB |
Output is correct |
13 |
Correct |
6 ms |
8688 KB |
Output is correct |
14 |
Correct |
6 ms |
8660 KB |
Output is correct |
15 |
Correct |
6 ms |
8660 KB |
Output is correct |
16 |
Correct |
6 ms |
8692 KB |
Output is correct |
17 |
Correct |
6 ms |
8716 KB |
Output is correct |
18 |
Correct |
6 ms |
8768 KB |
Output is correct |
19 |
Correct |
6 ms |
8660 KB |
Output is correct |
20 |
Correct |
5 ms |
8788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8484 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
5 ms |
8660 KB |
Output is correct |
5 |
Correct |
146 ms |
22588 KB |
Output is correct |
6 |
Correct |
94 ms |
30120 KB |
Output is correct |
7 |
Correct |
148 ms |
27528 KB |
Output is correct |
8 |
Correct |
115 ms |
22832 KB |
Output is correct |
9 |
Correct |
133 ms |
25960 KB |
Output is correct |
10 |
Correct |
126 ms |
22928 KB |
Output is correct |
11 |
Correct |
5 ms |
8532 KB |
Output is correct |
12 |
Correct |
5 ms |
8532 KB |
Output is correct |
13 |
Correct |
5 ms |
8788 KB |
Output is correct |
14 |
Correct |
190 ms |
32740 KB |
Output is correct |
15 |
Correct |
205 ms |
32800 KB |
Output is correct |
16 |
Correct |
198 ms |
32784 KB |
Output is correct |
17 |
Correct |
193 ms |
32720 KB |
Output is correct |
18 |
Correct |
213 ms |
32900 KB |
Output is correct |
19 |
Correct |
187 ms |
32844 KB |
Output is correct |
20 |
Correct |
193 ms |
32704 KB |
Output is correct |
21 |
Correct |
25 ms |
9532 KB |
Output is correct |
22 |
Correct |
223 ms |
32708 KB |
Output is correct |
23 |
Correct |
206 ms |
32716 KB |
Output is correct |
24 |
Correct |
194 ms |
32840 KB |
Output is correct |
25 |
Correct |
271 ms |
32816 KB |
Output is correct |
26 |
Correct |
192 ms |
32844 KB |
Output is correct |
27 |
Correct |
205 ms |
32764 KB |
Output is correct |
28 |
Correct |
242 ms |
32784 KB |
Output is correct |
29 |
Correct |
197 ms |
32772 KB |
Output is correct |
30 |
Correct |
205 ms |
32716 KB |
Output is correct |
31 |
Correct |
390 ms |
24488 KB |
Output is correct |
32 |
Correct |
187 ms |
35084 KB |
Output is correct |
33 |
Correct |
334 ms |
31976 KB |
Output is correct |
34 |
Correct |
256 ms |
27200 KB |
Output is correct |
35 |
Correct |
319 ms |
31496 KB |
Output is correct |
36 |
Correct |
255 ms |
27140 KB |
Output is correct |
37 |
Correct |
285 ms |
31044 KB |
Output is correct |
38 |
Correct |
330 ms |
27120 KB |
Output is correct |
39 |
Correct |
204 ms |
35024 KB |
Output is correct |
40 |
Correct |
298 ms |
30016 KB |
Output is correct |
41 |
Correct |
6 ms |
8660 KB |
Output is correct |
42 |
Correct |
6 ms |
8788 KB |
Output is correct |
43 |
Correct |
6 ms |
8688 KB |
Output is correct |
44 |
Correct |
6 ms |
8660 KB |
Output is correct |
45 |
Correct |
6 ms |
8660 KB |
Output is correct |
46 |
Correct |
6 ms |
8692 KB |
Output is correct |
47 |
Correct |
6 ms |
8716 KB |
Output is correct |
48 |
Correct |
6 ms |
8768 KB |
Output is correct |
49 |
Correct |
6 ms |
8660 KB |
Output is correct |
50 |
Correct |
5 ms |
8788 KB |
Output is correct |
51 |
Correct |
336 ms |
27388 KB |
Output is correct |
52 |
Correct |
216 ms |
35276 KB |
Output is correct |
53 |
Correct |
307 ms |
30644 KB |
Output is correct |
54 |
Correct |
259 ms |
27356 KB |
Output is correct |
55 |
Correct |
384 ms |
27104 KB |
Output is correct |
56 |
Correct |
209 ms |
35288 KB |
Output is correct |
57 |
Correct |
270 ms |
31232 KB |
Output is correct |
58 |
Correct |
293 ms |
27420 KB |
Output is correct |
59 |
Correct |
339 ms |
27336 KB |
Output is correct |
60 |
Correct |
204 ms |
35252 KB |
Output is correct |
61 |
Correct |
291 ms |
31408 KB |
Output is correct |
62 |
Correct |
271 ms |
27128 KB |
Output is correct |
63 |
Correct |
451 ms |
26968 KB |
Output is correct |
64 |
Correct |
204 ms |
35256 KB |
Output is correct |
65 |
Correct |
317 ms |
31180 KB |
Output is correct |
66 |
Correct |
235 ms |
27220 KB |
Output is correct |
67 |
Correct |
429 ms |
26996 KB |
Output is correct |
68 |
Correct |
204 ms |
35280 KB |
Output is correct |
69 |
Correct |
352 ms |
30112 KB |
Output is correct |
70 |
Correct |
263 ms |
27156 KB |
Output is correct |