/* made by amunduzbaev */
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
#include "bits/stdc++.h"
using namespace std;
//~ using namespace __gnu_pbds;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
//~ template<class T> using oset = tree<T,
//~ null_type, less_equal<T>, rb_tree_tag,
//~ tree_order_statistics_node_update>;
const int N = 2e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
#define MULTI 0
int n, m, k, s, t, q, ans, res[N], a[N];
int tin[N], tout[N], par[N][20], sz[N], d[N], hh[N];
int in = 0, pos[N], fir[N];
vv<tut<3>> qq[N];
vii edges[N];
void dfs(int u, int p = -1){
tin[u] = t++, sz[u] = 1;
for(int i=1;i<20;i++) par[u][i] = par[par[u][i-1]][i-1];
for(auto x : edges[u]){
if(x == p) continue;
par[x][0] = u, d[x] = d[u] + 1, dfs(x, u);
sz[u] += sz[x];
if(sz[x] > sz[hh[u]]) hh[u] = x;
} tout[u] = t-1;
}
void dec(int u, int h){
fir[u] = h, pos[u] = in++;
if(hh[u]) dec(hh[u], h);
for(auto x : edges[u]){
if(x == par[u][0]) continue;
if(x != hh[u]) dec(x, x);
}
}
bool anc(int a, int b){ return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int LCA(int a, int b){
if(anc(a, b)) return a;
if(anc(b, a)) return b;
for(int i=19;i>=0;i--){
if(!anc(par[a][i], b)) a = par[a][i];
} return par[a][0];
}
struct tree{
int tree[4*N];
void sett(int i, int v, int lx = 0, int rx = n, int x = 1){
if(lx == rx) { tree[x] = v; return; }
int m = (lx + rx)>>1;
if(i <= m) sett(i, v, lx, m, x<<1);
else sett(i, v, m+1, rx, x<<1|1);
tree[x] = tree[x<<1] + tree[x<<1|1];
}
int find(int l, int r, int lx = 0, int rx = n, int x = 1){
if(lx > r || rx < l) return 0;
if(lx >= l && rx <= r) return tree[x];
int m = (lx + rx)>>1;
return find(l, r, lx, m, x<<1) + find(l, r, m+1, rx, x<<1|1);
}
}tt1, tt2;
int find(tree& tt, int a, int b){
int res = 0;
for(; fir[a] != fir[b]; b = par[fir[b]][0]){
if(d[fir[a]] > d[fir[b]]) swap(a, b);
res += tt.find(pos[fir[b]], pos[b]);
} res += tt.find(min(pos[a], pos[b]), max(pos[a], pos[b]));
return res;
}
void dfs2(int u, int p = -1){
for(auto x : edges[u]) if(x != p) dfs2(x, u), res[u] += res[x];
tt1.sett(pos[u], res[u]);
for(auto x : qq[u]){
int sum = find(tt1, x[0], x[1]) - find(tt2, x[0], x[1]);
//~ cout<<x[0]<<" "<<x[1]<<" "<<find(tt1, x[0], x[1])<<" "<<find(tt2, x[0], x[1])<<"\n";
umax(res[u], sum + x[2]);
} tt2.sett(pos[u], res[u]);
//~ cout<<u<<" "<<res[u]<<"\n";
}
void solve(int t_case){
cin>>n;
tin[0] = 0, tout[0] = mod;
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].pb(b), edges[b].pb(a);
}
dfs(1);
cin>>m;
for(int i=0;i<m;i++){
int a, b, c; cin>>a>>b>>c;
int lca = LCA(a, b);
qq[lca].pb({a, b, c});
}
//~ cout<<"\n";
dec(1, 1);
dfs2(1);
cout<<res[1]<<"\n";
}
signed main(){
NeedForSpeed
if(MULTI){
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} else solve(1);
return 0;
}
Compilation message
election_campaign.cpp: In function 'void usaco(std::string)':
election_campaign.cpp:32:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:33:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9756 KB |
Output is correct |
4 |
Correct |
7 ms |
10060 KB |
Output is correct |
5 |
Correct |
206 ms |
40384 KB |
Output is correct |
6 |
Correct |
88 ms |
49004 KB |
Output is correct |
7 |
Correct |
173 ms |
46224 KB |
Output is correct |
8 |
Correct |
139 ms |
39184 KB |
Output is correct |
9 |
Correct |
184 ms |
44312 KB |
Output is correct |
10 |
Correct |
146 ms |
39108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
10112 KB |
Output is correct |
4 |
Correct |
191 ms |
52456 KB |
Output is correct |
5 |
Correct |
191 ms |
54456 KB |
Output is correct |
6 |
Correct |
169 ms |
54312 KB |
Output is correct |
7 |
Correct |
209 ms |
54260 KB |
Output is correct |
8 |
Correct |
188 ms |
54384 KB |
Output is correct |
9 |
Correct |
168 ms |
54248 KB |
Output is correct |
10 |
Correct |
212 ms |
54196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
10112 KB |
Output is correct |
4 |
Correct |
191 ms |
52456 KB |
Output is correct |
5 |
Correct |
191 ms |
54456 KB |
Output is correct |
6 |
Correct |
169 ms |
54312 KB |
Output is correct |
7 |
Correct |
209 ms |
54260 KB |
Output is correct |
8 |
Correct |
188 ms |
54384 KB |
Output is correct |
9 |
Correct |
168 ms |
54248 KB |
Output is correct |
10 |
Correct |
212 ms |
54196 KB |
Output is correct |
11 |
Correct |
26 ms |
11308 KB |
Output is correct |
12 |
Correct |
191 ms |
54512 KB |
Output is correct |
13 |
Correct |
195 ms |
54492 KB |
Output is correct |
14 |
Correct |
182 ms |
54584 KB |
Output is correct |
15 |
Correct |
186 ms |
54596 KB |
Output is correct |
16 |
Correct |
170 ms |
54616 KB |
Output is correct |
17 |
Correct |
223 ms |
54548 KB |
Output is correct |
18 |
Correct |
197 ms |
54584 KB |
Output is correct |
19 |
Correct |
177 ms |
54600 KB |
Output is correct |
20 |
Correct |
213 ms |
54568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
558 ms |
43300 KB |
Output is correct |
2 |
Correct |
185 ms |
52640 KB |
Output is correct |
3 |
Correct |
389 ms |
49392 KB |
Output is correct |
4 |
Correct |
337 ms |
42360 KB |
Output is correct |
5 |
Correct |
293 ms |
48476 KB |
Output is correct |
6 |
Correct |
298 ms |
41988 KB |
Output is correct |
7 |
Correct |
386 ms |
48260 KB |
Output is correct |
8 |
Correct |
368 ms |
43344 KB |
Output is correct |
9 |
Correct |
166 ms |
52640 KB |
Output is correct |
10 |
Correct |
394 ms |
47000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9756 KB |
Output is correct |
4 |
Correct |
7 ms |
10060 KB |
Output is correct |
5 |
Correct |
206 ms |
40384 KB |
Output is correct |
6 |
Correct |
88 ms |
49004 KB |
Output is correct |
7 |
Correct |
173 ms |
46224 KB |
Output is correct |
8 |
Correct |
139 ms |
39184 KB |
Output is correct |
9 |
Correct |
184 ms |
44312 KB |
Output is correct |
10 |
Correct |
146 ms |
39108 KB |
Output is correct |
11 |
Correct |
8 ms |
10060 KB |
Output is correct |
12 |
Correct |
7 ms |
10188 KB |
Output is correct |
13 |
Correct |
9 ms |
10188 KB |
Output is correct |
14 |
Correct |
8 ms |
10120 KB |
Output is correct |
15 |
Correct |
9 ms |
10120 KB |
Output is correct |
16 |
Correct |
8 ms |
10060 KB |
Output is correct |
17 |
Correct |
8 ms |
10108 KB |
Output is correct |
18 |
Correct |
8 ms |
10116 KB |
Output is correct |
19 |
Correct |
8 ms |
10076 KB |
Output is correct |
20 |
Correct |
8 ms |
10316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9756 KB |
Output is correct |
4 |
Correct |
7 ms |
10060 KB |
Output is correct |
5 |
Correct |
206 ms |
40384 KB |
Output is correct |
6 |
Correct |
88 ms |
49004 KB |
Output is correct |
7 |
Correct |
173 ms |
46224 KB |
Output is correct |
8 |
Correct |
139 ms |
39184 KB |
Output is correct |
9 |
Correct |
184 ms |
44312 KB |
Output is correct |
10 |
Correct |
146 ms |
39108 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
8 ms |
10112 KB |
Output is correct |
14 |
Correct |
191 ms |
52456 KB |
Output is correct |
15 |
Correct |
191 ms |
54456 KB |
Output is correct |
16 |
Correct |
169 ms |
54312 KB |
Output is correct |
17 |
Correct |
209 ms |
54260 KB |
Output is correct |
18 |
Correct |
188 ms |
54384 KB |
Output is correct |
19 |
Correct |
168 ms |
54248 KB |
Output is correct |
20 |
Correct |
212 ms |
54196 KB |
Output is correct |
21 |
Correct |
26 ms |
11308 KB |
Output is correct |
22 |
Correct |
191 ms |
54512 KB |
Output is correct |
23 |
Correct |
195 ms |
54492 KB |
Output is correct |
24 |
Correct |
182 ms |
54584 KB |
Output is correct |
25 |
Correct |
186 ms |
54596 KB |
Output is correct |
26 |
Correct |
170 ms |
54616 KB |
Output is correct |
27 |
Correct |
223 ms |
54548 KB |
Output is correct |
28 |
Correct |
197 ms |
54584 KB |
Output is correct |
29 |
Correct |
177 ms |
54600 KB |
Output is correct |
30 |
Correct |
213 ms |
54568 KB |
Output is correct |
31 |
Correct |
558 ms |
43300 KB |
Output is correct |
32 |
Correct |
185 ms |
52640 KB |
Output is correct |
33 |
Correct |
389 ms |
49392 KB |
Output is correct |
34 |
Correct |
337 ms |
42360 KB |
Output is correct |
35 |
Correct |
293 ms |
48476 KB |
Output is correct |
36 |
Correct |
298 ms |
41988 KB |
Output is correct |
37 |
Correct |
386 ms |
48260 KB |
Output is correct |
38 |
Correct |
368 ms |
43344 KB |
Output is correct |
39 |
Correct |
166 ms |
52640 KB |
Output is correct |
40 |
Correct |
394 ms |
47000 KB |
Output is correct |
41 |
Correct |
8 ms |
10060 KB |
Output is correct |
42 |
Correct |
7 ms |
10188 KB |
Output is correct |
43 |
Correct |
9 ms |
10188 KB |
Output is correct |
44 |
Correct |
8 ms |
10120 KB |
Output is correct |
45 |
Correct |
9 ms |
10120 KB |
Output is correct |
46 |
Correct |
8 ms |
10060 KB |
Output is correct |
47 |
Correct |
8 ms |
10108 KB |
Output is correct |
48 |
Correct |
8 ms |
10116 KB |
Output is correct |
49 |
Correct |
8 ms |
10076 KB |
Output is correct |
50 |
Correct |
8 ms |
10316 KB |
Output is correct |
51 |
Correct |
382 ms |
43368 KB |
Output is correct |
52 |
Correct |
220 ms |
52600 KB |
Output is correct |
53 |
Correct |
439 ms |
47232 KB |
Output is correct |
54 |
Correct |
256 ms |
42100 KB |
Output is correct |
55 |
Correct |
592 ms |
43280 KB |
Output is correct |
56 |
Correct |
190 ms |
52548 KB |
Output is correct |
57 |
Correct |
335 ms |
48024 KB |
Output is correct |
58 |
Correct |
336 ms |
42092 KB |
Output is correct |
59 |
Correct |
438 ms |
43456 KB |
Output is correct |
60 |
Correct |
203 ms |
52612 KB |
Output is correct |
61 |
Correct |
298 ms |
48040 KB |
Output is correct |
62 |
Correct |
336 ms |
41756 KB |
Output is correct |
63 |
Correct |
574 ms |
43048 KB |
Output is correct |
64 |
Correct |
232 ms |
52548 KB |
Output is correct |
65 |
Correct |
381 ms |
47920 KB |
Output is correct |
66 |
Correct |
274 ms |
41872 KB |
Output is correct |
67 |
Correct |
573 ms |
43304 KB |
Output is correct |
68 |
Correct |
191 ms |
52548 KB |
Output is correct |
69 |
Correct |
290 ms |
46676 KB |
Output is correct |
70 |
Correct |
328 ms |
42100 KB |
Output is correct |