#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
struct edge {
int u, f, b;
};
const int MN = 200005;
int n, q, u, v, w, x, cost, ans[MN], dp[MN], node[MN], pre[MN], up[MN], vis[MN], tim, in[MN], out[MN], id[MN * 2], lzy[MN * 8];
pii st[MN * 8];
vector<edge> a[MN];
void push_down(int l, int r, int idx) {
if (!lzy[idx]) return;
st[idx].f += lzy[idx];
if (l != r) lzy[idx * 2] += lzy[idx], lzy[idx * 2 + 1] += lzy[idx];
lzy[idx] = 0;
}
void build(int l, int r, int idx) {
if (l == r) {st[idx] = {0, id[l]}; return;}
int mid = (l + r) >> 1;
build(l, mid, idx * 2), build(mid + 1, r, idx * 2 + 1);
st[idx] = max(st[idx * 2], st[idx * 2 + 1]);
}
void upd(int l, int r, int x, int y, int val, int idx) {
push_down(l, r, idx);
if (r < x || l > y) return;
if (r <= y && l >= x) {
lzy[idx] += val;
push_down(l, r, idx);
return;
}
int mid = (l + r) >> 1;
upd(l, mid, x, y, val, idx * 2), upd(mid + 1, r, x, y, val, idx * 2 + 1);
st[idx] = max(st[idx * 2], st[idx * 2 + 1]);
}
void dfs1(int cur, int par) {
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.f;
dfs1(nxt.u, cur);
}
}
void dfs2(int cur, int par) {
ans[1] = min(ans[1], cost);
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.b - nxt.f;
dfs2(nxt.u, cur);
cost += nxt.f - nxt.b;
}
}
void dfs3(int cur, int par) {
node[cur] = cur;
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
cost += nxt.b - nxt.f;
dfs3(nxt.u, cur);
cost += nxt.f - nxt.b;
int val = dp[cur] + dp[nxt.u] + nxt.f;
if (cost - val < ans[2]) {
ans[2] = cost - val;
u = node[cur], v = node[nxt.u];
}
if (dp[cur] < dp[nxt.u] + nxt.f) {
dp[cur] = dp[nxt.u] + nxt.f;
node[cur] = node[nxt.u];
}
}
}
void dfs4(int cur, int par) {
in[cur] = ++tim;
id[tim] = cur;
pre[cur] = par;
for (auto nxt : a[cur]) {
if (nxt.u == par) continue;
up[nxt.u] = nxt.f;
dfs4(nxt.u, cur);
}
out[cur] = ++tim;
id[tim] = cur;
}
void solve1() {
dfs1(1, 0);
dfs2(1, 0);
}
void solve_other() {
dfs3(1, 0);
dfs4(u, 0);
build(1, n * 2, 1);
while (v != u) vis[v] = 1, v = pre[v];
vis[v] = 1;
for (int i = 1; i <= n; i++) if (!vis[i]) upd(1, n * 2, in[i], out[i], up[i], 1);
for (int i = 3; i <= n; i++) {
ans[i] = ans[i - 1] - st[1].f;
v = st[1].s;
while (!vis[v]) upd(1, n * 2, in[v], out[v], -up[v], 1), vis[v] = 1, v = pre[v];
}
}
int32_t main() {
boost();
memset(ans, 0x3f, sizeof(ans));
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v >> w >> x;
a[u].push_back({v, w, x});
a[v].push_back({u, x, w});
}
solve1();
solve_other();
cin >> q;
while (q--) {
cin >> x;
printf("%lld\n", ans[x]);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6596 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6604 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6604 KB |
Output is correct |
2 |
Correct |
532 ms |
58388 KB |
Output is correct |
3 |
Correct |
322 ms |
71852 KB |
Output is correct |
4 |
Correct |
497 ms |
63364 KB |
Output is correct |
5 |
Correct |
518 ms |
64484 KB |
Output is correct |
6 |
Correct |
528 ms |
66996 KB |
Output is correct |
7 |
Correct |
456 ms |
63360 KB |
Output is correct |
8 |
Correct |
348 ms |
79256 KB |
Output is correct |
9 |
Correct |
393 ms |
59100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
522 ms |
58372 KB |
Output is correct |
3 |
Correct |
348 ms |
74528 KB |
Output is correct |
4 |
Correct |
497 ms |
63392 KB |
Output is correct |
5 |
Correct |
513 ms |
64572 KB |
Output is correct |
6 |
Correct |
499 ms |
67452 KB |
Output is correct |
7 |
Correct |
404 ms |
62092 KB |
Output is correct |
8 |
Correct |
423 ms |
74540 KB |
Output is correct |
9 |
Correct |
418 ms |
58792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6596 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6604 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
12 |
Correct |
3 ms |
6604 KB |
Output is correct |
13 |
Correct |
6 ms |
7116 KB |
Output is correct |
14 |
Correct |
4 ms |
7116 KB |
Output is correct |
15 |
Correct |
6 ms |
7120 KB |
Output is correct |
16 |
Correct |
6 ms |
7060 KB |
Output is correct |
17 |
Correct |
7 ms |
7116 KB |
Output is correct |
18 |
Correct |
7 ms |
7116 KB |
Output is correct |
19 |
Correct |
6 ms |
7116 KB |
Output is correct |
20 |
Correct |
8 ms |
7140 KB |
Output is correct |
21 |
Correct |
6 ms |
7116 KB |
Output is correct |
22 |
Correct |
6 ms |
7116 KB |
Output is correct |
23 |
Correct |
6 ms |
7132 KB |
Output is correct |
24 |
Correct |
6 ms |
7116 KB |
Output is correct |
25 |
Correct |
5 ms |
7272 KB |
Output is correct |
26 |
Correct |
5 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6604 KB |
Output is correct |
2 |
Correct |
532 ms |
58388 KB |
Output is correct |
3 |
Correct |
322 ms |
71852 KB |
Output is correct |
4 |
Correct |
497 ms |
63364 KB |
Output is correct |
5 |
Correct |
518 ms |
64484 KB |
Output is correct |
6 |
Correct |
528 ms |
66996 KB |
Output is correct |
7 |
Correct |
456 ms |
63360 KB |
Output is correct |
8 |
Correct |
348 ms |
79256 KB |
Output is correct |
9 |
Correct |
393 ms |
59100 KB |
Output is correct |
10 |
Correct |
5 ms |
6604 KB |
Output is correct |
11 |
Correct |
522 ms |
58372 KB |
Output is correct |
12 |
Correct |
348 ms |
74528 KB |
Output is correct |
13 |
Correct |
497 ms |
63392 KB |
Output is correct |
14 |
Correct |
513 ms |
64572 KB |
Output is correct |
15 |
Correct |
499 ms |
67452 KB |
Output is correct |
16 |
Correct |
404 ms |
62092 KB |
Output is correct |
17 |
Correct |
423 ms |
74540 KB |
Output is correct |
18 |
Correct |
418 ms |
58792 KB |
Output is correct |
19 |
Correct |
3 ms |
6588 KB |
Output is correct |
20 |
Correct |
520 ms |
64776 KB |
Output is correct |
21 |
Correct |
332 ms |
74564 KB |
Output is correct |
22 |
Correct |
501 ms |
63460 KB |
Output is correct |
23 |
Correct |
532 ms |
65092 KB |
Output is correct |
24 |
Correct |
521 ms |
63996 KB |
Output is correct |
25 |
Correct |
534 ms |
65108 KB |
Output is correct |
26 |
Correct |
517 ms |
63844 KB |
Output is correct |
27 |
Correct |
499 ms |
64508 KB |
Output is correct |
28 |
Correct |
567 ms |
66976 KB |
Output is correct |
29 |
Correct |
533 ms |
65304 KB |
Output is correct |
30 |
Correct |
502 ms |
63184 KB |
Output is correct |
31 |
Correct |
432 ms |
63068 KB |
Output is correct |
32 |
Correct |
397 ms |
75208 KB |
Output is correct |
33 |
Correct |
444 ms |
59176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6604 KB |
Output is correct |
6 |
Correct |
3 ms |
6596 KB |
Output is correct |
7 |
Correct |
3 ms |
6604 KB |
Output is correct |
8 |
Correct |
3 ms |
6604 KB |
Output is correct |
9 |
Correct |
3 ms |
6604 KB |
Output is correct |
10 |
Correct |
3 ms |
6604 KB |
Output is correct |
11 |
Correct |
3 ms |
6604 KB |
Output is correct |
12 |
Correct |
3 ms |
6604 KB |
Output is correct |
13 |
Correct |
532 ms |
58388 KB |
Output is correct |
14 |
Correct |
322 ms |
71852 KB |
Output is correct |
15 |
Correct |
497 ms |
63364 KB |
Output is correct |
16 |
Correct |
518 ms |
64484 KB |
Output is correct |
17 |
Correct |
528 ms |
66996 KB |
Output is correct |
18 |
Correct |
456 ms |
63360 KB |
Output is correct |
19 |
Correct |
348 ms |
79256 KB |
Output is correct |
20 |
Correct |
393 ms |
59100 KB |
Output is correct |
21 |
Correct |
5 ms |
6604 KB |
Output is correct |
22 |
Correct |
522 ms |
58372 KB |
Output is correct |
23 |
Correct |
348 ms |
74528 KB |
Output is correct |
24 |
Correct |
497 ms |
63392 KB |
Output is correct |
25 |
Correct |
513 ms |
64572 KB |
Output is correct |
26 |
Correct |
499 ms |
67452 KB |
Output is correct |
27 |
Correct |
404 ms |
62092 KB |
Output is correct |
28 |
Correct |
423 ms |
74540 KB |
Output is correct |
29 |
Correct |
418 ms |
58792 KB |
Output is correct |
30 |
Correct |
3 ms |
6604 KB |
Output is correct |
31 |
Correct |
6 ms |
7116 KB |
Output is correct |
32 |
Correct |
4 ms |
7116 KB |
Output is correct |
33 |
Correct |
6 ms |
7120 KB |
Output is correct |
34 |
Correct |
6 ms |
7060 KB |
Output is correct |
35 |
Correct |
7 ms |
7116 KB |
Output is correct |
36 |
Correct |
7 ms |
7116 KB |
Output is correct |
37 |
Correct |
6 ms |
7116 KB |
Output is correct |
38 |
Correct |
8 ms |
7140 KB |
Output is correct |
39 |
Correct |
6 ms |
7116 KB |
Output is correct |
40 |
Correct |
6 ms |
7116 KB |
Output is correct |
41 |
Correct |
6 ms |
7132 KB |
Output is correct |
42 |
Correct |
6 ms |
7116 KB |
Output is correct |
43 |
Correct |
5 ms |
7272 KB |
Output is correct |
44 |
Correct |
5 ms |
6988 KB |
Output is correct |
45 |
Correct |
3 ms |
6588 KB |
Output is correct |
46 |
Correct |
520 ms |
64776 KB |
Output is correct |
47 |
Correct |
332 ms |
74564 KB |
Output is correct |
48 |
Correct |
501 ms |
63460 KB |
Output is correct |
49 |
Correct |
532 ms |
65092 KB |
Output is correct |
50 |
Correct |
521 ms |
63996 KB |
Output is correct |
51 |
Correct |
534 ms |
65108 KB |
Output is correct |
52 |
Correct |
517 ms |
63844 KB |
Output is correct |
53 |
Correct |
499 ms |
64508 KB |
Output is correct |
54 |
Correct |
567 ms |
66976 KB |
Output is correct |
55 |
Correct |
533 ms |
65304 KB |
Output is correct |
56 |
Correct |
502 ms |
63184 KB |
Output is correct |
57 |
Correct |
432 ms |
63068 KB |
Output is correct |
58 |
Correct |
397 ms |
75208 KB |
Output is correct |
59 |
Correct |
444 ms |
59176 KB |
Output is correct |
60 |
Correct |
4 ms |
6592 KB |
Output is correct |
61 |
Correct |
563 ms |
67468 KB |
Output is correct |
62 |
Correct |
341 ms |
73668 KB |
Output is correct |
63 |
Correct |
554 ms |
66120 KB |
Output is correct |
64 |
Correct |
602 ms |
67656 KB |
Output is correct |
65 |
Correct |
557 ms |
66304 KB |
Output is correct |
66 |
Correct |
587 ms |
67796 KB |
Output is correct |
67 |
Correct |
589 ms |
66428 KB |
Output is correct |
68 |
Correct |
595 ms |
67432 KB |
Output is correct |
69 |
Correct |
598 ms |
69324 KB |
Output is correct |
70 |
Correct |
596 ms |
67804 KB |
Output is correct |
71 |
Correct |
536 ms |
65912 KB |
Output is correct |
72 |
Correct |
496 ms |
66536 KB |
Output is correct |
73 |
Correct |
434 ms |
76012 KB |
Output is correct |
74 |
Correct |
425 ms |
63416 KB |
Output is correct |