#include <bits/stdc++.h>
#define int int64_t
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ar = array<int, 2>;
const int64_t INF = 1ll << 60;
const int N = 2e5 + 5;
vector<ar> adj[N];
int dp, mx[N], up[N], ans[N], n, q;
void dfs_dp(int v, int p) {
for (auto [u, w]: adj[v]) {
dp += (u == p ? w : 0);
}
for (auto [u, w]: adj[v]) if (u != p) {
mx[u] = mx[v] + w;
dfs_dp(u, v);
}
}
void dfs_up(int v, int p) {
for (auto [u, w]: adj[v]) up[v] -= (u == p ? w : 0);
for (auto [u, w]: adj[v]) if (u != p) {
up[u] = up[v] + w;
dfs_up(u, v);
}
}
vector<int> V;
int dfs(int v, int p) {
if (adj[v].size() == 1 && v != p) return 0;
vector<int> ch;
for (auto [u, w]: adj[v]) if (u != p) {
int x = dfs(u, v);
ch.push_back(x + w);
}
sort(ch.begin(), ch.end());
int u = ch.back();
ch.pop_back();
V.insert(V.end(), ch.begin(), ch.end());
return u;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int sum = 0;
cin >> n;
FOR(i, 1, n) {
int u, v, c, d;
cin >> u >> v >> c >> d;
--u, --v;
adj[u].push_back({v, c});
adj[v].push_back({u, d});
sum += c + d;
}
dfs_dp(0, 0);
up[0] = dp;
dfs_up(0, 0);
int rt = max_element(mx, mx + n) - mx;
ans[1] = *max_element(up, up + n);
V.push_back(dfs(rt, rt));
sort(V.rbegin(), V.rend());
int kbig = 0;
FOR(i, 2, n + 1) {
kbig += (i - 2 < V.size() ? V[i - 2] : 0);
ans[i] = up[rt] + kbig;
}
cin >> q;
FOR(i, 0, q) {
int x;
cin >> x;
cout << sum - ans[x] << '\n';
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:74:32: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | kbig += (i - 2 < V.size() ? V[i - 2] : 0);
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7776 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
195 ms |
27172 KB |
Output is correct |
3 |
Correct |
238 ms |
60908 KB |
Output is correct |
4 |
Correct |
141 ms |
25700 KB |
Output is correct |
5 |
Correct |
161 ms |
28296 KB |
Output is correct |
6 |
Correct |
172 ms |
31968 KB |
Output is correct |
7 |
Correct |
130 ms |
28596 KB |
Output is correct |
8 |
Correct |
273 ms |
59352 KB |
Output is correct |
9 |
Correct |
108 ms |
32436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
162 ms |
27252 KB |
Output is correct |
3 |
Correct |
252 ms |
60872 KB |
Output is correct |
4 |
Correct |
153 ms |
25688 KB |
Output is correct |
5 |
Correct |
191 ms |
28360 KB |
Output is correct |
6 |
Correct |
178 ms |
32820 KB |
Output is correct |
7 |
Correct |
123 ms |
32696 KB |
Output is correct |
8 |
Correct |
282 ms |
48884 KB |
Output is correct |
9 |
Correct |
111 ms |
30504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7776 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7772 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
3 ms |
7720 KB |
Output is correct |
16 |
Correct |
3 ms |
7772 KB |
Output is correct |
17 |
Correct |
3 ms |
7772 KB |
Output is correct |
18 |
Correct |
3 ms |
7772 KB |
Output is correct |
19 |
Correct |
3 ms |
7772 KB |
Output is correct |
20 |
Correct |
3 ms |
7772 KB |
Output is correct |
21 |
Correct |
3 ms |
7772 KB |
Output is correct |
22 |
Correct |
3 ms |
7772 KB |
Output is correct |
23 |
Correct |
3 ms |
7772 KB |
Output is correct |
24 |
Correct |
3 ms |
7772 KB |
Output is correct |
25 |
Correct |
4 ms |
8028 KB |
Output is correct |
26 |
Correct |
3 ms |
7772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
195 ms |
27172 KB |
Output is correct |
3 |
Correct |
238 ms |
60908 KB |
Output is correct |
4 |
Correct |
141 ms |
25700 KB |
Output is correct |
5 |
Correct |
161 ms |
28296 KB |
Output is correct |
6 |
Correct |
172 ms |
31968 KB |
Output is correct |
7 |
Correct |
130 ms |
28596 KB |
Output is correct |
8 |
Correct |
273 ms |
59352 KB |
Output is correct |
9 |
Correct |
108 ms |
32436 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
162 ms |
27252 KB |
Output is correct |
12 |
Correct |
252 ms |
60872 KB |
Output is correct |
13 |
Correct |
153 ms |
25688 KB |
Output is correct |
14 |
Correct |
191 ms |
28360 KB |
Output is correct |
15 |
Correct |
178 ms |
32820 KB |
Output is correct |
16 |
Correct |
123 ms |
32696 KB |
Output is correct |
17 |
Correct |
282 ms |
48884 KB |
Output is correct |
18 |
Correct |
111 ms |
30504 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
147 ms |
27384 KB |
Output is correct |
21 |
Correct |
230 ms |
61076 KB |
Output is correct |
22 |
Correct |
137 ms |
25940 KB |
Output is correct |
23 |
Correct |
156 ms |
27488 KB |
Output is correct |
24 |
Correct |
191 ms |
26520 KB |
Output is correct |
25 |
Correct |
150 ms |
27464 KB |
Output is correct |
26 |
Correct |
145 ms |
26540 KB |
Output is correct |
27 |
Correct |
147 ms |
27676 KB |
Output is correct |
28 |
Correct |
191 ms |
32500 KB |
Output is correct |
29 |
Correct |
177 ms |
28100 KB |
Output is correct |
30 |
Correct |
154 ms |
27080 KB |
Output is correct |
31 |
Correct |
156 ms |
28996 KB |
Output is correct |
32 |
Correct |
210 ms |
49964 KB |
Output is correct |
33 |
Correct |
112 ms |
31832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7776 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
195 ms |
27172 KB |
Output is correct |
14 |
Correct |
238 ms |
60908 KB |
Output is correct |
15 |
Correct |
141 ms |
25700 KB |
Output is correct |
16 |
Correct |
161 ms |
28296 KB |
Output is correct |
17 |
Correct |
172 ms |
31968 KB |
Output is correct |
18 |
Correct |
130 ms |
28596 KB |
Output is correct |
19 |
Correct |
273 ms |
59352 KB |
Output is correct |
20 |
Correct |
108 ms |
32436 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
162 ms |
27252 KB |
Output is correct |
23 |
Correct |
252 ms |
60872 KB |
Output is correct |
24 |
Correct |
153 ms |
25688 KB |
Output is correct |
25 |
Correct |
191 ms |
28360 KB |
Output is correct |
26 |
Correct |
178 ms |
32820 KB |
Output is correct |
27 |
Correct |
123 ms |
32696 KB |
Output is correct |
28 |
Correct |
282 ms |
48884 KB |
Output is correct |
29 |
Correct |
111 ms |
30504 KB |
Output is correct |
30 |
Correct |
2 ms |
7516 KB |
Output is correct |
31 |
Correct |
3 ms |
7772 KB |
Output is correct |
32 |
Correct |
3 ms |
8028 KB |
Output is correct |
33 |
Correct |
3 ms |
7720 KB |
Output is correct |
34 |
Correct |
3 ms |
7772 KB |
Output is correct |
35 |
Correct |
3 ms |
7772 KB |
Output is correct |
36 |
Correct |
3 ms |
7772 KB |
Output is correct |
37 |
Correct |
3 ms |
7772 KB |
Output is correct |
38 |
Correct |
3 ms |
7772 KB |
Output is correct |
39 |
Correct |
3 ms |
7772 KB |
Output is correct |
40 |
Correct |
3 ms |
7772 KB |
Output is correct |
41 |
Correct |
3 ms |
7772 KB |
Output is correct |
42 |
Correct |
3 ms |
7772 KB |
Output is correct |
43 |
Correct |
4 ms |
8028 KB |
Output is correct |
44 |
Correct |
3 ms |
7772 KB |
Output is correct |
45 |
Correct |
2 ms |
7516 KB |
Output is correct |
46 |
Correct |
147 ms |
27384 KB |
Output is correct |
47 |
Correct |
230 ms |
61076 KB |
Output is correct |
48 |
Correct |
137 ms |
25940 KB |
Output is correct |
49 |
Correct |
156 ms |
27488 KB |
Output is correct |
50 |
Correct |
191 ms |
26520 KB |
Output is correct |
51 |
Correct |
150 ms |
27464 KB |
Output is correct |
52 |
Correct |
145 ms |
26540 KB |
Output is correct |
53 |
Correct |
147 ms |
27676 KB |
Output is correct |
54 |
Correct |
191 ms |
32500 KB |
Output is correct |
55 |
Correct |
177 ms |
28100 KB |
Output is correct |
56 |
Correct |
154 ms |
27080 KB |
Output is correct |
57 |
Correct |
156 ms |
28996 KB |
Output is correct |
58 |
Correct |
210 ms |
49964 KB |
Output is correct |
59 |
Correct |
112 ms |
31832 KB |
Output is correct |
60 |
Correct |
2 ms |
7516 KB |
Output is correct |
61 |
Correct |
179 ms |
29752 KB |
Output is correct |
62 |
Correct |
251 ms |
62488 KB |
Output is correct |
63 |
Correct |
183 ms |
28664 KB |
Output is correct |
64 |
Correct |
182 ms |
30136 KB |
Output is correct |
65 |
Correct |
172 ms |
28660 KB |
Output is correct |
66 |
Correct |
206 ms |
30296 KB |
Output is correct |
67 |
Correct |
312 ms |
28668 KB |
Output is correct |
68 |
Correct |
174 ms |
30324 KB |
Output is correct |
69 |
Correct |
227 ms |
34548 KB |
Output is correct |
70 |
Correct |
203 ms |
30524 KB |
Output is correct |
71 |
Correct |
178 ms |
28964 KB |
Output is correct |
72 |
Correct |
161 ms |
30128 KB |
Output is correct |
73 |
Correct |
269 ms |
51964 KB |
Output is correct |
74 |
Correct |
132 ms |
32088 KB |
Output is correct |