#include<bits/stdc++.h>
using namespace std;
#ifdef ONPC
#include"debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define int long long
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 15;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int off = (1<<18);
struct sgt {
#define ls (idx<<1)
#define rs (idx<<1|1)
ll t[off<<1], lazy[MAXN];
int unit = 0;
void pull(int idx){
t[idx] = t[ls]+t[rs];
}
void push(int idx){
if (!lazy[idx]) return;
t[idx] += lazy[idx];
if (idx < off){
lazy[ls] += lazy[idx];
lazy[rs] += lazy[idx];
}
lazy[idx] = 0;
}
void update(int idx, int u){
t[idx+=off] = u;
while (idx/=2)
pull(idx);
}
ll get(int idx){
ll res = t[idx += off];
while (idx/=2)
res += t[idx];
return res;
}
int get(int l, int r, int idx=1, int lo=0, int hi=off){
if (r <= lo || hi <= l)
return unit;
if (l <= lo && hi <= r)
return t[idx];
int mi = (lo+hi)>>1;
return get(l, r, ls, lo, mi)+get(l, r, rs, mi, hi);
}
void update(int l, int r, int v, int idx=1, int lo=0, int hi=off){
if (r <= lo || hi <= l)
return;
if (l <= lo && hi <= r){
t[idx] += v;
return;
}
int mi = (lo+hi)>>1;
update(l, r, v, ls, lo, mi);
update(l, r, v, rs, mi, hi);
return;
}
} seg1, seg2;
vector<int> adj[MAXN];
int fa[MAXN][20], dep[MAXN], dt[MAXN], ft[MAXN], timer = 1;
void dfs(int node, int par){
dep[node] = dep[par] + 1;
fa[node][0] = par;
dt[node] = timer++;
for (auto to : adj[node]){
if (to != par) dfs(to, node);
}
ft[node] = timer++;
}
int lca(int u, int v){
if (dep[u] > dep[v]) swap(u, v);
for (int j = 19; j >= 0; j--){
if (dep[fa[v][j]] >= dep[u]) v = fa[v][j];
}
if (u == v) return u;
for (int j = 19; j >= 0; j--){
if (fa[v][j] != fa[u][j]) v = fa[v][j], u = fa[u][j];
}
return fa[u][0];
}
vector<array<int, 3>> queries[MAXN];
ll dp1[MAXN], dp2[MAXN];
// dp1 for best answer
// dp2 for sum of childern
bool contain(int u, int v){
return dt[u] <= dt[v] && ft[v] <= ft[u];
}
void solve(int node, int par){
for (int to : adj[node]){
if (to != par) {
solve(to, node);
dp1[node] += dp1[to];
}
}
dp2[node] = dp1[node];
seg2.update(dt[node], ft[node], dp2[node]);
for (auto [u, v, c] : queries[node]){
debug(node, u, v, c);
debug(seg2.get(dt[u]), seg2.get(dt[v]), seg2.get(dt[node]), seg2.get(dt[fa[node][0]]));
ll tm = c + seg2.get(dt[u]) + seg2.get(dt[v]) - seg2.get(dt[node]) - seg2.get(dt[fa[node][0]]);
tm -= seg1.get(dt[u]) + seg1.get(dt[v]);
ckmax(dp1[node], tm);
}
seg1.update(dt[node], ft[node], dp1[node]);
debug(node, dp1[node], dp2[node]);
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 0);
for (int j = 1; j < 20; j++){
for (int i = 1; i <= n; i++){
fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
}
for (int i = 1; i <= n; i++){
debug(i, dt[i], ft[i]);
}
int m;
cin >> m;
for (int i = 0; i < m; i++){
int u, v, c;
cin >> u >> v >> c;
queries[lca(u, v)].pb({u, v, c});
}
solve(1, 0);
cout << dp1[1] << endl;
}
Compilation message
election_campaign.cpp: In function 'void solve(long long int, long long int)':
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
election_campaign.cpp:112:3: note: in expansion of macro 'debug'
112 | debug(node, u, v, c);
| ^~~~~
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
election_campaign.cpp:113:3: note: in expansion of macro 'debug'
113 | debug(seg2.get(dt[u]), seg2.get(dt[v]), seg2.get(dt[node]), seg2.get(dt[fa[node][0]]));
| ^~~~~
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
election_campaign.cpp:119:2: note: in expansion of macro 'debug'
119 | debug(node, dp1[node], dp2[node]);
| ^~~~~
election_campaign.cpp: In function 'int32_t main()':
election_campaign.cpp:6:20: warning: statement has no effect [-Wunused-value]
6 | #define debug(...) 42
| ^~
election_campaign.cpp:139:3: note: in expansion of macro 'debug'
139 | debug(i, dt[i], ft[i]);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
61784 KB |
Output is correct |
2 |
Correct |
12 ms |
62032 KB |
Output is correct |
3 |
Correct |
13 ms |
61788 KB |
Output is correct |
4 |
Correct |
12 ms |
62044 KB |
Output is correct |
5 |
Correct |
93 ms |
93268 KB |
Output is correct |
6 |
Correct |
75 ms |
106880 KB |
Output is correct |
7 |
Correct |
95 ms |
103060 KB |
Output is correct |
8 |
Correct |
66 ms |
92752 KB |
Output is correct |
9 |
Correct |
97 ms |
100108 KB |
Output is correct |
10 |
Correct |
66 ms |
92692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
61788 KB |
Output is correct |
2 |
Correct |
12 ms |
61788 KB |
Output is correct |
3 |
Correct |
13 ms |
62044 KB |
Output is correct |
4 |
Correct |
133 ms |
109040 KB |
Output is correct |
5 |
Correct |
133 ms |
109088 KB |
Output is correct |
6 |
Correct |
126 ms |
108884 KB |
Output is correct |
7 |
Correct |
132 ms |
109016 KB |
Output is correct |
8 |
Correct |
134 ms |
108948 KB |
Output is correct |
9 |
Correct |
127 ms |
108884 KB |
Output is correct |
10 |
Correct |
143 ms |
109264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
61788 KB |
Output is correct |
2 |
Correct |
12 ms |
61788 KB |
Output is correct |
3 |
Correct |
13 ms |
62044 KB |
Output is correct |
4 |
Correct |
133 ms |
109040 KB |
Output is correct |
5 |
Correct |
133 ms |
109088 KB |
Output is correct |
6 |
Correct |
126 ms |
108884 KB |
Output is correct |
7 |
Correct |
132 ms |
109016 KB |
Output is correct |
8 |
Correct |
134 ms |
108948 KB |
Output is correct |
9 |
Correct |
127 ms |
108884 KB |
Output is correct |
10 |
Correct |
143 ms |
109264 KB |
Output is correct |
11 |
Correct |
22 ms |
63068 KB |
Output is correct |
12 |
Correct |
137 ms |
108884 KB |
Output is correct |
13 |
Correct |
144 ms |
108884 KB |
Output is correct |
14 |
Correct |
138 ms |
108856 KB |
Output is correct |
15 |
Correct |
139 ms |
109088 KB |
Output is correct |
16 |
Correct |
129 ms |
109092 KB |
Output is correct |
17 |
Correct |
135 ms |
108880 KB |
Output is correct |
18 |
Correct |
150 ms |
108916 KB |
Output is correct |
19 |
Correct |
129 ms |
109092 KB |
Output is correct |
20 |
Correct |
139 ms |
108876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
96516 KB |
Output is correct |
2 |
Correct |
132 ms |
111296 KB |
Output is correct |
3 |
Correct |
197 ms |
107028 KB |
Output is correct |
4 |
Correct |
138 ms |
97284 KB |
Output is correct |
5 |
Correct |
175 ms |
104080 KB |
Output is correct |
6 |
Correct |
123 ms |
99008 KB |
Output is correct |
7 |
Correct |
194 ms |
105736 KB |
Output is correct |
8 |
Correct |
156 ms |
99600 KB |
Output is correct |
9 |
Correct |
137 ms |
111440 KB |
Output is correct |
10 |
Correct |
185 ms |
104316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
61784 KB |
Output is correct |
2 |
Correct |
12 ms |
62032 KB |
Output is correct |
3 |
Correct |
13 ms |
61788 KB |
Output is correct |
4 |
Correct |
12 ms |
62044 KB |
Output is correct |
5 |
Correct |
93 ms |
93268 KB |
Output is correct |
6 |
Correct |
75 ms |
106880 KB |
Output is correct |
7 |
Correct |
95 ms |
103060 KB |
Output is correct |
8 |
Correct |
66 ms |
92752 KB |
Output is correct |
9 |
Correct |
97 ms |
100108 KB |
Output is correct |
10 |
Correct |
66 ms |
92692 KB |
Output is correct |
11 |
Correct |
13 ms |
62040 KB |
Output is correct |
12 |
Correct |
13 ms |
62044 KB |
Output is correct |
13 |
Correct |
13 ms |
62044 KB |
Output is correct |
14 |
Correct |
13 ms |
62188 KB |
Output is correct |
15 |
Correct |
13 ms |
62040 KB |
Output is correct |
16 |
Correct |
14 ms |
62044 KB |
Output is correct |
17 |
Correct |
13 ms |
61956 KB |
Output is correct |
18 |
Correct |
14 ms |
62044 KB |
Output is correct |
19 |
Correct |
13 ms |
62044 KB |
Output is correct |
20 |
Correct |
14 ms |
62068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
61784 KB |
Output is correct |
2 |
Correct |
12 ms |
62032 KB |
Output is correct |
3 |
Correct |
13 ms |
61788 KB |
Output is correct |
4 |
Correct |
12 ms |
62044 KB |
Output is correct |
5 |
Correct |
93 ms |
93268 KB |
Output is correct |
6 |
Correct |
75 ms |
106880 KB |
Output is correct |
7 |
Correct |
95 ms |
103060 KB |
Output is correct |
8 |
Correct |
66 ms |
92752 KB |
Output is correct |
9 |
Correct |
97 ms |
100108 KB |
Output is correct |
10 |
Correct |
66 ms |
92692 KB |
Output is correct |
11 |
Correct |
12 ms |
61788 KB |
Output is correct |
12 |
Correct |
12 ms |
61788 KB |
Output is correct |
13 |
Correct |
13 ms |
62044 KB |
Output is correct |
14 |
Correct |
133 ms |
109040 KB |
Output is correct |
15 |
Correct |
133 ms |
109088 KB |
Output is correct |
16 |
Correct |
126 ms |
108884 KB |
Output is correct |
17 |
Correct |
132 ms |
109016 KB |
Output is correct |
18 |
Correct |
134 ms |
108948 KB |
Output is correct |
19 |
Correct |
127 ms |
108884 KB |
Output is correct |
20 |
Correct |
143 ms |
109264 KB |
Output is correct |
21 |
Correct |
22 ms |
63068 KB |
Output is correct |
22 |
Correct |
137 ms |
108884 KB |
Output is correct |
23 |
Correct |
144 ms |
108884 KB |
Output is correct |
24 |
Correct |
138 ms |
108856 KB |
Output is correct |
25 |
Correct |
139 ms |
109088 KB |
Output is correct |
26 |
Correct |
129 ms |
109092 KB |
Output is correct |
27 |
Correct |
135 ms |
108880 KB |
Output is correct |
28 |
Correct |
150 ms |
108916 KB |
Output is correct |
29 |
Correct |
129 ms |
109092 KB |
Output is correct |
30 |
Correct |
139 ms |
108876 KB |
Output is correct |
31 |
Correct |
155 ms |
96516 KB |
Output is correct |
32 |
Correct |
132 ms |
111296 KB |
Output is correct |
33 |
Correct |
197 ms |
107028 KB |
Output is correct |
34 |
Correct |
138 ms |
97284 KB |
Output is correct |
35 |
Correct |
175 ms |
104080 KB |
Output is correct |
36 |
Correct |
123 ms |
99008 KB |
Output is correct |
37 |
Correct |
194 ms |
105736 KB |
Output is correct |
38 |
Correct |
156 ms |
99600 KB |
Output is correct |
39 |
Correct |
137 ms |
111440 KB |
Output is correct |
40 |
Correct |
185 ms |
104316 KB |
Output is correct |
41 |
Correct |
13 ms |
62040 KB |
Output is correct |
42 |
Correct |
13 ms |
62044 KB |
Output is correct |
43 |
Correct |
13 ms |
62044 KB |
Output is correct |
44 |
Correct |
13 ms |
62188 KB |
Output is correct |
45 |
Correct |
13 ms |
62040 KB |
Output is correct |
46 |
Correct |
14 ms |
62044 KB |
Output is correct |
47 |
Correct |
13 ms |
61956 KB |
Output is correct |
48 |
Correct |
14 ms |
62044 KB |
Output is correct |
49 |
Correct |
13 ms |
62044 KB |
Output is correct |
50 |
Correct |
14 ms |
62068 KB |
Output is correct |
51 |
Correct |
166 ms |
99516 KB |
Output is correct |
52 |
Correct |
137 ms |
111792 KB |
Output is correct |
53 |
Correct |
206 ms |
104812 KB |
Output is correct |
54 |
Correct |
117 ms |
99396 KB |
Output is correct |
55 |
Correct |
154 ms |
99648 KB |
Output is correct |
56 |
Correct |
148 ms |
111876 KB |
Output is correct |
57 |
Correct |
182 ms |
105368 KB |
Output is correct |
58 |
Correct |
118 ms |
97380 KB |
Output is correct |
59 |
Correct |
157 ms |
99876 KB |
Output is correct |
60 |
Correct |
136 ms |
111700 KB |
Output is correct |
61 |
Correct |
178 ms |
105248 KB |
Output is correct |
62 |
Correct |
124 ms |
98932 KB |
Output is correct |
63 |
Correct |
153 ms |
97640 KB |
Output is correct |
64 |
Correct |
135 ms |
111736 KB |
Output is correct |
65 |
Correct |
212 ms |
105476 KB |
Output is correct |
66 |
Correct |
119 ms |
99332 KB |
Output is correct |
67 |
Correct |
149 ms |
97696 KB |
Output is correct |
68 |
Correct |
135 ms |
111736 KB |
Output is correct |
69 |
Correct |
179 ms |
101772 KB |
Output is correct |
70 |
Correct |
133 ms |
99544 KB |
Output is correct |