#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) int(x.size())
const int mx = 100'000;
const int lg = 17;
int N, M;
vi edge[1 + mx];
int anc[1 + mx][1 + lg];
vi desc[1 + mx][1 + lg];
int depth[1 + mx];
void dfs0(int u)
{
// cerr << "dfs0 " << u << '\n';
for(int v : edge[u])
{
// cerr << "considering " << v << '\n';
if(anc[u][0] == v) continue;
// cerr << u << " -> " << v << '\n';
anc[v][0] = u;
desc[u][0].push_back(v);
depth[v] = 1 + depth[u];
dfs0(v);
}
// cerr << "u done\n";
}
int ancestor(int u, int k)
{
for(int e = 0; e <= lg; e++)
if(k & (1 << e))
u = anc[u][e];
return u;
}
pii prelca(int u, int v)
{
if(depth[u] > depth[v]) swap(u, v);
v = ancestor(v, depth[v] - depth[u]);
if(u == v) return {u, v};
for(int e = lg; e >= 0; e--)
{
if(anc[u][e] == anc[v][e]) continue;
u = anc[u][e];
v = anc[v][e];
}
return {u, v};
}
struct route
{
int a;
int b;
int c;
int aa;
int ab;
};
vector<pii> vert[1 + mx];
vector<route> horiz[1 + mx];
vi DP(1 + mx, 0);
int DPsum[1 + mx][1 + lg];
vi childsum(1 + mx, 0);
int chainsum(int u, int d)
{
int res = 0;
for(int e = lg; e >= 0; e--)
{
if(d & (1 << e))
{
res += DPsum[u][e];
u = anc[u][e];
}
}
return res;
}
void blift(int u, int e)
{
if(e == 0)
{
DPsum[u][e] = childsum[anc[u][0]] - DP[u];
}
else
{
DPsum[u][e] = DPsum[u][e-1] + DPsum[ anc[u][e-1] ][e-1];
}
if(e+1 <= lg)
{
for(int v : desc[u][e])
{
blift(v, e+1);
}
}
}
void dfs(int u)
{
for(int v : desc[u][0])
{
dfs(v);
childsum[u] += DP[v];
}
for(int v : desc[u][0])
{
blift(v, 0);
}
DP[u] = childsum[u];
for(pii vr : vert[u])
{
// cerr << "vr : " << u << " -> " << vr.first << '\n';
// int rightchild = ancestor(vr.first, depth[vr.first] - depth[u] - 1);
DP[u] = max(DP[u], /*childsum[u] - DP[rightchild] + */chainsum(vr.first, depth[vr.first] - depth[u]) + childsum[vr.first] + vr.second);
// vi good(1 + N, 0);
// for(int k = vr.first; 1; k = anc[k][0])
// {
// good[k] = 1;
// if(k == u) break;
// }
// int cr = vr.second;
// for(int i = 1; i <= N; i++)
// for(int j : desc[i][0])
// if(good[i] && !good[j]) cr += DP[j];
// DP[u] = max(DP[u], cr);
}
for(route hr : horiz[u])
{
DP[u] = max(DP[u], chainsum(hr.a, depth[hr.a] - depth[u] - 1) + chainsum(hr.b, depth[hr.b] - depth[u] - 1) + childsum[hr.a] + childsum[hr.b] + hr.c + childsum[u] - DP[hr.aa] - DP[hr.ab]);
// DP[u] = max(DP[u], chainsum(hr.a, depth[hr.a] - depth[u]) + chainsum(hr.b, depth[hr.b] - depth[u]) + childsum[hr.a] + childsum[hr.b] + hr.c - (childsum[u] - DP[hr.aa] - DP[hr.ab]));
// DP[u] = max(DP[u], childsum[u]
// - DP[hr.aa] - DP[hr.ab]
// + chainsum(hr.a, depth[hr.a] - depth[u]) + chainsum(hr.b, depth[hr.b] - depth[u])
// + childsum[hr.a] + childsum[hr.b]
// + hr.c
// - (childsum[u] - DP[hr.aa] - DP[hr.ab])
// );
// vi good(1 + N, 0);
// for(int k = hr.a; 1; k = anc[k][0])
// {
// good[k] = 1;
// if(k == u) break;
// }
// for(int k = hr.b; 1; k = anc[k][0])
// {
// good[k] = 1;
// if(k == u) break;
// }
// int cr = hr.c;
// for(int i = 1; i <= N; i++)
// for(int j : desc[i][0])
// if(good[i] && !good[j]) cr += DP[j];
// DP[u] = max(DP[u], cr);
// cerr << childsum[u] << ' ' << DP[hr.aa] << ' ' << DP[hr.ab] << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(int e = 1; e <= N-1; e++)
{
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
}
for(int i = 0; i <= N; i++)
for(int e = 0; e <= lg; e++)
anc[i][e] = 0;
depth[1] = 1;
dfs0(1);
for(int e = 1; e <= lg; e++)
{
for(int u = 0; u <= N; u++)
{
// cerr << u << ' ' << e << " ! \n";
anc[u][e] = anc[ anc[u][e-1] ][e-1];
if(anc[u][e] != 0)
desc[ anc[u][e] ][e].push_back(u);
}
}
// cerr << "A\n";
cin >> M;
for(int j = 1; j <= M; j++)
{
route r;
cin >> r.a >> r.b >> r.c;
if(depth[r.a] > depth[r.b]) swap(r.a, r.b);
pii pl = prelca(r.a, r.b);
r.aa = pl.first, r.ab = pl.second;
if(pl.first == pl.second) vert[pl.first].push_back({r.a + r.b - pl.first, r.c});
else
{
horiz[ anc[pl.first][0] ].push_back(r);
}
}
dfs(1);
cout << DP[1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
50372 KB |
Output is correct |
2 |
Correct |
25 ms |
50380 KB |
Output is correct |
3 |
Correct |
28 ms |
50380 KB |
Output is correct |
4 |
Correct |
22 ms |
50628 KB |
Output is correct |
5 |
Correct |
172 ms |
72508 KB |
Output is correct |
6 |
Correct |
297 ms |
129868 KB |
Output is correct |
7 |
Correct |
758 ms |
107696 KB |
Output is correct |
8 |
Correct |
89 ms |
70152 KB |
Output is correct |
9 |
Correct |
711 ms |
103876 KB |
Output is correct |
10 |
Correct |
105 ms |
70124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
50380 KB |
Output is correct |
2 |
Correct |
26 ms |
50372 KB |
Output is correct |
3 |
Correct |
29 ms |
50952 KB |
Output is correct |
4 |
Correct |
368 ms |
131440 KB |
Output is correct |
5 |
Correct |
344 ms |
131484 KB |
Output is correct |
6 |
Correct |
378 ms |
131684 KB |
Output is correct |
7 |
Correct |
336 ms |
131444 KB |
Output is correct |
8 |
Correct |
350 ms |
131448 KB |
Output is correct |
9 |
Correct |
339 ms |
131440 KB |
Output is correct |
10 |
Correct |
343 ms |
131484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
50380 KB |
Output is correct |
2 |
Correct |
26 ms |
50372 KB |
Output is correct |
3 |
Correct |
29 ms |
50952 KB |
Output is correct |
4 |
Correct |
368 ms |
131440 KB |
Output is correct |
5 |
Correct |
344 ms |
131484 KB |
Output is correct |
6 |
Correct |
378 ms |
131684 KB |
Output is correct |
7 |
Correct |
336 ms |
131444 KB |
Output is correct |
8 |
Correct |
350 ms |
131448 KB |
Output is correct |
9 |
Correct |
339 ms |
131440 KB |
Output is correct |
10 |
Correct |
343 ms |
131484 KB |
Output is correct |
11 |
Correct |
38 ms |
51532 KB |
Output is correct |
12 |
Correct |
353 ms |
131488 KB |
Output is correct |
13 |
Correct |
352 ms |
131416 KB |
Output is correct |
14 |
Correct |
407 ms |
131568 KB |
Output is correct |
15 |
Correct |
348 ms |
131520 KB |
Output is correct |
16 |
Correct |
347 ms |
131460 KB |
Output is correct |
17 |
Correct |
345 ms |
131400 KB |
Output is correct |
18 |
Correct |
343 ms |
131468 KB |
Output is correct |
19 |
Correct |
399 ms |
131536 KB |
Output is correct |
20 |
Correct |
339 ms |
131488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
75228 KB |
Output is correct |
2 |
Correct |
377 ms |
133736 KB |
Output is correct |
3 |
Correct |
831 ms |
111316 KB |
Output is correct |
4 |
Correct |
134 ms |
74864 KB |
Output is correct |
5 |
Correct |
772 ms |
109600 KB |
Output is correct |
6 |
Correct |
127 ms |
74664 KB |
Output is correct |
7 |
Correct |
811 ms |
109648 KB |
Output is correct |
8 |
Correct |
271 ms |
76716 KB |
Output is correct |
9 |
Correct |
315 ms |
133704 KB |
Output is correct |
10 |
Correct |
808 ms |
107816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
50372 KB |
Output is correct |
2 |
Correct |
25 ms |
50380 KB |
Output is correct |
3 |
Correct |
28 ms |
50380 KB |
Output is correct |
4 |
Correct |
22 ms |
50628 KB |
Output is correct |
5 |
Correct |
172 ms |
72508 KB |
Output is correct |
6 |
Correct |
297 ms |
129868 KB |
Output is correct |
7 |
Correct |
758 ms |
107696 KB |
Output is correct |
8 |
Correct |
89 ms |
70152 KB |
Output is correct |
9 |
Correct |
711 ms |
103876 KB |
Output is correct |
10 |
Correct |
105 ms |
70124 KB |
Output is correct |
11 |
Correct |
27 ms |
50636 KB |
Output is correct |
12 |
Correct |
30 ms |
50936 KB |
Output is correct |
13 |
Correct |
30 ms |
50864 KB |
Output is correct |
14 |
Correct |
27 ms |
50716 KB |
Output is correct |
15 |
Correct |
30 ms |
50672 KB |
Output is correct |
16 |
Correct |
26 ms |
50764 KB |
Output is correct |
17 |
Correct |
25 ms |
50684 KB |
Output is correct |
18 |
Correct |
26 ms |
50884 KB |
Output is correct |
19 |
Correct |
25 ms |
50568 KB |
Output is correct |
20 |
Correct |
32 ms |
50960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
50372 KB |
Output is correct |
2 |
Correct |
25 ms |
50380 KB |
Output is correct |
3 |
Correct |
28 ms |
50380 KB |
Output is correct |
4 |
Correct |
22 ms |
50628 KB |
Output is correct |
5 |
Correct |
172 ms |
72508 KB |
Output is correct |
6 |
Correct |
297 ms |
129868 KB |
Output is correct |
7 |
Correct |
758 ms |
107696 KB |
Output is correct |
8 |
Correct |
89 ms |
70152 KB |
Output is correct |
9 |
Correct |
711 ms |
103876 KB |
Output is correct |
10 |
Correct |
105 ms |
70124 KB |
Output is correct |
11 |
Correct |
24 ms |
50380 KB |
Output is correct |
12 |
Correct |
26 ms |
50372 KB |
Output is correct |
13 |
Correct |
29 ms |
50952 KB |
Output is correct |
14 |
Correct |
368 ms |
131440 KB |
Output is correct |
15 |
Correct |
344 ms |
131484 KB |
Output is correct |
16 |
Correct |
378 ms |
131684 KB |
Output is correct |
17 |
Correct |
336 ms |
131444 KB |
Output is correct |
18 |
Correct |
350 ms |
131448 KB |
Output is correct |
19 |
Correct |
339 ms |
131440 KB |
Output is correct |
20 |
Correct |
343 ms |
131484 KB |
Output is correct |
21 |
Correct |
38 ms |
51532 KB |
Output is correct |
22 |
Correct |
353 ms |
131488 KB |
Output is correct |
23 |
Correct |
352 ms |
131416 KB |
Output is correct |
24 |
Correct |
407 ms |
131568 KB |
Output is correct |
25 |
Correct |
348 ms |
131520 KB |
Output is correct |
26 |
Correct |
347 ms |
131460 KB |
Output is correct |
27 |
Correct |
345 ms |
131400 KB |
Output is correct |
28 |
Correct |
343 ms |
131468 KB |
Output is correct |
29 |
Correct |
399 ms |
131536 KB |
Output is correct |
30 |
Correct |
339 ms |
131488 KB |
Output is correct |
31 |
Correct |
237 ms |
75228 KB |
Output is correct |
32 |
Correct |
377 ms |
133736 KB |
Output is correct |
33 |
Correct |
831 ms |
111316 KB |
Output is correct |
34 |
Correct |
134 ms |
74864 KB |
Output is correct |
35 |
Correct |
772 ms |
109600 KB |
Output is correct |
36 |
Correct |
127 ms |
74664 KB |
Output is correct |
37 |
Correct |
811 ms |
109648 KB |
Output is correct |
38 |
Correct |
271 ms |
76716 KB |
Output is correct |
39 |
Correct |
315 ms |
133704 KB |
Output is correct |
40 |
Correct |
808 ms |
107816 KB |
Output is correct |
41 |
Correct |
27 ms |
50636 KB |
Output is correct |
42 |
Correct |
30 ms |
50936 KB |
Output is correct |
43 |
Correct |
30 ms |
50864 KB |
Output is correct |
44 |
Correct |
27 ms |
50716 KB |
Output is correct |
45 |
Correct |
30 ms |
50672 KB |
Output is correct |
46 |
Correct |
26 ms |
50764 KB |
Output is correct |
47 |
Correct |
25 ms |
50684 KB |
Output is correct |
48 |
Correct |
26 ms |
50884 KB |
Output is correct |
49 |
Correct |
25 ms |
50568 KB |
Output is correct |
50 |
Correct |
32 ms |
50960 KB |
Output is correct |
51 |
Correct |
215 ms |
77132 KB |
Output is correct |
52 |
Correct |
343 ms |
133892 KB |
Output is correct |
53 |
Correct |
806 ms |
108304 KB |
Output is correct |
54 |
Correct |
145 ms |
73896 KB |
Output is correct |
55 |
Correct |
217 ms |
77816 KB |
Output is correct |
56 |
Correct |
342 ms |
133932 KB |
Output is correct |
57 |
Correct |
766 ms |
108824 KB |
Output is correct |
58 |
Correct |
134 ms |
74992 KB |
Output is correct |
59 |
Correct |
240 ms |
77068 KB |
Output is correct |
60 |
Correct |
333 ms |
133880 KB |
Output is correct |
61 |
Correct |
781 ms |
109088 KB |
Output is correct |
62 |
Correct |
145 ms |
74468 KB |
Output is correct |
63 |
Correct |
248 ms |
77516 KB |
Output is correct |
64 |
Correct |
348 ms |
134172 KB |
Output is correct |
65 |
Correct |
801 ms |
109512 KB |
Output is correct |
66 |
Correct |
132 ms |
74144 KB |
Output is correct |
67 |
Correct |
221 ms |
77628 KB |
Output is correct |
68 |
Correct |
414 ms |
133852 KB |
Output is correct |
69 |
Correct |
776 ms |
106740 KB |
Output is correct |
70 |
Correct |
141 ms |
75040 KB |
Output is correct |