// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, m;
vector<int> adj[maxN];
struct TPath
{
int s, t, w;
} a[maxN];
int sz[maxN], p[maxN], depth[maxN];
int chainhead[maxN], chainid[maxN], nChain = 0;
int timein[maxN], Time = 0;
vector<TPath> vc[maxN];
int f[maxN];
struct SegTree
{
int st[4 * maxN];
void Init()
{
memset(st, 0, sizeof st);
}
void update(int id, int l, int r, int pos, int val)
{
if(l > pos || r < pos) return;
if(l == r && l == pos)
{
st[id] += val;
return;
}
int mid = (l + r) / 2;
update(id * 2, l, mid, pos, val);
update(id * 2 + 1, mid + 1, r, pos, val);
st[id] = st[id * 2] + st[id * 2 + 1];
}
int get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return 0;
if(l >= u && r <= v) return st[id];
int mid = (l + r) / 2;
return get(id * 2, l, mid, u, v) + get(id * 2 + 1, mid + 1, r, u, v);
}
} tree1, tree2;
void dfs(int u)
{
sz[u] = 1;
for(int v : adj[u])
{
if(v == p[u]) continue;
p[v] = u;
depth[v] = depth[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
void HLD(int u)
{
if(!chainhead[nChain]) chainhead[nChain] = u;
chainid[u] = nChain;
timein[u] = ++Time;
int bigChild = -1;
for(int v : adj[u])
{
if(v == p[u]) continue;
if(bigChild == -1 || sz[bigChild] < sz[v]) bigChild = v;
}
if(bigChild != -1) HLD(bigChild);
for(int v : adj[u])
{
if(v == p[u] || v == bigChild) continue;
nChain++;
HLD(v);
}
}
int LCA(int u, int v)
{
while(chainhead[chainid[u]] != chainhead[chainid[v]])
{
if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
u = p[chainhead[chainid[u]]];
}
if(depth[u] > depth[v]) swap(u, v);
return u;
}
int Query(int u, int v)
{
int res = 0;
while(chainhead[chainid[u]] != chainhead[chainid[v]])
{
if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
res += tree1.get(1, 1, n, timein[chainhead[chainid[u]]], timein[u]);
u = p[chainhead[chainid[u]]];
}
if(depth[u] > depth[v]) swap(u, v);
res += tree1.get(1, 1, n, timein[u], timein[v]);
return res;
}
int Query1(int u, int v)
{
int res = 0;
while(chainhead[chainid[u]] != chainhead[chainid[v]])
{
if(depth[chainhead[chainid[u]]] < depth[chainhead[chainid[v]]]) swap(u, v);
res += tree2.get(1, 1, n, timein[chainhead[chainid[u]]], timein[u]);
u = p[chainhead[chainid[u]]];
}
if(depth[u] > depth[v]) swap(u, v);
res += tree2.get(1, 1, n, timein[u], timein[v]);
return res;
}
void ReadInput()
{
cin >> n;
for(int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
cin >> m;
for(int i=1; i<=m; i++)
{
cin >> a[i].s >> a[i].t >> a[i].w;
}
}
void DFS(int u)
{
int sum = 0;
for(int v : adj[u])
{
if(v == p[u]) continue;
DFS(v);
sum += f[v];
}
f[u] = sum;
for(int v : adj[u])
{
if(v == p[u]) continue;
tree1.update(1, 1, n, timein[u], f[v]);
}
for(TPath a : vc[u])
{
f[u] = max(f[u], Query(a.s, a.t) - Query1(a.s, a.t) + a.w);
}
tree2.update(1, 1, n, timein[u], f[u]);
}
void Solve()
{
dfs(1);
HLD(1);
tree1.Init();
tree2.Init();
for(int i=1; i<=m; i++)
vc[LCA(a[i].s, a[i].t)].pb(a[i]);
DFS(1);
cout << f[1];
}
int32_t main()
{
//freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
6 ms |
11308 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11440 KB |
Output is correct |
5 |
Correct |
109 ms |
21348 KB |
Output is correct |
6 |
Correct |
59 ms |
34248 KB |
Output is correct |
7 |
Correct |
90 ms |
29996 KB |
Output is correct |
8 |
Correct |
79 ms |
21920 KB |
Output is correct |
9 |
Correct |
96 ms |
27272 KB |
Output is correct |
10 |
Correct |
78 ms |
21868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11316 KB |
Output is correct |
2 |
Correct |
6 ms |
11220 KB |
Output is correct |
3 |
Correct |
6 ms |
11476 KB |
Output is correct |
4 |
Correct |
147 ms |
41516 KB |
Output is correct |
5 |
Correct |
141 ms |
41580 KB |
Output is correct |
6 |
Correct |
127 ms |
41484 KB |
Output is correct |
7 |
Correct |
146 ms |
41500 KB |
Output is correct |
8 |
Correct |
142 ms |
41444 KB |
Output is correct |
9 |
Correct |
138 ms |
41524 KB |
Output is correct |
10 |
Correct |
140 ms |
41444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11316 KB |
Output is correct |
2 |
Correct |
6 ms |
11220 KB |
Output is correct |
3 |
Correct |
6 ms |
11476 KB |
Output is correct |
4 |
Correct |
147 ms |
41516 KB |
Output is correct |
5 |
Correct |
141 ms |
41580 KB |
Output is correct |
6 |
Correct |
127 ms |
41484 KB |
Output is correct |
7 |
Correct |
146 ms |
41500 KB |
Output is correct |
8 |
Correct |
142 ms |
41444 KB |
Output is correct |
9 |
Correct |
138 ms |
41524 KB |
Output is correct |
10 |
Correct |
140 ms |
41444 KB |
Output is correct |
11 |
Correct |
20 ms |
13244 KB |
Output is correct |
12 |
Correct |
163 ms |
41788 KB |
Output is correct |
13 |
Correct |
147 ms |
41884 KB |
Output is correct |
14 |
Correct |
132 ms |
41956 KB |
Output is correct |
15 |
Correct |
158 ms |
41796 KB |
Output is correct |
16 |
Correct |
143 ms |
41844 KB |
Output is correct |
17 |
Correct |
142 ms |
41804 KB |
Output is correct |
18 |
Correct |
147 ms |
41820 KB |
Output is correct |
19 |
Correct |
132 ms |
41788 KB |
Output is correct |
20 |
Correct |
143 ms |
41712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
504 ms |
28032 KB |
Output is correct |
2 |
Correct |
124 ms |
41548 KB |
Output is correct |
3 |
Correct |
217 ms |
36536 KB |
Output is correct |
4 |
Correct |
251 ms |
28848 KB |
Output is correct |
5 |
Correct |
180 ms |
35528 KB |
Output is correct |
6 |
Correct |
216 ms |
28608 KB |
Output is correct |
7 |
Correct |
229 ms |
35328 KB |
Output is correct |
8 |
Correct |
271 ms |
28112 KB |
Output is correct |
9 |
Correct |
122 ms |
41436 KB |
Output is correct |
10 |
Correct |
234 ms |
33380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
6 ms |
11308 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11440 KB |
Output is correct |
5 |
Correct |
109 ms |
21348 KB |
Output is correct |
6 |
Correct |
59 ms |
34248 KB |
Output is correct |
7 |
Correct |
90 ms |
29996 KB |
Output is correct |
8 |
Correct |
79 ms |
21920 KB |
Output is correct |
9 |
Correct |
96 ms |
27272 KB |
Output is correct |
10 |
Correct |
78 ms |
21868 KB |
Output is correct |
11 |
Correct |
7 ms |
11476 KB |
Output is correct |
12 |
Correct |
7 ms |
11576 KB |
Output is correct |
13 |
Correct |
7 ms |
11580 KB |
Output is correct |
14 |
Correct |
9 ms |
11476 KB |
Output is correct |
15 |
Correct |
8 ms |
11448 KB |
Output is correct |
16 |
Correct |
7 ms |
11476 KB |
Output is correct |
17 |
Correct |
7 ms |
11524 KB |
Output is correct |
18 |
Correct |
7 ms |
11476 KB |
Output is correct |
19 |
Correct |
7 ms |
11576 KB |
Output is correct |
20 |
Correct |
7 ms |
11576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11220 KB |
Output is correct |
2 |
Correct |
6 ms |
11308 KB |
Output is correct |
3 |
Correct |
5 ms |
11220 KB |
Output is correct |
4 |
Correct |
7 ms |
11440 KB |
Output is correct |
5 |
Correct |
109 ms |
21348 KB |
Output is correct |
6 |
Correct |
59 ms |
34248 KB |
Output is correct |
7 |
Correct |
90 ms |
29996 KB |
Output is correct |
8 |
Correct |
79 ms |
21920 KB |
Output is correct |
9 |
Correct |
96 ms |
27272 KB |
Output is correct |
10 |
Correct |
78 ms |
21868 KB |
Output is correct |
11 |
Correct |
6 ms |
11316 KB |
Output is correct |
12 |
Correct |
6 ms |
11220 KB |
Output is correct |
13 |
Correct |
6 ms |
11476 KB |
Output is correct |
14 |
Correct |
147 ms |
41516 KB |
Output is correct |
15 |
Correct |
141 ms |
41580 KB |
Output is correct |
16 |
Correct |
127 ms |
41484 KB |
Output is correct |
17 |
Correct |
146 ms |
41500 KB |
Output is correct |
18 |
Correct |
142 ms |
41444 KB |
Output is correct |
19 |
Correct |
138 ms |
41524 KB |
Output is correct |
20 |
Correct |
140 ms |
41444 KB |
Output is correct |
21 |
Correct |
20 ms |
13244 KB |
Output is correct |
22 |
Correct |
163 ms |
41788 KB |
Output is correct |
23 |
Correct |
147 ms |
41884 KB |
Output is correct |
24 |
Correct |
132 ms |
41956 KB |
Output is correct |
25 |
Correct |
158 ms |
41796 KB |
Output is correct |
26 |
Correct |
143 ms |
41844 KB |
Output is correct |
27 |
Correct |
142 ms |
41804 KB |
Output is correct |
28 |
Correct |
147 ms |
41820 KB |
Output is correct |
29 |
Correct |
132 ms |
41788 KB |
Output is correct |
30 |
Correct |
143 ms |
41712 KB |
Output is correct |
31 |
Correct |
504 ms |
28032 KB |
Output is correct |
32 |
Correct |
124 ms |
41548 KB |
Output is correct |
33 |
Correct |
217 ms |
36536 KB |
Output is correct |
34 |
Correct |
251 ms |
28848 KB |
Output is correct |
35 |
Correct |
180 ms |
35528 KB |
Output is correct |
36 |
Correct |
216 ms |
28608 KB |
Output is correct |
37 |
Correct |
229 ms |
35328 KB |
Output is correct |
38 |
Correct |
271 ms |
28112 KB |
Output is correct |
39 |
Correct |
122 ms |
41436 KB |
Output is correct |
40 |
Correct |
234 ms |
33380 KB |
Output is correct |
41 |
Correct |
7 ms |
11476 KB |
Output is correct |
42 |
Correct |
7 ms |
11576 KB |
Output is correct |
43 |
Correct |
7 ms |
11580 KB |
Output is correct |
44 |
Correct |
9 ms |
11476 KB |
Output is correct |
45 |
Correct |
8 ms |
11448 KB |
Output is correct |
46 |
Correct |
7 ms |
11476 KB |
Output is correct |
47 |
Correct |
7 ms |
11524 KB |
Output is correct |
48 |
Correct |
7 ms |
11476 KB |
Output is correct |
49 |
Correct |
7 ms |
11576 KB |
Output is correct |
50 |
Correct |
7 ms |
11576 KB |
Output is correct |
51 |
Correct |
329 ms |
28316 KB |
Output is correct |
52 |
Correct |
152 ms |
41704 KB |
Output is correct |
53 |
Correct |
272 ms |
33892 KB |
Output is correct |
54 |
Correct |
200 ms |
28800 KB |
Output is correct |
55 |
Correct |
428 ms |
28160 KB |
Output is correct |
56 |
Correct |
156 ms |
41976 KB |
Output is correct |
57 |
Correct |
181 ms |
34996 KB |
Output is correct |
58 |
Correct |
238 ms |
28800 KB |
Output is correct |
59 |
Correct |
308 ms |
28388 KB |
Output is correct |
60 |
Correct |
143 ms |
41708 KB |
Output is correct |
61 |
Correct |
187 ms |
35136 KB |
Output is correct |
62 |
Correct |
222 ms |
28504 KB |
Output is correct |
63 |
Correct |
412 ms |
28016 KB |
Output is correct |
64 |
Correct |
153 ms |
41752 KB |
Output is correct |
65 |
Correct |
219 ms |
34988 KB |
Output is correct |
66 |
Correct |
186 ms |
28812 KB |
Output is correct |
67 |
Correct |
426 ms |
28096 KB |
Output is correct |
68 |
Correct |
145 ms |
41784 KB |
Output is correct |
69 |
Correct |
190 ms |
32908 KB |
Output is correct |
70 |
Correct |
199 ms |
28788 KB |
Output is correct |