#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
vector<int> graf[100005];
int par[100005][20];
int dep[100005];
int disc[100005];
int out[100005];
int t=1;
void dfs(int u, int p){
disc[u] = t;
dep[u] = dep[p] + 1;
par[u][0] = p;
ff(i,1,19)par[u][i] = par[par[u][i-1]][i-1];
for(auto c:graf[u]){
if(c == p)continue;
t++;
dfs(c, u);
}
t++;
out[u] = t;
}
struct sta{
int a,b,c;
};
vector<sta> st[100005];
ll bit[200005];
void upd(int x, int y){
while(x < 200005){
bit[x] += y;
x += (x&(-x));
}
}
ll get(int x){
ll ans = 0;
while(x > 0){
ans += bit[x];
x -= (x&(-x));
}
return ans;
}
ll get(int l, int r){
return get(r) - get(l - 1);
}
bool anc(int u, int v){
return disc[u] <= disc[v] && out[u] >= out[v];
}
int lca(int u, int v){
if(anc(u, v))return u;
if(anc(v, u))return v;
fb(i,19,0)if(par[u][i] != 0 && !anc(par[u][i], v))u = par[u][i];
return par[u][0];
}
int dist(int u, int v){
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int lift(int u, int k){
ff(i,0,19)if(k & (1 << i))u = par[u][i];
return u;
}
ll dp[100005];
void solve(int u, int p){
dp[u] = get(disc[u], disc[u]);
for(auto c:graf[u]){
if(c == p)continue;
solve(c, u);
dp[u] += dp[c];
}
for(auto x:st[u]){
int a = x.a;
int b = x.b;
int c = x.c;
if(anc(b, a))swap(a, b);
if(a == u){
dp[u] = max(dp[u], get(disc[u], disc[b]) + c);
}
else{
dp[u] = max(dp[u], get(disc[u], disc[a]) + get(disc[lift(b, dist(b, u) - 1)], disc[b]) + c);
}
}
if(u != 1){
upd(disc[par[u][0]], dp[u]);
upd(disc[u], -dp[u]);
upd(out[u], dp[u]);
upd(out[par[u][0]], -dp[u]);
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
ff(i,1,n-1){
int x,y;
cin >> x >> y;
graf[x].pb(y);
graf[y].pb(x);
}
dfs(1, 0);
cin >> m;
ff(i,1,m){
int a,b,c;
cin >> a >> b >> c;
st[lca(a,b)].pb({a,b,c});
}
solve(1, 0);
cout << dp[1];
return 0;
}
Compilation message
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
election_campaign.cpp:37:5: note: in expansion of macro 'ff'
37 | ff(i,1,19)par[u][i] = par[par[u][i-1]][i-1];
| ^~
election_campaign.cpp: In function 'int lca(int, int)':
election_campaign.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
election_campaign.cpp:81:5: note: in expansion of macro 'fb'
81 | fb(i,19,0)if(par[u][i] != 0 && !anc(par[u][i], v))u = par[u][i];
| ^~
election_campaign.cpp: In function 'int lift(int, int)':
election_campaign.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
election_campaign.cpp:90:5: note: in expansion of macro 'ff'
90 | ff(i,0,19)if(k & (1 << i))u = par[u][i];
| ^~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
election_campaign.cpp:128:5: note: in expansion of macro 'ff'
128 | ff(i,1,n-1){
| ^~
election_campaign.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
election_campaign.cpp:136:5: note: in expansion of macro 'ff'
136 | ff(i,1,m){
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5068 KB |
Output is correct |
2 |
Correct |
2 ms |
5068 KB |
Output is correct |
3 |
Correct |
2 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
93 ms |
19552 KB |
Output is correct |
6 |
Correct |
49 ms |
31516 KB |
Output is correct |
7 |
Correct |
99 ms |
27700 KB |
Output is correct |
8 |
Correct |
71 ms |
21060 KB |
Output is correct |
9 |
Correct |
96 ms |
25684 KB |
Output is correct |
10 |
Correct |
72 ms |
21052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5068 KB |
Output is correct |
2 |
Correct |
2 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
80 ms |
32380 KB |
Output is correct |
5 |
Correct |
83 ms |
32452 KB |
Output is correct |
6 |
Correct |
80 ms |
32384 KB |
Output is correct |
7 |
Correct |
81 ms |
32376 KB |
Output is correct |
8 |
Correct |
86 ms |
32332 KB |
Output is correct |
9 |
Correct |
78 ms |
32464 KB |
Output is correct |
10 |
Correct |
82 ms |
32324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5068 KB |
Output is correct |
2 |
Correct |
2 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
80 ms |
32380 KB |
Output is correct |
5 |
Correct |
83 ms |
32452 KB |
Output is correct |
6 |
Correct |
80 ms |
32384 KB |
Output is correct |
7 |
Correct |
81 ms |
32376 KB |
Output is correct |
8 |
Correct |
86 ms |
32332 KB |
Output is correct |
9 |
Correct |
78 ms |
32464 KB |
Output is correct |
10 |
Correct |
82 ms |
32324 KB |
Output is correct |
11 |
Correct |
11 ms |
5708 KB |
Output is correct |
12 |
Correct |
85 ms |
32384 KB |
Output is correct |
13 |
Correct |
89 ms |
32428 KB |
Output is correct |
14 |
Correct |
88 ms |
32444 KB |
Output is correct |
15 |
Correct |
84 ms |
32452 KB |
Output is correct |
16 |
Correct |
101 ms |
32372 KB |
Output is correct |
17 |
Correct |
85 ms |
32324 KB |
Output is correct |
18 |
Correct |
96 ms |
32420 KB |
Output is correct |
19 |
Correct |
84 ms |
32492 KB |
Output is correct |
20 |
Correct |
84 ms |
32356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
21052 KB |
Output is correct |
2 |
Correct |
84 ms |
34896 KB |
Output is correct |
3 |
Correct |
170 ms |
30660 KB |
Output is correct |
4 |
Correct |
115 ms |
23928 KB |
Output is correct |
5 |
Correct |
148 ms |
29828 KB |
Output is correct |
6 |
Correct |
112 ms |
23888 KB |
Output is correct |
7 |
Correct |
166 ms |
29568 KB |
Output is correct |
8 |
Correct |
149 ms |
23828 KB |
Output is correct |
9 |
Correct |
101 ms |
34888 KB |
Output is correct |
10 |
Correct |
188 ms |
28172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5068 KB |
Output is correct |
2 |
Correct |
2 ms |
5068 KB |
Output is correct |
3 |
Correct |
2 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
93 ms |
19552 KB |
Output is correct |
6 |
Correct |
49 ms |
31516 KB |
Output is correct |
7 |
Correct |
99 ms |
27700 KB |
Output is correct |
8 |
Correct |
71 ms |
21060 KB |
Output is correct |
9 |
Correct |
96 ms |
25684 KB |
Output is correct |
10 |
Correct |
72 ms |
21052 KB |
Output is correct |
11 |
Correct |
3 ms |
5196 KB |
Output is correct |
12 |
Correct |
3 ms |
5328 KB |
Output is correct |
13 |
Correct |
3 ms |
5328 KB |
Output is correct |
14 |
Correct |
3 ms |
5196 KB |
Output is correct |
15 |
Correct |
3 ms |
5200 KB |
Output is correct |
16 |
Correct |
3 ms |
5296 KB |
Output is correct |
17 |
Correct |
4 ms |
5176 KB |
Output is correct |
18 |
Correct |
3 ms |
5324 KB |
Output is correct |
19 |
Correct |
4 ms |
5196 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5068 KB |
Output is correct |
2 |
Correct |
2 ms |
5068 KB |
Output is correct |
3 |
Correct |
2 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
93 ms |
19552 KB |
Output is correct |
6 |
Correct |
49 ms |
31516 KB |
Output is correct |
7 |
Correct |
99 ms |
27700 KB |
Output is correct |
8 |
Correct |
71 ms |
21060 KB |
Output is correct |
9 |
Correct |
96 ms |
25684 KB |
Output is correct |
10 |
Correct |
72 ms |
21052 KB |
Output is correct |
11 |
Correct |
2 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5324 KB |
Output is correct |
14 |
Correct |
80 ms |
32380 KB |
Output is correct |
15 |
Correct |
83 ms |
32452 KB |
Output is correct |
16 |
Correct |
80 ms |
32384 KB |
Output is correct |
17 |
Correct |
81 ms |
32376 KB |
Output is correct |
18 |
Correct |
86 ms |
32332 KB |
Output is correct |
19 |
Correct |
78 ms |
32464 KB |
Output is correct |
20 |
Correct |
82 ms |
32324 KB |
Output is correct |
21 |
Correct |
11 ms |
5708 KB |
Output is correct |
22 |
Correct |
85 ms |
32384 KB |
Output is correct |
23 |
Correct |
89 ms |
32428 KB |
Output is correct |
24 |
Correct |
88 ms |
32444 KB |
Output is correct |
25 |
Correct |
84 ms |
32452 KB |
Output is correct |
26 |
Correct |
101 ms |
32372 KB |
Output is correct |
27 |
Correct |
85 ms |
32324 KB |
Output is correct |
28 |
Correct |
96 ms |
32420 KB |
Output is correct |
29 |
Correct |
84 ms |
32492 KB |
Output is correct |
30 |
Correct |
84 ms |
32356 KB |
Output is correct |
31 |
Correct |
143 ms |
21052 KB |
Output is correct |
32 |
Correct |
84 ms |
34896 KB |
Output is correct |
33 |
Correct |
170 ms |
30660 KB |
Output is correct |
34 |
Correct |
115 ms |
23928 KB |
Output is correct |
35 |
Correct |
148 ms |
29828 KB |
Output is correct |
36 |
Correct |
112 ms |
23888 KB |
Output is correct |
37 |
Correct |
166 ms |
29568 KB |
Output is correct |
38 |
Correct |
149 ms |
23828 KB |
Output is correct |
39 |
Correct |
101 ms |
34888 KB |
Output is correct |
40 |
Correct |
188 ms |
28172 KB |
Output is correct |
41 |
Correct |
3 ms |
5196 KB |
Output is correct |
42 |
Correct |
3 ms |
5328 KB |
Output is correct |
43 |
Correct |
3 ms |
5328 KB |
Output is correct |
44 |
Correct |
3 ms |
5196 KB |
Output is correct |
45 |
Correct |
3 ms |
5200 KB |
Output is correct |
46 |
Correct |
3 ms |
5296 KB |
Output is correct |
47 |
Correct |
4 ms |
5176 KB |
Output is correct |
48 |
Correct |
3 ms |
5324 KB |
Output is correct |
49 |
Correct |
4 ms |
5196 KB |
Output is correct |
50 |
Correct |
3 ms |
5324 KB |
Output is correct |
51 |
Correct |
169 ms |
24056 KB |
Output is correct |
52 |
Correct |
84 ms |
35140 KB |
Output is correct |
53 |
Correct |
171 ms |
28732 KB |
Output is correct |
54 |
Correct |
108 ms |
24288 KB |
Output is correct |
55 |
Correct |
163 ms |
23860 KB |
Output is correct |
56 |
Correct |
91 ms |
35180 KB |
Output is correct |
57 |
Correct |
167 ms |
29528 KB |
Output is correct |
58 |
Correct |
111 ms |
24168 KB |
Output is correct |
59 |
Correct |
173 ms |
24048 KB |
Output is correct |
60 |
Correct |
88 ms |
35244 KB |
Output is correct |
61 |
Correct |
161 ms |
29684 KB |
Output is correct |
62 |
Correct |
127 ms |
23996 KB |
Output is correct |
63 |
Correct |
180 ms |
23708 KB |
Output is correct |
64 |
Correct |
86 ms |
35172 KB |
Output is correct |
65 |
Correct |
173 ms |
29572 KB |
Output is correct |
66 |
Correct |
109 ms |
24292 KB |
Output is correct |
67 |
Correct |
151 ms |
23732 KB |
Output is correct |
68 |
Correct |
91 ms |
35172 KB |
Output is correct |
69 |
Correct |
161 ms |
27920 KB |
Output is correct |
70 |
Correct |
125 ms |
24356 KB |
Output is correct |