#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct edge_t {
int v;
int away;
int come;
edge_t() {}
edge_t(int v, int a, int c) : v(v), away(a), come(c) {}
};
int N;
vector<vector<edge_t>> adj;
vector<lint> Answer;
vector<lint> BaseCost; // BaseCost[i] = sum of all edges going to i (root at i), so we do not need to consider them afterwards
vector<bool> processed; // processed as R, done via centroid decomposition
vector<int> sz; // subtree size
vector<lint> optimal; // maximum weighted depth
void GenerateBaseCost() {
vector<lint> dp(N);
function<void(int, int)> dfs = [&](int n, int p) {
for (const auto &i : adj[n]) if (i.v != p) {
dfs(i.v, n);
dp[n] += dp[i.v] + i.come;
}
};
function<void(int, int)> reroot = [&](int n, int p) {
BaseCost[n] = dp[n];
for (const auto &i : adj[n]) if (i.v != p) {
dp[n] -= dp[i.v] + i.come;
dp[i.v] += dp[n] + i.away;
reroot(i.v, n);
dp[i.v] -= dp[n] + i.away;
dp[n] += dp[i.v] + i.come;
}
};
dfs(0, -1);
reroot(0, -1);
}
int Centroid(int n) {
function<void(int, int)> dfs = [&](int n, int p) {
sz[n] = 1;
for (const auto &i : adj[n]) if (i.v != p && !processed[i.v]) {
dfs(i.v, n);
sz[n] += sz[i.v];
}
};
dfs(n, -1);
int cur = n, p = -1;
while (true) {
pair<int, int> mx = {-1, -1};
for (const auto &i : adj[cur]) if (i.v != p && !processed[i.v]) {
mx = max(mx, {sz[i.v], i.v});
}
if (mx.first * 2 <= sz[n]) {
return cur;
}
p = cur;
cur = mx.second;
}
}
void Solve(int R) {
function<lint(int, int, vector<lint>&)> dfs = [&](int n, int p, vector<lint> &v) {
lint cur = 0;
for (const auto &i : adj[n]) if (i.v != p && !processed[i.v]) {
lint nxt = dfs(i.v, n, v) + i.away;
if (cur < nxt) swap(cur, nxt);
if (nxt > 0) v.emplace_back(nxt);
}
return cur;
};
vector<lint> all;
for (const auto &i : adj[R]) if (!processed[i.v]) {
optimal[i.v] = dfs(i.v, R, all) + i.away;
all.emplace_back(optimal[i.v]);
}
sort(begin(all), end(all), greater<lint>());
{ // Case #1: R is chosen
lint sum = BaseCost[R];
int cnt = 1;
Answer[cnt] = max(Answer[cnt], sum);
for (int i = 0; i < all.size(); i++) {
sum += all[i], cnt++;
Answer[cnt] = max(Answer[cnt], sum);
}
}
{ // Case #2: R is not chosen, two different subtrees
pair<lint, lint> mx = {-1, -1};
for (const auto &i : adj[R]) if (!processed[i.v]) {
if (optimal[i.v] > mx.first) {
mx.second = mx.first;
mx.first = optimal[i.v];
} else if (optimal[i.v] > mx.second) {
mx.second = optimal[i.v];
}
}
if (mx.first != -1 && mx.second != -1) {
lint sum = BaseCost[R] + mx.first + mx.second;
int cnt = 2;
Answer[cnt] = max(Answer[cnt], sum);
for (int i = 0; i < all.size(); i++) {
if (all[i] == mx.first) {
mx.first = -1;
} else if (all[i] == mx.second) {
mx.second = -1;
} else {
sum += all[i], cnt++;
Answer[cnt] = max(Answer[cnt], sum);
}
}
}
}
}
void CentroidDecomposition() {
queue<int> q;
q.emplace(0);
while (!q.empty()) {
int R = Centroid(q.front());
q.pop();
Solve(R);
processed[R] = true;
for (const auto &i : adj[R]) if (!processed[i.v]) {
q.emplace(i.v);
}
}
for (int i = 1; i <= N; i++) {
Answer[i] = max(Answer[i], Answer[i - 1]);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
sz.resize(N);
adj.resize(N);
optimal.resize(N);
BaseCost.resize(N);
Answer.resize(N + 1);
processed.resize(N, false);
lint sum_all_costs = 0;
for (int i = 0; i < N - 1; i++) {
int A, B, C, D;
cin >> A >> B >> C >> D;
A--, B--;
adj[A].emplace_back(B, C, D);
adj[B].emplace_back(A, D, C);
sum_all_costs += C + D;
}
GenerateBaseCost();
CentroidDecomposition();
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
int E;
cin >> E;
cout << (sum_all_costs - Answer[E]) << "\n";
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void Solve(int)':
designated_cities.cpp:95:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < all.size(); i++) {
~~^~~~~~~~~~~~
designated_cities.cpp:117:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < all.size(); i++) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
881 ms |
20732 KB |
Output is correct |
3 |
Correct |
1258 ms |
32344 KB |
Output is correct |
4 |
Correct |
824 ms |
20956 KB |
Output is correct |
5 |
Correct |
438 ms |
20892 KB |
Output is correct |
6 |
Correct |
1006 ms |
23000 KB |
Output is correct |
7 |
Correct |
319 ms |
21004 KB |
Output is correct |
8 |
Correct |
1272 ms |
33544 KB |
Output is correct |
9 |
Correct |
213 ms |
23180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
874 ms |
20980 KB |
Output is correct |
3 |
Correct |
1280 ms |
35424 KB |
Output is correct |
4 |
Correct |
839 ms |
21080 KB |
Output is correct |
5 |
Correct |
417 ms |
21012 KB |
Output is correct |
6 |
Correct |
1039 ms |
23600 KB |
Output is correct |
7 |
Correct |
261 ms |
23308 KB |
Output is correct |
8 |
Correct |
1201 ms |
29912 KB |
Output is correct |
9 |
Correct |
211 ms |
23564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
512 KB |
Output is correct |
14 |
Correct |
9 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
512 KB |
Output is correct |
16 |
Correct |
8 ms |
640 KB |
Output is correct |
17 |
Correct |
8 ms |
512 KB |
Output is correct |
18 |
Correct |
7 ms |
512 KB |
Output is correct |
19 |
Correct |
7 ms |
512 KB |
Output is correct |
20 |
Correct |
7 ms |
512 KB |
Output is correct |
21 |
Correct |
8 ms |
512 KB |
Output is correct |
22 |
Correct |
9 ms |
512 KB |
Output is correct |
23 |
Correct |
7 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
548 KB |
Output is correct |
25 |
Correct |
8 ms |
640 KB |
Output is correct |
26 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
881 ms |
20732 KB |
Output is correct |
3 |
Correct |
1258 ms |
32344 KB |
Output is correct |
4 |
Correct |
824 ms |
20956 KB |
Output is correct |
5 |
Correct |
438 ms |
20892 KB |
Output is correct |
6 |
Correct |
1006 ms |
23000 KB |
Output is correct |
7 |
Correct |
319 ms |
21004 KB |
Output is correct |
8 |
Correct |
1272 ms |
33544 KB |
Output is correct |
9 |
Correct |
213 ms |
23180 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
874 ms |
20980 KB |
Output is correct |
12 |
Correct |
1280 ms |
35424 KB |
Output is correct |
13 |
Correct |
839 ms |
21080 KB |
Output is correct |
14 |
Correct |
417 ms |
21012 KB |
Output is correct |
15 |
Correct |
1039 ms |
23600 KB |
Output is correct |
16 |
Correct |
261 ms |
23308 KB |
Output is correct |
17 |
Correct |
1201 ms |
29912 KB |
Output is correct |
18 |
Correct |
211 ms |
23564 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
884 ms |
21552 KB |
Output is correct |
21 |
Correct |
1266 ms |
36056 KB |
Output is correct |
22 |
Correct |
857 ms |
21596 KB |
Output is correct |
23 |
Correct |
869 ms |
21592 KB |
Output is correct |
24 |
Correct |
883 ms |
21464 KB |
Output is correct |
25 |
Correct |
858 ms |
21464 KB |
Output is correct |
26 |
Correct |
869 ms |
21468 KB |
Output is correct |
27 |
Correct |
440 ms |
21268 KB |
Output is correct |
28 |
Correct |
1054 ms |
23512 KB |
Output is correct |
29 |
Correct |
867 ms |
21448 KB |
Output is correct |
30 |
Correct |
761 ms |
21904 KB |
Output is correct |
31 |
Correct |
307 ms |
22796 KB |
Output is correct |
32 |
Correct |
1223 ms |
30684 KB |
Output is correct |
33 |
Correct |
218 ms |
24076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
881 ms |
20732 KB |
Output is correct |
14 |
Correct |
1258 ms |
32344 KB |
Output is correct |
15 |
Correct |
824 ms |
20956 KB |
Output is correct |
16 |
Correct |
438 ms |
20892 KB |
Output is correct |
17 |
Correct |
1006 ms |
23000 KB |
Output is correct |
18 |
Correct |
319 ms |
21004 KB |
Output is correct |
19 |
Correct |
1272 ms |
33544 KB |
Output is correct |
20 |
Correct |
213 ms |
23180 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
874 ms |
20980 KB |
Output is correct |
23 |
Correct |
1280 ms |
35424 KB |
Output is correct |
24 |
Correct |
839 ms |
21080 KB |
Output is correct |
25 |
Correct |
417 ms |
21012 KB |
Output is correct |
26 |
Correct |
1039 ms |
23600 KB |
Output is correct |
27 |
Correct |
261 ms |
23308 KB |
Output is correct |
28 |
Correct |
1201 ms |
29912 KB |
Output is correct |
29 |
Correct |
211 ms |
23564 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
7 ms |
512 KB |
Output is correct |
32 |
Correct |
9 ms |
640 KB |
Output is correct |
33 |
Correct |
7 ms |
512 KB |
Output is correct |
34 |
Correct |
8 ms |
640 KB |
Output is correct |
35 |
Correct |
8 ms |
512 KB |
Output is correct |
36 |
Correct |
7 ms |
512 KB |
Output is correct |
37 |
Correct |
7 ms |
512 KB |
Output is correct |
38 |
Correct |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
8 ms |
512 KB |
Output is correct |
40 |
Correct |
9 ms |
512 KB |
Output is correct |
41 |
Correct |
7 ms |
512 KB |
Output is correct |
42 |
Correct |
7 ms |
548 KB |
Output is correct |
43 |
Correct |
8 ms |
640 KB |
Output is correct |
44 |
Correct |
7 ms |
640 KB |
Output is correct |
45 |
Correct |
4 ms |
384 KB |
Output is correct |
46 |
Correct |
884 ms |
21552 KB |
Output is correct |
47 |
Correct |
1266 ms |
36056 KB |
Output is correct |
48 |
Correct |
857 ms |
21596 KB |
Output is correct |
49 |
Correct |
869 ms |
21592 KB |
Output is correct |
50 |
Correct |
883 ms |
21464 KB |
Output is correct |
51 |
Correct |
858 ms |
21464 KB |
Output is correct |
52 |
Correct |
869 ms |
21468 KB |
Output is correct |
53 |
Correct |
440 ms |
21268 KB |
Output is correct |
54 |
Correct |
1054 ms |
23512 KB |
Output is correct |
55 |
Correct |
867 ms |
21448 KB |
Output is correct |
56 |
Correct |
761 ms |
21904 KB |
Output is correct |
57 |
Correct |
307 ms |
22796 KB |
Output is correct |
58 |
Correct |
1223 ms |
30684 KB |
Output is correct |
59 |
Correct |
218 ms |
24076 KB |
Output is correct |
60 |
Correct |
4 ms |
384 KB |
Output is correct |
61 |
Correct |
929 ms |
23004 KB |
Output is correct |
62 |
Correct |
1316 ms |
33892 KB |
Output is correct |
63 |
Correct |
896 ms |
23020 KB |
Output is correct |
64 |
Correct |
883 ms |
23120 KB |
Output is correct |
65 |
Correct |
885 ms |
22876 KB |
Output is correct |
66 |
Correct |
903 ms |
22876 KB |
Output is correct |
67 |
Correct |
904 ms |
22872 KB |
Output is correct |
68 |
Correct |
486 ms |
22936 KB |
Output is correct |
69 |
Correct |
1063 ms |
24664 KB |
Output is correct |
70 |
Correct |
929 ms |
22672 KB |
Output is correct |
71 |
Correct |
757 ms |
23568 KB |
Output is correct |
72 |
Correct |
362 ms |
23944 KB |
Output is correct |
73 |
Correct |
1269 ms |
30484 KB |
Output is correct |
74 |
Correct |
258 ms |
25328 KB |
Output is correct |