#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
pll operator + (const pll &x, const pll &y) { return pll(x.fr + y.fr, x.sc + y.sc); }
cl N = 1e5 + 7;
struct segtree {
ll l, r, sum, sum2;
segtree *lt, *rt;
inline segtree (cl &L = 1, cl &R = 0) : l(L), r(R), sum(0), sum2(0) {}
void build () {
if (l == r) return;
lt = new segtree (l, mid);
rt = new segtree (mid + 1, r);
lt->build();
rt->build();
}
void upd (cl &pos, cl &x, cl &y) {
if (l == r) {
sum = x;
sum2 = y;
return;
}
if (pos <= lt->r) lt->upd(pos, x, y);
else rt->upd(pos, x, y);
sum = lt->sum + rt->sum;
sum2 = lt->sum2 + rt->sum2;
}
pll ask (cl &ql, cl &qr) {
if (r < ql || qr < l) return pll(0, 0);
if (ql <= l && r <= qr) return pll(sum, sum2);
return lt->ask(ql, qr) + rt->ask(ql, qr);
}
} seg [N];
ll sz [N], bc [N], h [N], pos [N], head [N], par [N], dp [N], val [N], n, q;
vector <pair <pll, ll>> path [N];
vector <ll> adj [N];
ll DFS (cl &v = 1, cl &pr = 0) {
par[v] = pr; h[v] = h[pr] + 1;
for (auto &u : adj[v]) if (u != pr) sz[v] += DFS(u, v), bc[v] = (sz[u] > sz[bc[v]] ? u : bc[v]);
return ++sz[v];
}
void HLD (cl &v = 1, cl &hd = 1) {
head[v] = hd; pos[v] = h[v] - h[head[v]] + 1;
if (bc[v]) HLD(bc[v], hd);
for (auto &u : adj[v]) if (u != par[v] && u != bc[v]) HLD(u, u);
}
ll LCA (ll v, ll u) {
while (head[v] != head[u]) {
if (h[par[head[v]]] < h[par[head[u]]]) u = par[head[u]];
else v = par[head[v]];
}
return (h[v] < h[u] ? v : u);
}
ll ask (ll v, ll u, ll lca) {
pll ret = {0, 0};
for (; head[v] != head[lca]; v = par[head[v]]) ret = ret + seg[head[v]].ask(1, pos[v]);
ret = ret + seg[head[v]].ask(pos[lca] + 1, pos[v]);
for (; head[u] != head[lca]; u = par[head[u]]) ret = ret + seg[head[u]].ask(1, pos[u]);
ret = ret + seg[head[u]].ask(pos[lca] + 1, pos[u]);
ret.fr += val[lca];
return ret.fr - ret.sc;
}
void dfs (cl &v = 1) {
for (auto &u : adj[v]) if (u != par[v]) dfs(u);
dp[v] = val[v];
for (auto &x : path[v]) dp[v] = max(dp[v], ask(x.fr.fr, x.fr.sc, v) + x.sc);
seg[head[v]].upd(pos[v], val[v], dp[v]);
val[par[v]] += dp[v];
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n;
for (ll i = 1, v, u; i < n; i++) {
cin>> v >> u;
adj[v].push_back(u); adj[u].push_back(v);
}
DFS(); HLD();
for (ll v = 1; v <= n; v++) seg[head[v]].r++;
for (ll i = 1; i <= n; i++) if (seg[i].r) seg[i].build();
cin>> q;
for (ll i = 0, v, u, c; i < q; i++) {
cin>> v >> u >> c;
path[LCA(v, u)].push_back({{v, u}, c});
}
dfs();
cout<< *max_element(dp, dp + N) << endl;
cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
5
1 2
1 3
2 4
2 5
2
4 5 3
3 2 4
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
20
17 10
11 4
8 3
3 16
1 14
15 18
5 4
6 18
10 18
19 4
16 7
2 13
4 12
12 20
9 20
18 13
20 14
14 7
13 7
15
19 9 2341
13 8 6974
8 3 3339
15 17 6515
10 13 4370
1 7 8376
18 2 9272
6 7 4595
1 20 505
10 9 308
6 19 8937
2 15 5072
5 4 4217
2 4 4170
19 12 8204
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8172 KB |
Output is correct |
3 |
Correct |
6 ms |
8172 KB |
Output is correct |
4 |
Correct |
7 ms |
8300 KB |
Output is correct |
5 |
Correct |
100 ms |
20588 KB |
Output is correct |
6 |
Correct |
81 ms |
42348 KB |
Output is correct |
7 |
Correct |
96 ms |
34028 KB |
Output is correct |
8 |
Correct |
60 ms |
15468 KB |
Output is correct |
9 |
Correct |
98 ms |
30464 KB |
Output is correct |
10 |
Correct |
62 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
7 ms |
8172 KB |
Output is correct |
3 |
Correct |
7 ms |
8556 KB |
Output is correct |
4 |
Correct |
179 ms |
45548 KB |
Output is correct |
5 |
Correct |
192 ms |
45528 KB |
Output is correct |
6 |
Correct |
153 ms |
45676 KB |
Output is correct |
7 |
Correct |
175 ms |
45568 KB |
Output is correct |
8 |
Correct |
176 ms |
45548 KB |
Output is correct |
9 |
Correct |
158 ms |
45568 KB |
Output is correct |
10 |
Correct |
180 ms |
45548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
7 ms |
8172 KB |
Output is correct |
3 |
Correct |
7 ms |
8556 KB |
Output is correct |
4 |
Correct |
179 ms |
45548 KB |
Output is correct |
5 |
Correct |
192 ms |
45528 KB |
Output is correct |
6 |
Correct |
153 ms |
45676 KB |
Output is correct |
7 |
Correct |
175 ms |
45568 KB |
Output is correct |
8 |
Correct |
176 ms |
45548 KB |
Output is correct |
9 |
Correct |
158 ms |
45568 KB |
Output is correct |
10 |
Correct |
180 ms |
45548 KB |
Output is correct |
11 |
Correct |
19 ms |
9324 KB |
Output is correct |
12 |
Correct |
178 ms |
45932 KB |
Output is correct |
13 |
Correct |
179 ms |
45804 KB |
Output is correct |
14 |
Correct |
161 ms |
45932 KB |
Output is correct |
15 |
Correct |
188 ms |
45804 KB |
Output is correct |
16 |
Correct |
158 ms |
45896 KB |
Output is correct |
17 |
Correct |
174 ms |
45804 KB |
Output is correct |
18 |
Correct |
171 ms |
45804 KB |
Output is correct |
19 |
Correct |
157 ms |
45824 KB |
Output is correct |
20 |
Correct |
173 ms |
45804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
23288 KB |
Output is correct |
2 |
Correct |
154 ms |
45560 KB |
Output is correct |
3 |
Correct |
249 ms |
36716 KB |
Output is correct |
4 |
Correct |
107 ms |
18340 KB |
Output is correct |
5 |
Correct |
196 ms |
35436 KB |
Output is correct |
6 |
Correct |
113 ms |
18272 KB |
Output is correct |
7 |
Correct |
226 ms |
34984 KB |
Output is correct |
8 |
Correct |
181 ms |
23532 KB |
Output is correct |
9 |
Correct |
153 ms |
45548 KB |
Output is correct |
10 |
Correct |
247 ms |
33060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8172 KB |
Output is correct |
3 |
Correct |
6 ms |
8172 KB |
Output is correct |
4 |
Correct |
7 ms |
8300 KB |
Output is correct |
5 |
Correct |
100 ms |
20588 KB |
Output is correct |
6 |
Correct |
81 ms |
42348 KB |
Output is correct |
7 |
Correct |
96 ms |
34028 KB |
Output is correct |
8 |
Correct |
60 ms |
15468 KB |
Output is correct |
9 |
Correct |
98 ms |
30464 KB |
Output is correct |
10 |
Correct |
62 ms |
15440 KB |
Output is correct |
11 |
Correct |
8 ms |
8300 KB |
Output is correct |
12 |
Correct |
8 ms |
8556 KB |
Output is correct |
13 |
Correct |
8 ms |
8428 KB |
Output is correct |
14 |
Correct |
7 ms |
8300 KB |
Output is correct |
15 |
Correct |
8 ms |
8300 KB |
Output is correct |
16 |
Correct |
7 ms |
8428 KB |
Output is correct |
17 |
Correct |
7 ms |
8300 KB |
Output is correct |
18 |
Correct |
7 ms |
8556 KB |
Output is correct |
19 |
Correct |
7 ms |
8300 KB |
Output is correct |
20 |
Correct |
8 ms |
8556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
8172 KB |
Output is correct |
2 |
Correct |
6 ms |
8172 KB |
Output is correct |
3 |
Correct |
6 ms |
8172 KB |
Output is correct |
4 |
Correct |
7 ms |
8300 KB |
Output is correct |
5 |
Correct |
100 ms |
20588 KB |
Output is correct |
6 |
Correct |
81 ms |
42348 KB |
Output is correct |
7 |
Correct |
96 ms |
34028 KB |
Output is correct |
8 |
Correct |
60 ms |
15468 KB |
Output is correct |
9 |
Correct |
98 ms |
30464 KB |
Output is correct |
10 |
Correct |
62 ms |
15440 KB |
Output is correct |
11 |
Correct |
6 ms |
8172 KB |
Output is correct |
12 |
Correct |
7 ms |
8172 KB |
Output is correct |
13 |
Correct |
7 ms |
8556 KB |
Output is correct |
14 |
Correct |
179 ms |
45548 KB |
Output is correct |
15 |
Correct |
192 ms |
45528 KB |
Output is correct |
16 |
Correct |
153 ms |
45676 KB |
Output is correct |
17 |
Correct |
175 ms |
45568 KB |
Output is correct |
18 |
Correct |
176 ms |
45548 KB |
Output is correct |
19 |
Correct |
158 ms |
45568 KB |
Output is correct |
20 |
Correct |
180 ms |
45548 KB |
Output is correct |
21 |
Correct |
19 ms |
9324 KB |
Output is correct |
22 |
Correct |
178 ms |
45932 KB |
Output is correct |
23 |
Correct |
179 ms |
45804 KB |
Output is correct |
24 |
Correct |
161 ms |
45932 KB |
Output is correct |
25 |
Correct |
188 ms |
45804 KB |
Output is correct |
26 |
Correct |
158 ms |
45896 KB |
Output is correct |
27 |
Correct |
174 ms |
45804 KB |
Output is correct |
28 |
Correct |
171 ms |
45804 KB |
Output is correct |
29 |
Correct |
157 ms |
45824 KB |
Output is correct |
30 |
Correct |
173 ms |
45804 KB |
Output is correct |
31 |
Correct |
212 ms |
23288 KB |
Output is correct |
32 |
Correct |
154 ms |
45560 KB |
Output is correct |
33 |
Correct |
249 ms |
36716 KB |
Output is correct |
34 |
Correct |
107 ms |
18340 KB |
Output is correct |
35 |
Correct |
196 ms |
35436 KB |
Output is correct |
36 |
Correct |
113 ms |
18272 KB |
Output is correct |
37 |
Correct |
226 ms |
34984 KB |
Output is correct |
38 |
Correct |
181 ms |
23532 KB |
Output is correct |
39 |
Correct |
153 ms |
45548 KB |
Output is correct |
40 |
Correct |
247 ms |
33060 KB |
Output is correct |
41 |
Correct |
8 ms |
8300 KB |
Output is correct |
42 |
Correct |
8 ms |
8556 KB |
Output is correct |
43 |
Correct |
8 ms |
8428 KB |
Output is correct |
44 |
Correct |
7 ms |
8300 KB |
Output is correct |
45 |
Correct |
8 ms |
8300 KB |
Output is correct |
46 |
Correct |
7 ms |
8428 KB |
Output is correct |
47 |
Correct |
7 ms |
8300 KB |
Output is correct |
48 |
Correct |
7 ms |
8556 KB |
Output is correct |
49 |
Correct |
7 ms |
8300 KB |
Output is correct |
50 |
Correct |
8 ms |
8556 KB |
Output is correct |
51 |
Correct |
180 ms |
23916 KB |
Output is correct |
52 |
Correct |
176 ms |
45932 KB |
Output is correct |
53 |
Correct |
255 ms |
33704 KB |
Output is correct |
54 |
Correct |
110 ms |
18600 KB |
Output is correct |
55 |
Correct |
227 ms |
23716 KB |
Output is correct |
56 |
Correct |
174 ms |
45804 KB |
Output is correct |
57 |
Correct |
191 ms |
34668 KB |
Output is correct |
58 |
Correct |
114 ms |
18468 KB |
Output is correct |
59 |
Correct |
177 ms |
24044 KB |
Output is correct |
60 |
Correct |
194 ms |
45932 KB |
Output is correct |
61 |
Correct |
205 ms |
35052 KB |
Output is correct |
62 |
Correct |
108 ms |
18248 KB |
Output is correct |
63 |
Correct |
211 ms |
23384 KB |
Output is correct |
64 |
Correct |
175 ms |
45804 KB |
Output is correct |
65 |
Correct |
233 ms |
34852 KB |
Output is correct |
66 |
Correct |
106 ms |
18600 KB |
Output is correct |
67 |
Correct |
216 ms |
23520 KB |
Output is correct |
68 |
Correct |
176 ms |
45828 KB |
Output is correct |
69 |
Correct |
203 ms |
32364 KB |
Output is correct |
70 |
Correct |
113 ms |
18640 KB |
Output is correct |