#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll NMAX = 2e5 + 5;
#define all(v) v.begin(), v.end()
ll N, Q, a, b, c, d, W[NMAX], vis[NMAX], dp[NMAX], U[NMAX], D[NMAX], ans[NMAX];
vector<pair<ll, ll>> to[NMAX], v;
void dfs2(int now, int bef) {
for (auto& e : to[now]) {
int nxt = e.first;
if (nxt == bef) {
U[now] += e.second;
continue;
}
dfs2(nxt, now);
D[now] += D[nxt] + e.second;
}
return;
}
void dfs3(int now, int bef) {
for (auto& e : to[now]) {
int nxt = e.first;
if (nxt == bef) continue;
U[nxt] += U[now] + D[now] - D[nxt] - e.second;
dfs3(nxt, now);
}
return;
}
void init() {
dfs2(1, -1); dfs3(1, -1);
for (int i = 1; i <= N; i++) dp[i] = U[i] + D[i];
return;
}
int dfs(int now, int bef) {
W[now] = 1;
for (auto& e : to[now]) {
int nxt = e.first;
if (nxt == bef || vis[nxt]) continue;
W[now] += dfs(nxt, now);
}
return W[now];
}
int centroid(int now, int bef, int lim) {
for (auto& e : to[now]) {
int nxt = e.first;
if (nxt == bef || vis[nxt]) continue;
if (W[nxt] > lim) return centroid(nxt, now, lim);
}
return now;
}
ll go(int now, int bef, int r) {
ll mx = -1, tmp;
for (auto& e : to[now]) {
int nxt = e.first;
if (nxt == bef || vis[nxt]) continue;
tmp = go(nxt, now, r) + e.second;
if (mx == -1) mx = tmp;
else {
if (mx < tmp) swap(mx, tmp);
if (tmp) v.emplace_back(tmp, r);
}
}
return max(mx, 0LL);
}
void sol(int r) {
int lim = dfs(r, -1) / 2;
int ct = centroid(r, -1, lim);
vis[ct] = 1;
v.clear();
for (auto& e : to[ct]) {
int nxt = e.first;
if (vis[nxt]) continue;
v.emplace_back(go(nxt, ct, nxt) + e.second, nxt);
}
sort(all(v));
reverse(all(v));
ll x = dp[ct];
ans[1] = min(ans[1], x);
for (int i = 1; i <= v.size(); i++) {
x -= v[i - 1].first;
ans[i + 1] = min(ans[i + 1], x);
}
x = dp[ct];
ll a = b = 0, cnt;
for (int i = 1; i < v.size(); i++) {
if (v[a].second != v[i].second) {
b = i; break;
}
}
if (b) {
x -= v[a].first + v[b].first;
ans[2] = min(ans[2], x);
cnt = 2;
for (int i = 0; i < v.size(); i++) {
if (i == a || i == b) continue;
cnt++; x -= v[i].first;
ans[cnt] = min(ans[cnt], x);
}
}
for (auto& e : to[ct]) {
int nxt = e.first;
if (vis[nxt]) continue;
sol(nxt);
}
return;
}
int main(void) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 0; i < N - 1; i++) {
cin >> a >> b >> c >> d;
to[a].emplace_back(b, c);
to[b].emplace_back(a, d);
}
init();
/*for (int i = 1; i <= N; i++) cout << dp[i] << ' ';
return 0;*/
fill(ans, ans + NMAX, 1e18);
sol(1);
for (int i = 2; i <= N; i++) ans[i] = min(ans[i], ans[i - 1]);
cin >> Q;
int k;
while (Q--) {
cin >> k;
cout << ans[k] << '\n';
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void sol(int)':
designated_cities.cpp:91:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 1; i <= v.size(); i++) {
| ~~^~~~~~~~~~~
designated_cities.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
designated_cities.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Correct |
5 ms |
6604 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
5 ms |
6608 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
704 ms |
25960 KB |
Output is correct |
3 |
Correct |
1081 ms |
37728 KB |
Output is correct |
4 |
Correct |
759 ms |
26112 KB |
Output is correct |
5 |
Correct |
378 ms |
26132 KB |
Output is correct |
6 |
Correct |
928 ms |
28108 KB |
Output is correct |
7 |
Correct |
317 ms |
26124 KB |
Output is correct |
8 |
Correct |
1131 ms |
38292 KB |
Output is correct |
9 |
Correct |
220 ms |
26788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Correct |
750 ms |
26056 KB |
Output is correct |
3 |
Correct |
1058 ms |
39664 KB |
Output is correct |
4 |
Correct |
786 ms |
25964 KB |
Output is correct |
5 |
Correct |
373 ms |
26144 KB |
Output is correct |
6 |
Correct |
926 ms |
28240 KB |
Output is correct |
7 |
Correct |
249 ms |
28328 KB |
Output is correct |
8 |
Correct |
1050 ms |
34148 KB |
Output is correct |
9 |
Correct |
210 ms |
26792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Correct |
5 ms |
6604 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
5 ms |
6608 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
12 |
Correct |
4 ms |
6624 KB |
Output is correct |
13 |
Correct |
7 ms |
6732 KB |
Output is correct |
14 |
Correct |
7 ms |
6860 KB |
Output is correct |
15 |
Correct |
6 ms |
6732 KB |
Output is correct |
16 |
Correct |
7 ms |
6732 KB |
Output is correct |
17 |
Correct |
7 ms |
6732 KB |
Output is correct |
18 |
Correct |
6 ms |
6732 KB |
Output is correct |
19 |
Correct |
7 ms |
6732 KB |
Output is correct |
20 |
Correct |
6 ms |
6732 KB |
Output is correct |
21 |
Correct |
7 ms |
6736 KB |
Output is correct |
22 |
Correct |
7 ms |
6732 KB |
Output is correct |
23 |
Correct |
7 ms |
6860 KB |
Output is correct |
24 |
Correct |
6 ms |
6868 KB |
Output is correct |
25 |
Correct |
9 ms |
6936 KB |
Output is correct |
26 |
Correct |
6 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
704 ms |
25960 KB |
Output is correct |
3 |
Correct |
1081 ms |
37728 KB |
Output is correct |
4 |
Correct |
759 ms |
26112 KB |
Output is correct |
5 |
Correct |
378 ms |
26132 KB |
Output is correct |
6 |
Correct |
928 ms |
28108 KB |
Output is correct |
7 |
Correct |
317 ms |
26124 KB |
Output is correct |
8 |
Correct |
1131 ms |
38292 KB |
Output is correct |
9 |
Correct |
220 ms |
26788 KB |
Output is correct |
10 |
Correct |
4 ms |
6604 KB |
Output is correct |
11 |
Correct |
750 ms |
26056 KB |
Output is correct |
12 |
Correct |
1058 ms |
39664 KB |
Output is correct |
13 |
Correct |
786 ms |
25964 KB |
Output is correct |
14 |
Correct |
373 ms |
26144 KB |
Output is correct |
15 |
Correct |
926 ms |
28240 KB |
Output is correct |
16 |
Correct |
249 ms |
28328 KB |
Output is correct |
17 |
Correct |
1050 ms |
34148 KB |
Output is correct |
18 |
Correct |
210 ms |
26792 KB |
Output is correct |
19 |
Correct |
4 ms |
6604 KB |
Output is correct |
20 |
Correct |
760 ms |
26116 KB |
Output is correct |
21 |
Correct |
1081 ms |
39888 KB |
Output is correct |
22 |
Correct |
758 ms |
26132 KB |
Output is correct |
23 |
Correct |
784 ms |
26228 KB |
Output is correct |
24 |
Correct |
765 ms |
26212 KB |
Output is correct |
25 |
Correct |
772 ms |
26220 KB |
Output is correct |
26 |
Correct |
762 ms |
26296 KB |
Output is correct |
27 |
Correct |
392 ms |
26160 KB |
Output is correct |
28 |
Correct |
908 ms |
28024 KB |
Output is correct |
29 |
Correct |
710 ms |
26440 KB |
Output is correct |
30 |
Correct |
650 ms |
26176 KB |
Output is correct |
31 |
Correct |
286 ms |
26604 KB |
Output is correct |
32 |
Correct |
1041 ms |
34636 KB |
Output is correct |
33 |
Correct |
231 ms |
26808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6604 KB |
Output is correct |
2 |
Correct |
4 ms |
6604 KB |
Output is correct |
3 |
Correct |
4 ms |
6604 KB |
Output is correct |
4 |
Correct |
4 ms |
6604 KB |
Output is correct |
5 |
Correct |
4 ms |
6604 KB |
Output is correct |
6 |
Correct |
4 ms |
6604 KB |
Output is correct |
7 |
Correct |
4 ms |
6604 KB |
Output is correct |
8 |
Correct |
5 ms |
6604 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
5 ms |
6608 KB |
Output is correct |
11 |
Correct |
4 ms |
6604 KB |
Output is correct |
12 |
Correct |
5 ms |
6604 KB |
Output is correct |
13 |
Correct |
704 ms |
25960 KB |
Output is correct |
14 |
Correct |
1081 ms |
37728 KB |
Output is correct |
15 |
Correct |
759 ms |
26112 KB |
Output is correct |
16 |
Correct |
378 ms |
26132 KB |
Output is correct |
17 |
Correct |
928 ms |
28108 KB |
Output is correct |
18 |
Correct |
317 ms |
26124 KB |
Output is correct |
19 |
Correct |
1131 ms |
38292 KB |
Output is correct |
20 |
Correct |
220 ms |
26788 KB |
Output is correct |
21 |
Correct |
4 ms |
6604 KB |
Output is correct |
22 |
Correct |
750 ms |
26056 KB |
Output is correct |
23 |
Correct |
1058 ms |
39664 KB |
Output is correct |
24 |
Correct |
786 ms |
25964 KB |
Output is correct |
25 |
Correct |
373 ms |
26144 KB |
Output is correct |
26 |
Correct |
926 ms |
28240 KB |
Output is correct |
27 |
Correct |
249 ms |
28328 KB |
Output is correct |
28 |
Correct |
1050 ms |
34148 KB |
Output is correct |
29 |
Correct |
210 ms |
26792 KB |
Output is correct |
30 |
Correct |
4 ms |
6624 KB |
Output is correct |
31 |
Correct |
7 ms |
6732 KB |
Output is correct |
32 |
Correct |
7 ms |
6860 KB |
Output is correct |
33 |
Correct |
6 ms |
6732 KB |
Output is correct |
34 |
Correct |
7 ms |
6732 KB |
Output is correct |
35 |
Correct |
7 ms |
6732 KB |
Output is correct |
36 |
Correct |
6 ms |
6732 KB |
Output is correct |
37 |
Correct |
7 ms |
6732 KB |
Output is correct |
38 |
Correct |
6 ms |
6732 KB |
Output is correct |
39 |
Correct |
7 ms |
6736 KB |
Output is correct |
40 |
Correct |
7 ms |
6732 KB |
Output is correct |
41 |
Correct |
7 ms |
6860 KB |
Output is correct |
42 |
Correct |
6 ms |
6868 KB |
Output is correct |
43 |
Correct |
9 ms |
6936 KB |
Output is correct |
44 |
Correct |
6 ms |
6860 KB |
Output is correct |
45 |
Correct |
4 ms |
6604 KB |
Output is correct |
46 |
Correct |
760 ms |
26116 KB |
Output is correct |
47 |
Correct |
1081 ms |
39888 KB |
Output is correct |
48 |
Correct |
758 ms |
26132 KB |
Output is correct |
49 |
Correct |
784 ms |
26228 KB |
Output is correct |
50 |
Correct |
765 ms |
26212 KB |
Output is correct |
51 |
Correct |
772 ms |
26220 KB |
Output is correct |
52 |
Correct |
762 ms |
26296 KB |
Output is correct |
53 |
Correct |
392 ms |
26160 KB |
Output is correct |
54 |
Correct |
908 ms |
28024 KB |
Output is correct |
55 |
Correct |
710 ms |
26440 KB |
Output is correct |
56 |
Correct |
650 ms |
26176 KB |
Output is correct |
57 |
Correct |
286 ms |
26604 KB |
Output is correct |
58 |
Correct |
1041 ms |
34636 KB |
Output is correct |
59 |
Correct |
231 ms |
26808 KB |
Output is correct |
60 |
Correct |
4 ms |
6604 KB |
Output is correct |
61 |
Correct |
812 ms |
27412 KB |
Output is correct |
62 |
Correct |
1130 ms |
37956 KB |
Output is correct |
63 |
Correct |
761 ms |
27460 KB |
Output is correct |
64 |
Correct |
729 ms |
27696 KB |
Output is correct |
65 |
Correct |
797 ms |
27464 KB |
Output is correct |
66 |
Correct |
790 ms |
27612 KB |
Output is correct |
67 |
Correct |
785 ms |
27460 KB |
Output is correct |
68 |
Correct |
415 ms |
27692 KB |
Output is correct |
69 |
Correct |
918 ms |
29144 KB |
Output is correct |
70 |
Correct |
775 ms |
27684 KB |
Output is correct |
71 |
Correct |
664 ms |
27636 KB |
Output is correct |
72 |
Correct |
341 ms |
28264 KB |
Output is correct |
73 |
Correct |
1092 ms |
34660 KB |
Output is correct |
74 |
Correct |
248 ms |
28320 KB |
Output is correct |