#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 1e6;
int iDeb[MAXN], iFin[MAXN], lazy[MAXN];
pair<int, int> seg[MAXN];
void pull(int node) { seg[node] = max(seg[2 * node], seg[2 * node + 1]); }
void build(int node, int l, int r) {
iDeb[node] = l, iFin[node] = r;
lazy[node] = 0;
if (l == r) {
seg[node] = pair(0, l);
return;
}
int m = (l + r) / 2;
build(2 * node, l, m);
build(2 * node + 1, m + 1, r);
}
void push(int node) {
if (!lazy[node])
return;
int x = lazy[node];
lazy[node] = 0;
seg[node].first += x;
if (iDeb[node] < iFin[node]) {
lazy[2 * node] += x, lazy[2 * node + 1] += x;
}
}
void rangeAdd(int node, int l, int r, int x) {
push(node);
if (iDeb[node] > r or iFin[node] < l)
return;
if (l <= iDeb[node] and iFin[node] <= r) {
lazy[node] = x;
push(node);
return;
}
rangeAdd(2 * node, l, r, x);
rangeAdd(2 * node + 1, l, r, x);
pull(node);
}
const int INF = 1e18;
const int MAX = 2e5;
vector<tuple<int, int, int>> adj[MAX];
int tin[MAX], tout[MAX], par[MAXN];
int costDown[MAXN], costUp[MAXN];
int sol[MAXN];
vector<int> order;
int Time;
int nbSommet;
void dfs(int u, int p) {
par[u] = p;
tin[u] = Time++;
order.push_back(u);
for (auto [v, down, up] : adj[u])
if (v != p) {
costDown[v] = up;
costUp[v] = down;
dfs(v, u);
}
tout[u] = Time;
}
void solve1() {
int tot = 0;
for (int i = 1; i < nbSommet; ++i)
tot += costDown[i];
auto Solve = [&](auto rec, int u, int p, int curCost) -> void {
sol[1] = max(sol[1], curCost + tot);
for (auto [v, a, b] : adj[u])
if (v != p)
rec(rec, v, u, curCost + costUp[v] - costDown[v]);
};
Solve(Solve, 0, 0, 0);
}
int solve2() {
if (nbSommet == 2) {
sol[2] = 0;
for (int i = 1; i < nbSommet; ++i)
sol[2] += costDown[i] + costUp[i];
return 0;
}
int root = 0;
while (adj[root].size() == 1)
++root;
dfs(root, root);
tuple<int, int, int> ret(0, 0, 0);
int tot = 0;
for (int i = 0; i < nbSommet; ++i)
if (i != root)
tot += costDown[i];
auto Solve = [&](auto rec, int u, int p, int curDiff,
int curDepth) -> pair<int, int> {
vector<pair<int, int>> choix;
for (auto [v, a, b] : adj[u])
if (v != p)
choix.push_back(rec(rec, v, u, curDiff + costUp[v] - costDown[v],
curDepth + costUp[v]));
sort(choix.rbegin(), choix.rend());
if (choix.size() > 1) {
auto [c1, s] = choix[0];
auto [c2, t] = choix[1];
ret = max(ret, make_tuple(tot + c1 + c2 - 2 * curDepth + curDiff, s, t));
}
if (choix.empty())
return pair(curDepth, u);
return choix.front();
};
Solve(Solve, root, root, 0, 0);
auto [s, u, v] = ret;
dbg(s, u, v);
sol[2] = s;
return u;
}
void solve(int root) {
order.clear();
Time = 0;
dfs(root, root);
dbg(order);
build(1, 0, Time - 1);
int tot = 0;
for (int i = 0; i < nbSommet; ++i)
if (i != root)
tot += costDown[i];
vector<bool> cleared(nbSommet, false);
cleared[root] = true;
auto Clear = [&](auto rec, int u) -> void {
if (cleared[u])
return;
cleared[u] = true;
rangeAdd(1, tin[u], tout[u] - 1, -costUp[u]);
rec(rec, par[u]);
};
for (int u = 0; u < nbSommet; ++u)
if (u != root)
rangeAdd(1, tin[u], tout[u] - 1, costUp[u]);
int cur = tot;
for (int i = 2; i <= nbSommet; ++i) {
auto [toAdd, t] = seg[1];
cur += toAdd;
sol[i] = cur;
Clear(Clear, order[t]);
}
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> nbSommet;
int tot = 0;
for (int i = 1; i < nbSommet; ++i) {
int u, v, C, D;
cin >> u >> v >> C >> D;
--u, --v;
adj[u].emplace_back(v, C, D);
adj[v].emplace_back(u, D, C);
tot += C + D;
}
dfs(0, 0);
solve1();
int r = solve2();
solve(r);
int Q;
cin >> Q;
while (Q--) {
int E;
cin >> E;
cout << tot - sol[E] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5084 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
630 ms |
51484 KB |
Output is correct |
3 |
Correct |
622 ms |
84532 KB |
Output is correct |
4 |
Correct |
561 ms |
51332 KB |
Output is correct |
5 |
Correct |
669 ms |
52592 KB |
Output is correct |
6 |
Correct |
694 ms |
56132 KB |
Output is correct |
7 |
Correct |
566 ms |
50156 KB |
Output is correct |
8 |
Correct |
724 ms |
85424 KB |
Output is correct |
9 |
Correct |
527 ms |
53232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
650 ms |
51312 KB |
Output is correct |
3 |
Correct |
579 ms |
90088 KB |
Output is correct |
4 |
Correct |
594 ms |
51340 KB |
Output is correct |
5 |
Correct |
609 ms |
52600 KB |
Output is correct |
6 |
Correct |
681 ms |
56808 KB |
Output is correct |
7 |
Correct |
508 ms |
53376 KB |
Output is correct |
8 |
Correct |
694 ms |
72528 KB |
Output is correct |
9 |
Correct |
546 ms |
53080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5084 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
6 ms |
5572 KB |
Output is correct |
14 |
Correct |
5 ms |
5844 KB |
Output is correct |
15 |
Correct |
6 ms |
5580 KB |
Output is correct |
16 |
Correct |
9 ms |
5616 KB |
Output is correct |
17 |
Correct |
6 ms |
5628 KB |
Output is correct |
18 |
Correct |
6 ms |
5560 KB |
Output is correct |
19 |
Correct |
6 ms |
5588 KB |
Output is correct |
20 |
Correct |
6 ms |
5588 KB |
Output is correct |
21 |
Correct |
6 ms |
5628 KB |
Output is correct |
22 |
Correct |
5 ms |
5560 KB |
Output is correct |
23 |
Correct |
6 ms |
5588 KB |
Output is correct |
24 |
Correct |
5 ms |
5624 KB |
Output is correct |
25 |
Correct |
6 ms |
5972 KB |
Output is correct |
26 |
Correct |
6 ms |
5588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
630 ms |
51484 KB |
Output is correct |
3 |
Correct |
622 ms |
84532 KB |
Output is correct |
4 |
Correct |
561 ms |
51332 KB |
Output is correct |
5 |
Correct |
669 ms |
52592 KB |
Output is correct |
6 |
Correct |
694 ms |
56132 KB |
Output is correct |
7 |
Correct |
566 ms |
50156 KB |
Output is correct |
8 |
Correct |
724 ms |
85424 KB |
Output is correct |
9 |
Correct |
527 ms |
53232 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
650 ms |
51312 KB |
Output is correct |
12 |
Correct |
579 ms |
90088 KB |
Output is correct |
13 |
Correct |
594 ms |
51340 KB |
Output is correct |
14 |
Correct |
609 ms |
52600 KB |
Output is correct |
15 |
Correct |
681 ms |
56808 KB |
Output is correct |
16 |
Correct |
508 ms |
53376 KB |
Output is correct |
17 |
Correct |
694 ms |
72528 KB |
Output is correct |
18 |
Correct |
546 ms |
53080 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
643 ms |
51328 KB |
Output is correct |
21 |
Correct |
635 ms |
96856 KB |
Output is correct |
22 |
Correct |
644 ms |
56432 KB |
Output is correct |
23 |
Correct |
653 ms |
58132 KB |
Output is correct |
24 |
Correct |
619 ms |
56804 KB |
Output is correct |
25 |
Correct |
656 ms |
58092 KB |
Output is correct |
26 |
Correct |
634 ms |
56936 KB |
Output is correct |
27 |
Correct |
655 ms |
58448 KB |
Output is correct |
28 |
Correct |
691 ms |
61580 KB |
Output is correct |
29 |
Correct |
656 ms |
58576 KB |
Output is correct |
30 |
Correct |
628 ms |
57904 KB |
Output is correct |
31 |
Correct |
547 ms |
56392 KB |
Output is correct |
32 |
Correct |
722 ms |
80652 KB |
Output is correct |
33 |
Correct |
538 ms |
59692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5084 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
2 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5028 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
630 ms |
51484 KB |
Output is correct |
14 |
Correct |
622 ms |
84532 KB |
Output is correct |
15 |
Correct |
561 ms |
51332 KB |
Output is correct |
16 |
Correct |
669 ms |
52592 KB |
Output is correct |
17 |
Correct |
694 ms |
56132 KB |
Output is correct |
18 |
Correct |
566 ms |
50156 KB |
Output is correct |
19 |
Correct |
724 ms |
85424 KB |
Output is correct |
20 |
Correct |
527 ms |
53232 KB |
Output is correct |
21 |
Correct |
2 ms |
5076 KB |
Output is correct |
22 |
Correct |
650 ms |
51312 KB |
Output is correct |
23 |
Correct |
579 ms |
90088 KB |
Output is correct |
24 |
Correct |
594 ms |
51340 KB |
Output is correct |
25 |
Correct |
609 ms |
52600 KB |
Output is correct |
26 |
Correct |
681 ms |
56808 KB |
Output is correct |
27 |
Correct |
508 ms |
53376 KB |
Output is correct |
28 |
Correct |
694 ms |
72528 KB |
Output is correct |
29 |
Correct |
546 ms |
53080 KB |
Output is correct |
30 |
Correct |
3 ms |
5028 KB |
Output is correct |
31 |
Correct |
6 ms |
5572 KB |
Output is correct |
32 |
Correct |
5 ms |
5844 KB |
Output is correct |
33 |
Correct |
6 ms |
5580 KB |
Output is correct |
34 |
Correct |
9 ms |
5616 KB |
Output is correct |
35 |
Correct |
6 ms |
5628 KB |
Output is correct |
36 |
Correct |
6 ms |
5560 KB |
Output is correct |
37 |
Correct |
6 ms |
5588 KB |
Output is correct |
38 |
Correct |
6 ms |
5588 KB |
Output is correct |
39 |
Correct |
6 ms |
5628 KB |
Output is correct |
40 |
Correct |
5 ms |
5560 KB |
Output is correct |
41 |
Correct |
6 ms |
5588 KB |
Output is correct |
42 |
Correct |
5 ms |
5624 KB |
Output is correct |
43 |
Correct |
6 ms |
5972 KB |
Output is correct |
44 |
Correct |
6 ms |
5588 KB |
Output is correct |
45 |
Correct |
2 ms |
5076 KB |
Output is correct |
46 |
Correct |
643 ms |
51328 KB |
Output is correct |
47 |
Correct |
635 ms |
96856 KB |
Output is correct |
48 |
Correct |
644 ms |
56432 KB |
Output is correct |
49 |
Correct |
653 ms |
58132 KB |
Output is correct |
50 |
Correct |
619 ms |
56804 KB |
Output is correct |
51 |
Correct |
656 ms |
58092 KB |
Output is correct |
52 |
Correct |
634 ms |
56936 KB |
Output is correct |
53 |
Correct |
655 ms |
58448 KB |
Output is correct |
54 |
Correct |
691 ms |
61580 KB |
Output is correct |
55 |
Correct |
656 ms |
58576 KB |
Output is correct |
56 |
Correct |
628 ms |
57904 KB |
Output is correct |
57 |
Correct |
547 ms |
56392 KB |
Output is correct |
58 |
Correct |
722 ms |
80652 KB |
Output is correct |
59 |
Correct |
538 ms |
59692 KB |
Output is correct |
60 |
Correct |
3 ms |
5076 KB |
Output is correct |
61 |
Correct |
682 ms |
60536 KB |
Output is correct |
62 |
Correct |
652 ms |
92792 KB |
Output is correct |
63 |
Correct |
667 ms |
59048 KB |
Output is correct |
64 |
Correct |
664 ms |
60736 KB |
Output is correct |
65 |
Correct |
644 ms |
59320 KB |
Output is correct |
66 |
Correct |
658 ms |
60704 KB |
Output is correct |
67 |
Correct |
653 ms |
59348 KB |
Output is correct |
68 |
Correct |
635 ms |
61184 KB |
Output is correct |
69 |
Correct |
679 ms |
64404 KB |
Output is correct |
70 |
Correct |
658 ms |
61268 KB |
Output is correct |
71 |
Correct |
595 ms |
60664 KB |
Output is correct |
72 |
Correct |
597 ms |
59804 KB |
Output is correct |
73 |
Correct |
734 ms |
79660 KB |
Output is correct |
74 |
Correct |
546 ms |
63828 KB |
Output is correct |