#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair
typedef long long ll;
const int maxn = 1e5+2;
const int mod = 1e9+7;
const int inf = 1e9+10;
vector<int> g[maxn];
int par[maxn][20], tin[maxn], st[maxn], tim, h[maxn], n, m;
void pfs(int v)
{
for(int i = 1; (1<<i) <= h[v]; i++)
par[v][i] = par[par[v][i-1]][i-1];
tin[v] = ++tim;
st[v] = 1;
for(auto u : g[v])
if(u != par[v][0])
{
par[u][0] = v;
h[u] = h[v] + 1;
pfs(u);
st[v] += st[u];
}
}
int get_par(int v, int he)
{
int i = 0;
while(he)
{
if(he&1) v = par[v][i];
he >>= 1; i++;
}
return v;
}
int lca(int u, int v)
{
if(h[u] > h[v]) swap(u,v);
v = get_par(v,h[v]-h[u]);
if(u == v) return v;
for(int i = 19; i >= 0; i--)
if(par[v][i] != par[u][i])
{
v = par[v][i];
u = par[u][i];
}
return par[v][0];
}
struct fenwick
{
int fen[maxn];
void _add(int pos, int val)
{
for(; pos <= n; pos |= (pos+1))
fen[pos] += val;
}
void add(int v, int val)
{
/// be zirderakht v val ta ezafe kon
int l = tin[v], r = tin[v] + st[v] - 1;
_add(l,val);
_add(r+1,-val);
}
int qu(int v)
{
int pos = tin[v], res = 0;
for(; pos >= 0; pos = (pos&(pos+1))-1)
res += fen[pos];
return res;
}
} fen;
int dp[maxn];
vector<pair<pii,int>> Q[maxn];
void dfs(int v)
{
int sum = 0;
for(auto u : g[v])
if(u != par[v][0])
{
dfs(u);
sum += dp[u];
}
dp[v] = sum;
for(auto e : Q[v])
{
int x = e.F.F, y = e.F.S, c = e.S;
int ans = fen.qu(x) + fen.qu(y) - 2 * fen.qu(par[v][0]);
/// (v = Root) ham check kon x anc y ham chek kon
ans -= sum;
dp[v] = max(dp[v],ans+c);
}
fen.add(par[v][0],dp[v]);
fen.add(v,-dp[v]);
}
signed main()
{
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n;
for(int i = 1, u, v; i < n; i++)
{
cin>> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
pfs(1);
cin>> m;
for(int i = 1, x, y, c; i <= m; i++)
{
cin>> x >> y >> c;
Q[lca(x,y)].push_back({{x,y},c});
}
dfs(1);
cout<< dp[1];
}
/*
7
3 4
6 5
2 7
1 5
7 5
4 5
5
4 3 10
5 6 5
2 6 9
7 2 2
1 3 8
8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
5
7 5 4
5 8 9
4 3 9
1 3 3
2 8 11
10
10 6
2 7
1 9
9 8
3 8
6 4
7 8
5 4
4 8
7
1 3 1
4 10 1
2 8 1
5 3 1
3 7 1
8 5 1
1 9 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
225 ms |
29688 KB |
Output is correct |
6 |
Correct |
160 ms |
36728 KB |
Output is correct |
7 |
Correct |
235 ms |
34424 KB |
Output is correct |
8 |
Correct |
191 ms |
30028 KB |
Output is correct |
9 |
Correct |
227 ms |
32888 KB |
Output is correct |
10 |
Correct |
185 ms |
29816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
5376 KB |
Output is correct |
4 |
Correct |
396 ms |
41464 KB |
Output is correct |
5 |
Correct |
388 ms |
41720 KB |
Output is correct |
6 |
Correct |
346 ms |
41592 KB |
Output is correct |
7 |
Correct |
384 ms |
41464 KB |
Output is correct |
8 |
Correct |
380 ms |
41592 KB |
Output is correct |
9 |
Correct |
352 ms |
41552 KB |
Output is correct |
10 |
Correct |
402 ms |
41676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
5376 KB |
Output is correct |
4 |
Correct |
396 ms |
41464 KB |
Output is correct |
5 |
Correct |
388 ms |
41720 KB |
Output is correct |
6 |
Correct |
346 ms |
41592 KB |
Output is correct |
7 |
Correct |
384 ms |
41464 KB |
Output is correct |
8 |
Correct |
380 ms |
41592 KB |
Output is correct |
9 |
Correct |
352 ms |
41552 KB |
Output is correct |
10 |
Correct |
402 ms |
41676 KB |
Output is correct |
11 |
Correct |
38 ms |
6520 KB |
Output is correct |
12 |
Correct |
405 ms |
41792 KB |
Output is correct |
13 |
Correct |
409 ms |
41932 KB |
Output is correct |
14 |
Correct |
366 ms |
41976 KB |
Output is correct |
15 |
Correct |
412 ms |
41828 KB |
Output is correct |
16 |
Correct |
371 ms |
41848 KB |
Output is correct |
17 |
Correct |
399 ms |
41848 KB |
Output is correct |
18 |
Correct |
388 ms |
41780 KB |
Output is correct |
19 |
Correct |
370 ms |
41976 KB |
Output is correct |
20 |
Correct |
395 ms |
41848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
416 ms |
33964 KB |
Output is correct |
2 |
Correct |
344 ms |
41592 KB |
Output is correct |
3 |
Correct |
500 ms |
38836 KB |
Output is correct |
4 |
Correct |
349 ms |
34348 KB |
Output is correct |
5 |
Correct |
452 ms |
38440 KB |
Output is correct |
6 |
Correct |
349 ms |
34152 KB |
Output is correct |
7 |
Correct |
520 ms |
37988 KB |
Output is correct |
8 |
Correct |
418 ms |
33912 KB |
Output is correct |
9 |
Correct |
354 ms |
41592 KB |
Output is correct |
10 |
Correct |
520 ms |
36776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
225 ms |
29688 KB |
Output is correct |
6 |
Correct |
160 ms |
36728 KB |
Output is correct |
7 |
Correct |
235 ms |
34424 KB |
Output is correct |
8 |
Correct |
191 ms |
30028 KB |
Output is correct |
9 |
Correct |
227 ms |
32888 KB |
Output is correct |
10 |
Correct |
185 ms |
29816 KB |
Output is correct |
11 |
Correct |
6 ms |
5376 KB |
Output is correct |
12 |
Correct |
6 ms |
5376 KB |
Output is correct |
13 |
Correct |
7 ms |
5376 KB |
Output is correct |
14 |
Correct |
7 ms |
5504 KB |
Output is correct |
15 |
Correct |
6 ms |
5376 KB |
Output is correct |
16 |
Correct |
7 ms |
5436 KB |
Output is correct |
17 |
Correct |
6 ms |
5376 KB |
Output is correct |
18 |
Correct |
7 ms |
5376 KB |
Output is correct |
19 |
Correct |
6 ms |
5376 KB |
Output is correct |
20 |
Correct |
7 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
5248 KB |
Output is correct |
5 |
Correct |
225 ms |
29688 KB |
Output is correct |
6 |
Correct |
160 ms |
36728 KB |
Output is correct |
7 |
Correct |
235 ms |
34424 KB |
Output is correct |
8 |
Correct |
191 ms |
30028 KB |
Output is correct |
9 |
Correct |
227 ms |
32888 KB |
Output is correct |
10 |
Correct |
185 ms |
29816 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
5 ms |
5376 KB |
Output is correct |
14 |
Correct |
396 ms |
41464 KB |
Output is correct |
15 |
Correct |
388 ms |
41720 KB |
Output is correct |
16 |
Correct |
346 ms |
41592 KB |
Output is correct |
17 |
Correct |
384 ms |
41464 KB |
Output is correct |
18 |
Correct |
380 ms |
41592 KB |
Output is correct |
19 |
Correct |
352 ms |
41552 KB |
Output is correct |
20 |
Correct |
402 ms |
41676 KB |
Output is correct |
21 |
Correct |
38 ms |
6520 KB |
Output is correct |
22 |
Correct |
405 ms |
41792 KB |
Output is correct |
23 |
Correct |
409 ms |
41932 KB |
Output is correct |
24 |
Correct |
366 ms |
41976 KB |
Output is correct |
25 |
Correct |
412 ms |
41828 KB |
Output is correct |
26 |
Correct |
371 ms |
41848 KB |
Output is correct |
27 |
Correct |
399 ms |
41848 KB |
Output is correct |
28 |
Correct |
388 ms |
41780 KB |
Output is correct |
29 |
Correct |
370 ms |
41976 KB |
Output is correct |
30 |
Correct |
395 ms |
41848 KB |
Output is correct |
31 |
Correct |
416 ms |
33964 KB |
Output is correct |
32 |
Correct |
344 ms |
41592 KB |
Output is correct |
33 |
Correct |
500 ms |
38836 KB |
Output is correct |
34 |
Correct |
349 ms |
34348 KB |
Output is correct |
35 |
Correct |
452 ms |
38440 KB |
Output is correct |
36 |
Correct |
349 ms |
34152 KB |
Output is correct |
37 |
Correct |
520 ms |
37988 KB |
Output is correct |
38 |
Correct |
418 ms |
33912 KB |
Output is correct |
39 |
Correct |
354 ms |
41592 KB |
Output is correct |
40 |
Correct |
520 ms |
36776 KB |
Output is correct |
41 |
Correct |
6 ms |
5376 KB |
Output is correct |
42 |
Correct |
6 ms |
5376 KB |
Output is correct |
43 |
Correct |
7 ms |
5376 KB |
Output is correct |
44 |
Correct |
7 ms |
5504 KB |
Output is correct |
45 |
Correct |
6 ms |
5376 KB |
Output is correct |
46 |
Correct |
7 ms |
5436 KB |
Output is correct |
47 |
Correct |
6 ms |
5376 KB |
Output is correct |
48 |
Correct |
7 ms |
5376 KB |
Output is correct |
49 |
Correct |
6 ms |
5376 KB |
Output is correct |
50 |
Correct |
7 ms |
5376 KB |
Output is correct |
51 |
Correct |
435 ms |
34340 KB |
Output is correct |
52 |
Correct |
433 ms |
41848 KB |
Output is correct |
53 |
Correct |
535 ms |
37288 KB |
Output is correct |
54 |
Correct |
382 ms |
34284 KB |
Output is correct |
55 |
Correct |
431 ms |
34348 KB |
Output is correct |
56 |
Correct |
413 ms |
41848 KB |
Output is correct |
57 |
Correct |
464 ms |
38196 KB |
Output is correct |
58 |
Correct |
384 ms |
34348 KB |
Output is correct |
59 |
Correct |
427 ms |
34288 KB |
Output is correct |
60 |
Correct |
395 ms |
41976 KB |
Output is correct |
61 |
Correct |
466 ms |
38196 KB |
Output is correct |
62 |
Correct |
366 ms |
34048 KB |
Output is correct |
63 |
Correct |
423 ms |
33804 KB |
Output is correct |
64 |
Correct |
388 ms |
41844 KB |
Output is correct |
65 |
Correct |
520 ms |
38060 KB |
Output is correct |
66 |
Correct |
368 ms |
34364 KB |
Output is correct |
67 |
Correct |
456 ms |
34160 KB |
Output is correct |
68 |
Correct |
393 ms |
41880 KB |
Output is correct |
69 |
Correct |
456 ms |
36788 KB |
Output is correct |
70 |
Correct |
376 ms |
34348 KB |
Output is correct |