/* I can do this all day */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
///int par[LOG][N];
int n, m, ptr, q, H[N], St[N], Fi[N], sub[N], Hv[N], Hd[N], P[N], fir[N], sec[N], cost[N];
struct Fenwick
{
ll fen[N];
void add(int i, ll x)
{
for(i += 5; i < N; i += i & -i) fen[i] += x;
}
ll _ask(int i, ll ret = 0)
{
for(i += 5; i; i -= i & -i)
{
ret += fen[i];
}
return ret;
}
ll ask(int l, int r)
{
return _ask(r) - _ask(l - 1);
}
} DP, SUM;
ll dp[N], sum[N];
vector < int > G[N], Vec[N];
void pre(int v)
{
/*for(int T = 1; T < LOG; T ++)
{
par[T][v] = par[T - 1][par[T - 1][v]];
}
*/
sub[v] = 1;
for(auto u : G[v])
{
if(u == P[v]) continue;
H[u] = H[v] + 1;
P[u] = v;
pre(u);
if(sub[u] > sub[Hv[v]]) Hv[v] = u;
sub[v] += sub[u];
}
///printf("v = %d sub = %d Hv = %d\n", v, sub[v], Hv[v]);
}
void dfs(int v)
{
St[v] = ++ptr;
if(Hv[v] > 0)
{
Hd[Hv[v]] = Hd[v];
dfs(Hv[v]);
}
for(auto u : G[v])
{
if(u == P[v] || u == Hv[v]) continue;
Hd[u] = u;
dfs(u);
}
Fi[v] = ptr;
}
inline int LCA(int v, int u)
{
while(Hd[v] != Hd[u])
{
if(H[Hd[v]] < H[Hd[u]]) swap(u, v);
v = P[Hd[v]];
}
if(H[v] < H[u]) swap(v, u);
return u;
}
inline ll G1(int v, int u)
{
ll ret = 0;
while(Hd[v] != Hd[u])
{
ret -= DP.ask(St[Hd[v]], St[v]);
ret += SUM.ask(St[Hd[v]], St[v]);
v = P[Hd[v]];
}
ret -= DP.ask(St[u], St[v]);
ret += SUM.ask(St[u], St[v]);
return ret;
}
void solve(int v)
{
for(auto u : G[v])
{
if(u == P[v]) continue;
solve(u);
sum[v] += dp[u];
}
dp[v] = sum[v];
for(auto id : Vec[v])
{
ll now = G1(fir[id], v) + G1(sec[id], v) + sum[v] + cost[id];
dp[v] = max(dp[v], now);
}
SUM.add(St[v], sum[v]);
DP.add(St[v], dp[v]);
///printf("v = %d dp = %lld sum = %lld\n", v, dp[v], sum[v]);
}
int main()
{
fast_io;
cin >> n;
for(int i = 1; i < n; i ++)
{
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
pre(1);
dfs(Hd[1] = 1);
cin >> q;
for(int i = 1; i <= q; i ++)
{
cin >> fir[i] >> sec[i] >> cost[i];
int lca = LCA(fir[i], sec[i]);
Vec[lca].push_back(i);
///printf("lca = %d\n", lca);
}
solve(1);
cout << dp[1];
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47412 KB |
Output is correct |
2 |
Correct |
27 ms |
47448 KB |
Output is correct |
3 |
Correct |
28 ms |
47456 KB |
Output is correct |
4 |
Correct |
29 ms |
47544 KB |
Output is correct |
5 |
Correct |
112 ms |
57656 KB |
Output is correct |
6 |
Correct |
96 ms |
70088 KB |
Output is correct |
7 |
Correct |
84 ms |
65784 KB |
Output is correct |
8 |
Correct |
71 ms |
56944 KB |
Output is correct |
9 |
Correct |
82 ms |
63276 KB |
Output is correct |
10 |
Correct |
75 ms |
57012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47568 KB |
Output is correct |
2 |
Correct |
26 ms |
47440 KB |
Output is correct |
3 |
Correct |
25 ms |
47688 KB |
Output is correct |
4 |
Correct |
104 ms |
73584 KB |
Output is correct |
5 |
Correct |
107 ms |
73656 KB |
Output is correct |
6 |
Correct |
112 ms |
73680 KB |
Output is correct |
7 |
Correct |
113 ms |
73676 KB |
Output is correct |
8 |
Correct |
110 ms |
73700 KB |
Output is correct |
9 |
Correct |
106 ms |
73716 KB |
Output is correct |
10 |
Correct |
113 ms |
73684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47568 KB |
Output is correct |
2 |
Correct |
26 ms |
47440 KB |
Output is correct |
3 |
Correct |
25 ms |
47688 KB |
Output is correct |
4 |
Correct |
104 ms |
73584 KB |
Output is correct |
5 |
Correct |
107 ms |
73656 KB |
Output is correct |
6 |
Correct |
112 ms |
73680 KB |
Output is correct |
7 |
Correct |
113 ms |
73676 KB |
Output is correct |
8 |
Correct |
110 ms |
73700 KB |
Output is correct |
9 |
Correct |
106 ms |
73716 KB |
Output is correct |
10 |
Correct |
113 ms |
73684 KB |
Output is correct |
11 |
Correct |
35 ms |
48452 KB |
Output is correct |
12 |
Correct |
111 ms |
73924 KB |
Output is correct |
13 |
Correct |
112 ms |
73964 KB |
Output is correct |
14 |
Correct |
129 ms |
73992 KB |
Output is correct |
15 |
Correct |
112 ms |
73932 KB |
Output is correct |
16 |
Correct |
108 ms |
73968 KB |
Output is correct |
17 |
Correct |
110 ms |
73976 KB |
Output is correct |
18 |
Correct |
116 ms |
73992 KB |
Output is correct |
19 |
Correct |
109 ms |
73956 KB |
Output is correct |
20 |
Correct |
126 ms |
73908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
211 ms |
60708 KB |
Output is correct |
2 |
Correct |
101 ms |
73676 KB |
Output is correct |
3 |
Correct |
141 ms |
68792 KB |
Output is correct |
4 |
Correct |
119 ms |
59992 KB |
Output is correct |
5 |
Correct |
167 ms |
68060 KB |
Output is correct |
6 |
Correct |
133 ms |
60116 KB |
Output is correct |
7 |
Correct |
149 ms |
67696 KB |
Output is correct |
8 |
Correct |
187 ms |
61132 KB |
Output is correct |
9 |
Correct |
106 ms |
73644 KB |
Output is correct |
10 |
Correct |
146 ms |
66468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47412 KB |
Output is correct |
2 |
Correct |
27 ms |
47448 KB |
Output is correct |
3 |
Correct |
28 ms |
47456 KB |
Output is correct |
4 |
Correct |
29 ms |
47544 KB |
Output is correct |
5 |
Correct |
112 ms |
57656 KB |
Output is correct |
6 |
Correct |
96 ms |
70088 KB |
Output is correct |
7 |
Correct |
84 ms |
65784 KB |
Output is correct |
8 |
Correct |
71 ms |
56944 KB |
Output is correct |
9 |
Correct |
82 ms |
63276 KB |
Output is correct |
10 |
Correct |
75 ms |
57012 KB |
Output is correct |
11 |
Correct |
28 ms |
47556 KB |
Output is correct |
12 |
Correct |
27 ms |
47696 KB |
Output is correct |
13 |
Correct |
27 ms |
47692 KB |
Output is correct |
14 |
Correct |
28 ms |
47516 KB |
Output is correct |
15 |
Correct |
29 ms |
47516 KB |
Output is correct |
16 |
Correct |
27 ms |
47648 KB |
Output is correct |
17 |
Correct |
27 ms |
47568 KB |
Output is correct |
18 |
Correct |
26 ms |
47596 KB |
Output is correct |
19 |
Correct |
27 ms |
47612 KB |
Output is correct |
20 |
Correct |
26 ms |
47724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47412 KB |
Output is correct |
2 |
Correct |
27 ms |
47448 KB |
Output is correct |
3 |
Correct |
28 ms |
47456 KB |
Output is correct |
4 |
Correct |
29 ms |
47544 KB |
Output is correct |
5 |
Correct |
112 ms |
57656 KB |
Output is correct |
6 |
Correct |
96 ms |
70088 KB |
Output is correct |
7 |
Correct |
84 ms |
65784 KB |
Output is correct |
8 |
Correct |
71 ms |
56944 KB |
Output is correct |
9 |
Correct |
82 ms |
63276 KB |
Output is correct |
10 |
Correct |
75 ms |
57012 KB |
Output is correct |
11 |
Correct |
29 ms |
47568 KB |
Output is correct |
12 |
Correct |
26 ms |
47440 KB |
Output is correct |
13 |
Correct |
25 ms |
47688 KB |
Output is correct |
14 |
Correct |
104 ms |
73584 KB |
Output is correct |
15 |
Correct |
107 ms |
73656 KB |
Output is correct |
16 |
Correct |
112 ms |
73680 KB |
Output is correct |
17 |
Correct |
113 ms |
73676 KB |
Output is correct |
18 |
Correct |
110 ms |
73700 KB |
Output is correct |
19 |
Correct |
106 ms |
73716 KB |
Output is correct |
20 |
Correct |
113 ms |
73684 KB |
Output is correct |
21 |
Correct |
35 ms |
48452 KB |
Output is correct |
22 |
Correct |
111 ms |
73924 KB |
Output is correct |
23 |
Correct |
112 ms |
73964 KB |
Output is correct |
24 |
Correct |
129 ms |
73992 KB |
Output is correct |
25 |
Correct |
112 ms |
73932 KB |
Output is correct |
26 |
Correct |
108 ms |
73968 KB |
Output is correct |
27 |
Correct |
110 ms |
73976 KB |
Output is correct |
28 |
Correct |
116 ms |
73992 KB |
Output is correct |
29 |
Correct |
109 ms |
73956 KB |
Output is correct |
30 |
Correct |
126 ms |
73908 KB |
Output is correct |
31 |
Correct |
211 ms |
60708 KB |
Output is correct |
32 |
Correct |
101 ms |
73676 KB |
Output is correct |
33 |
Correct |
141 ms |
68792 KB |
Output is correct |
34 |
Correct |
119 ms |
59992 KB |
Output is correct |
35 |
Correct |
167 ms |
68060 KB |
Output is correct |
36 |
Correct |
133 ms |
60116 KB |
Output is correct |
37 |
Correct |
149 ms |
67696 KB |
Output is correct |
38 |
Correct |
187 ms |
61132 KB |
Output is correct |
39 |
Correct |
106 ms |
73644 KB |
Output is correct |
40 |
Correct |
146 ms |
66468 KB |
Output is correct |
41 |
Correct |
28 ms |
47556 KB |
Output is correct |
42 |
Correct |
27 ms |
47696 KB |
Output is correct |
43 |
Correct |
27 ms |
47692 KB |
Output is correct |
44 |
Correct |
28 ms |
47516 KB |
Output is correct |
45 |
Correct |
29 ms |
47516 KB |
Output is correct |
46 |
Correct |
27 ms |
47648 KB |
Output is correct |
47 |
Correct |
27 ms |
47568 KB |
Output is correct |
48 |
Correct |
26 ms |
47596 KB |
Output is correct |
49 |
Correct |
27 ms |
47612 KB |
Output is correct |
50 |
Correct |
26 ms |
47724 KB |
Output is correct |
51 |
Correct |
162 ms |
61456 KB |
Output is correct |
52 |
Correct |
111 ms |
73916 KB |
Output is correct |
53 |
Correct |
138 ms |
66776 KB |
Output is correct |
54 |
Correct |
118 ms |
60248 KB |
Output is correct |
55 |
Correct |
211 ms |
61064 KB |
Output is correct |
56 |
Correct |
111 ms |
73916 KB |
Output is correct |
57 |
Correct |
143 ms |
67840 KB |
Output is correct |
58 |
Correct |
119 ms |
60352 KB |
Output is correct |
59 |
Correct |
176 ms |
61508 KB |
Output is correct |
60 |
Correct |
113 ms |
73912 KB |
Output is correct |
61 |
Correct |
157 ms |
67916 KB |
Output is correct |
62 |
Correct |
144 ms |
60396 KB |
Output is correct |
63 |
Correct |
183 ms |
61104 KB |
Output is correct |
64 |
Correct |
111 ms |
73928 KB |
Output is correct |
65 |
Correct |
143 ms |
67652 KB |
Output is correct |
66 |
Correct |
145 ms |
60324 KB |
Output is correct |
67 |
Correct |
185 ms |
61124 KB |
Output is correct |
68 |
Correct |
115 ms |
73936 KB |
Output is correct |
69 |
Correct |
135 ms |
65864 KB |
Output is correct |
70 |
Correct |
120 ms |
60356 KB |
Output is correct |