#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define fore(a, b, c) for(int a=b; a<c; ++a)
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define ii pair<int,int>
#define dbg(x) cerr << #x << ": " << x << endl
#define raya cerr << "=============" << endl
const int N = 2e5+5;
const int LG = 20;
struct fenwick {
int n;
vi t;
fenwick(int n) : n(n) {
t.assign(n+5, 0);
}
void add(int i, int x){
for(; i<=n; i+=i&-i){
t[i] += x;
}
}
int query(int i){
int r = 0;
for(; i>0; i-=i&-i){
r += t[i];
}
return r;
}
} bit(N-5);
vi adj[N];
vector<array<int,3>> ways[N];
int dp[N][2];
int pa[N][LG];
int dep[N];
int timer;
int entro[N], salgo[N];
int n, m;
void dfs(int at, int ante=0){
entro[at] = ++timer;
pa[at][0] = ante;
for(int u : adj[at]){
if(u == ante) continue;
dep[u] = dep[at] + 1;
dfs(u, at);
}
salgo[at] = timer;
}
int jump(int a, int k){
fore(i, 0, LG){
if((k >> i) & 1){
a = pa[a][i];
}
}
return a;
}
int lca(int a, int b){
if(dep[a] > dep[b]) swap(a, b);
b = jump(b, dep[b] - dep[a]);
if(a == b) return a;
for(int i=LG-1; i>=0; --i){
if(pa[a][i] != pa[b][i]){
a = pa[a][i];
b = pa[b][i];
}
}
return pa[a][0];
}
int f(int at, int tomo){
int &res = dp[at][tomo];
if(res > -1) return res;
res = 0;
if(tomo == 0){
int cur = 0;
for(int u : adj[at]){
if(u == pa[at][0]) continue;
cur += max(f(u, 0), f(u, 1));
}
res = cur;
}
if(tomo == 1){
int sintomar = f(at, 0);
int mx = sintomar;
for(auto [a, b, c] : ways[at]){
int tmp = bit.query(entro[a]) + bit.query(entro[b]) + c;
mx = max(mx, sintomar + tmp);
}
res = mx;
bit.add(entro[at], f(at, 0) - f(at, 1));
bit.add(salgo[at]+1, f(at, 1) - f(at, 0));
}
return res;
}
signed main(){
memset(dp, -1, sizeof dp);
cin >> n;
fore(i, 1, n){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
for(int i=1; i<LG; ++i){
for(int j=1; j<=n; ++j){
pa[j][i] = pa[pa[j][i-1]][i-1];
}
}
cin >> m;
fore(i, 0, m){
int a, b, c;
cin >> a >> b >> c;
ways[lca(a, b)].pb({a, b, c});
}
int ans = max(f(1, 0), f(1, 1));
cout << ans << endl;
}
Compilation message
election_campaign.cpp: In function 'll f(ll, ll)':
election_campaign.cpp:102:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
102 | for(auto [a, b, c] : ways[at]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
Output is correct |
2 |
Correct |
3 ms |
19544 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
3 ms |
19548 KB |
Output is correct |
5 |
Correct |
57 ms |
38896 KB |
Output is correct |
6 |
Correct |
51 ms |
53588 KB |
Output is correct |
7 |
Correct |
64 ms |
48720 KB |
Output is correct |
8 |
Correct |
48 ms |
38992 KB |
Output is correct |
9 |
Correct |
60 ms |
45652 KB |
Output is correct |
10 |
Correct |
52 ms |
38996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19548 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19804 KB |
Output is correct |
4 |
Correct |
105 ms |
57172 KB |
Output is correct |
5 |
Correct |
105 ms |
57304 KB |
Output is correct |
6 |
Correct |
99 ms |
57168 KB |
Output is correct |
7 |
Correct |
108 ms |
57172 KB |
Output is correct |
8 |
Correct |
110 ms |
57168 KB |
Output is correct |
9 |
Correct |
99 ms |
57356 KB |
Output is correct |
10 |
Correct |
108 ms |
57172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19548 KB |
Output is correct |
2 |
Correct |
3 ms |
19548 KB |
Output is correct |
3 |
Correct |
3 ms |
19804 KB |
Output is correct |
4 |
Correct |
105 ms |
57172 KB |
Output is correct |
5 |
Correct |
105 ms |
57304 KB |
Output is correct |
6 |
Correct |
99 ms |
57168 KB |
Output is correct |
7 |
Correct |
108 ms |
57172 KB |
Output is correct |
8 |
Correct |
110 ms |
57168 KB |
Output is correct |
9 |
Correct |
99 ms |
57356 KB |
Output is correct |
10 |
Correct |
108 ms |
57172 KB |
Output is correct |
11 |
Correct |
12 ms |
20572 KB |
Output is correct |
12 |
Correct |
108 ms |
57168 KB |
Output is correct |
13 |
Correct |
112 ms |
57168 KB |
Output is correct |
14 |
Correct |
102 ms |
57168 KB |
Output is correct |
15 |
Correct |
117 ms |
57168 KB |
Output is correct |
16 |
Correct |
103 ms |
57172 KB |
Output is correct |
17 |
Correct |
112 ms |
57284 KB |
Output is correct |
18 |
Correct |
113 ms |
57172 KB |
Output is correct |
19 |
Correct |
107 ms |
57172 KB |
Output is correct |
20 |
Correct |
114 ms |
57196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
41716 KB |
Output is correct |
2 |
Correct |
98 ms |
57136 KB |
Output is correct |
3 |
Correct |
130 ms |
51600 KB |
Output is correct |
4 |
Correct |
97 ms |
42248 KB |
Output is correct |
5 |
Correct |
114 ms |
50576 KB |
Output is correct |
6 |
Correct |
97 ms |
42180 KB |
Output is correct |
7 |
Correct |
121 ms |
49928 KB |
Output is correct |
8 |
Correct |
108 ms |
41812 KB |
Output is correct |
9 |
Correct |
103 ms |
57168 KB |
Output is correct |
10 |
Correct |
135 ms |
48136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
Output is correct |
2 |
Correct |
3 ms |
19544 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
3 ms |
19548 KB |
Output is correct |
5 |
Correct |
57 ms |
38896 KB |
Output is correct |
6 |
Correct |
51 ms |
53588 KB |
Output is correct |
7 |
Correct |
64 ms |
48720 KB |
Output is correct |
8 |
Correct |
48 ms |
38992 KB |
Output is correct |
9 |
Correct |
60 ms |
45652 KB |
Output is correct |
10 |
Correct |
52 ms |
38996 KB |
Output is correct |
11 |
Correct |
4 ms |
19544 KB |
Output is correct |
12 |
Correct |
3 ms |
19804 KB |
Output is correct |
13 |
Correct |
3 ms |
19548 KB |
Output is correct |
14 |
Correct |
3 ms |
19548 KB |
Output is correct |
15 |
Correct |
4 ms |
19548 KB |
Output is correct |
16 |
Correct |
3 ms |
19572 KB |
Output is correct |
17 |
Correct |
3 ms |
19800 KB |
Output is correct |
18 |
Correct |
3 ms |
19548 KB |
Output is correct |
19 |
Correct |
4 ms |
19548 KB |
Output is correct |
20 |
Correct |
4 ms |
19672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19800 KB |
Output is correct |
2 |
Correct |
3 ms |
19544 KB |
Output is correct |
3 |
Correct |
3 ms |
19548 KB |
Output is correct |
4 |
Correct |
3 ms |
19548 KB |
Output is correct |
5 |
Correct |
57 ms |
38896 KB |
Output is correct |
6 |
Correct |
51 ms |
53588 KB |
Output is correct |
7 |
Correct |
64 ms |
48720 KB |
Output is correct |
8 |
Correct |
48 ms |
38992 KB |
Output is correct |
9 |
Correct |
60 ms |
45652 KB |
Output is correct |
10 |
Correct |
52 ms |
38996 KB |
Output is correct |
11 |
Correct |
3 ms |
19548 KB |
Output is correct |
12 |
Correct |
3 ms |
19548 KB |
Output is correct |
13 |
Correct |
3 ms |
19804 KB |
Output is correct |
14 |
Correct |
105 ms |
57172 KB |
Output is correct |
15 |
Correct |
105 ms |
57304 KB |
Output is correct |
16 |
Correct |
99 ms |
57168 KB |
Output is correct |
17 |
Correct |
108 ms |
57172 KB |
Output is correct |
18 |
Correct |
110 ms |
57168 KB |
Output is correct |
19 |
Correct |
99 ms |
57356 KB |
Output is correct |
20 |
Correct |
108 ms |
57172 KB |
Output is correct |
21 |
Correct |
12 ms |
20572 KB |
Output is correct |
22 |
Correct |
108 ms |
57168 KB |
Output is correct |
23 |
Correct |
112 ms |
57168 KB |
Output is correct |
24 |
Correct |
102 ms |
57168 KB |
Output is correct |
25 |
Correct |
117 ms |
57168 KB |
Output is correct |
26 |
Correct |
103 ms |
57172 KB |
Output is correct |
27 |
Correct |
112 ms |
57284 KB |
Output is correct |
28 |
Correct |
113 ms |
57172 KB |
Output is correct |
29 |
Correct |
107 ms |
57172 KB |
Output is correct |
30 |
Correct |
114 ms |
57196 KB |
Output is correct |
31 |
Correct |
111 ms |
41716 KB |
Output is correct |
32 |
Correct |
98 ms |
57136 KB |
Output is correct |
33 |
Correct |
130 ms |
51600 KB |
Output is correct |
34 |
Correct |
97 ms |
42248 KB |
Output is correct |
35 |
Correct |
114 ms |
50576 KB |
Output is correct |
36 |
Correct |
97 ms |
42180 KB |
Output is correct |
37 |
Correct |
121 ms |
49928 KB |
Output is correct |
38 |
Correct |
108 ms |
41812 KB |
Output is correct |
39 |
Correct |
103 ms |
57168 KB |
Output is correct |
40 |
Correct |
135 ms |
48136 KB |
Output is correct |
41 |
Correct |
4 ms |
19544 KB |
Output is correct |
42 |
Correct |
3 ms |
19804 KB |
Output is correct |
43 |
Correct |
3 ms |
19548 KB |
Output is correct |
44 |
Correct |
3 ms |
19548 KB |
Output is correct |
45 |
Correct |
4 ms |
19548 KB |
Output is correct |
46 |
Correct |
3 ms |
19572 KB |
Output is correct |
47 |
Correct |
3 ms |
19800 KB |
Output is correct |
48 |
Correct |
3 ms |
19548 KB |
Output is correct |
49 |
Correct |
4 ms |
19548 KB |
Output is correct |
50 |
Correct |
4 ms |
19672 KB |
Output is correct |
51 |
Correct |
113 ms |
41868 KB |
Output is correct |
52 |
Correct |
133 ms |
57172 KB |
Output is correct |
53 |
Correct |
141 ms |
48388 KB |
Output is correct |
54 |
Correct |
105 ms |
41800 KB |
Output is correct |
55 |
Correct |
111 ms |
41732 KB |
Output is correct |
56 |
Correct |
111 ms |
57172 KB |
Output is correct |
57 |
Correct |
126 ms |
49468 KB |
Output is correct |
58 |
Correct |
103 ms |
41972 KB |
Output is correct |
59 |
Correct |
109 ms |
41676 KB |
Output is correct |
60 |
Correct |
112 ms |
57168 KB |
Output is correct |
61 |
Correct |
114 ms |
49520 KB |
Output is correct |
62 |
Correct |
103 ms |
42284 KB |
Output is correct |
63 |
Correct |
109 ms |
41836 KB |
Output is correct |
64 |
Correct |
109 ms |
57168 KB |
Output is correct |
65 |
Correct |
130 ms |
49492 KB |
Output is correct |
66 |
Correct |
94 ms |
41804 KB |
Output is correct |
67 |
Correct |
117 ms |
42436 KB |
Output is correct |
68 |
Correct |
113 ms |
57172 KB |
Output is correct |
69 |
Correct |
120 ms |
47248 KB |
Output is correct |
70 |
Correct |
112 ms |
42180 KB |
Output is correct |