#include <bits/stdc++.h>
#define N 200005
#define INF 1000000000000000000LL
using namespace std;
typedef long long ll;
bool vs[N];
struct P {
int to, cto, cfr;
};
vector<P> adj[N];
int tree_sz[N];
void sz_DFS(int c, int pr) {
tree_sz[c] = 1;
for(auto&i: adj[c]) {
if(vs[i.to] || i.to == pr) continue;
sz_DFS(i.to, c);
tree_sz[c] += tree_sz[i.to];
}
}
int find_cent(int c, int pr, int sz) {
for(auto&i: adj[c]) {
if(i.to == pr || vs[i.to] || tree_sz[i.to]*2 < sz) continue;
return find_cent(i.to, c, sz);
}
return c;
}
vector<ll> vals[N];
priority_queue<ll> pq1, pq2;
ll initVal, ans[N];
ll update_DFS(int c, int pr) {
ll sum = 0;
vals[c].clear();
for(auto&i: adj[c]) {
if(i.to == pr || vs[i.to]) continue;
sum += i.cfr;
sum += update_DFS(i.to, c);
vals[c].push_back(vals[i.to].back()+i.cfr);
}
if(vals[c].empty()) vals[c].push_back(0);
sort(vals[c].begin(),vals[c].end());
for(int i = 0; i < vals[c].size()-1; i ++) {
pq1.push(vals[c][i]);
}
if(pr == -1) {
pq1.push(vals[c].back());
if(vals[c].size() < 2) {
initVal = -INF;
pq2 = priority_queue<ll>();
} else {
initVal = vals[c].back(); vals[c].pop_back();
initVal += vals[c].back(); vals[c].pop_back();
for(ll i: vals[c]) pq2.push(i);
}
} else {
for(int i = 0; i < vals[c].size()-1; i ++) {
pq2.push(vals[c][i]);
}
}
return sum;
}
ll getSum(int c, int pr) {
ll sum = 0;
for(auto&i: adj[c]) {
if(vs[i.to] || i.to == pr) continue;
sum += i.cfr + getSum(i.to, c);
}
return sum;
}
vector<ll> updateVals(int rt, ll cum) {
ll sum = update_DFS(rt, -1);
ans[2] = min(ans[2], cum+sum-initVal);
for(int i = 3; pq2.size(); i ++, pq2.pop()) {
initVal += pq2.top();
ans[i] = min(ans[i], cum+sum-initVal);
}
ans[1] = min(ans[1], cum+sum);
initVal = 0;
for(int i = 2; pq1.size(); i ++, pq1.pop()) {
initVal += pq1.top();
ans[i] = min(ans[i],cum+sum-initVal);
}
vector<ll> cs;
for(auto&i: adj[rt]) {
if(vs[i.to]) continue;
cs.push_back(getSum(i.to,rt)+i.cfr);
}
return cs;
}
void DNC(int c, ll cum) {
sz_DFS(c, -1);
int cent = find_cent(c, -1, tree_sz[c]);
vector<ll> sms = updateVals(cent, cum);
for(ll i: sms) cum += i;
auto it = sms.begin();
vs[cent] = 1;
for(auto&i: adj[cent]) {
if(vs[i.to]) continue;
DNC(i.to, cum - (*it) + i.cto);
it ++;
}
}
int n, q;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i < n; i ++) {
int a, b, csf, cst;
cin >> a >> b >> csf >> cst;
swap(csf, cst);
adj[a].push_back({b, csf, cst});
adj[b].push_back({a, cst, csf});
}
for(auto&i: ans) i = INF;
DNC(1, 0);
for(int i = 2; i <= n; i ++) {
ans[i] = min(ans[i], ans[i-1]);
}
cin >> q;
while(q--) {
int i;
cin >> i;
cout << ans[i] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'll update_DFS(int, int)':
designated_cities.cpp:41:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i = 0; i < vals[c].size()-1; i ++) {
| ~~^~~~~~~~~~~~~~~~~~
designated_cities.cpp:55:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < vals[c].size()-1; i ++) {
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12120 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12120 KB |
Output is correct |
2 |
Correct |
593 ms |
28992 KB |
Output is correct |
3 |
Correct |
702 ms |
35852 KB |
Output is correct |
4 |
Correct |
580 ms |
29228 KB |
Output is correct |
5 |
Correct |
260 ms |
29256 KB |
Output is correct |
6 |
Correct |
695 ms |
30400 KB |
Output is correct |
7 |
Correct |
249 ms |
31236 KB |
Output is correct |
8 |
Correct |
762 ms |
36000 KB |
Output is correct |
9 |
Correct |
194 ms |
35320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12120 KB |
Output is correct |
2 |
Correct |
650 ms |
35380 KB |
Output is correct |
3 |
Correct |
671 ms |
43344 KB |
Output is correct |
4 |
Correct |
526 ms |
33996 KB |
Output is correct |
5 |
Correct |
249 ms |
35572 KB |
Output is correct |
6 |
Correct |
725 ms |
37048 KB |
Output is correct |
7 |
Correct |
188 ms |
40700 KB |
Output is correct |
8 |
Correct |
831 ms |
40896 KB |
Output is correct |
9 |
Correct |
186 ms |
41476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12120 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
6 ms |
12380 KB |
Output is correct |
14 |
Correct |
5 ms |
12380 KB |
Output is correct |
15 |
Correct |
5 ms |
12380 KB |
Output is correct |
16 |
Correct |
5 ms |
12380 KB |
Output is correct |
17 |
Correct |
5 ms |
12380 KB |
Output is correct |
18 |
Correct |
6 ms |
12380 KB |
Output is correct |
19 |
Correct |
5 ms |
12380 KB |
Output is correct |
20 |
Correct |
5 ms |
12376 KB |
Output is correct |
21 |
Correct |
6 ms |
12300 KB |
Output is correct |
22 |
Correct |
11 ms |
12260 KB |
Output is correct |
23 |
Correct |
6 ms |
12452 KB |
Output is correct |
24 |
Correct |
4 ms |
12300 KB |
Output is correct |
25 |
Correct |
6 ms |
12380 KB |
Output is correct |
26 |
Correct |
4 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12120 KB |
Output is correct |
2 |
Correct |
593 ms |
28992 KB |
Output is correct |
3 |
Correct |
702 ms |
35852 KB |
Output is correct |
4 |
Correct |
580 ms |
29228 KB |
Output is correct |
5 |
Correct |
260 ms |
29256 KB |
Output is correct |
6 |
Correct |
695 ms |
30400 KB |
Output is correct |
7 |
Correct |
249 ms |
31236 KB |
Output is correct |
8 |
Correct |
762 ms |
36000 KB |
Output is correct |
9 |
Correct |
194 ms |
35320 KB |
Output is correct |
10 |
Correct |
3 ms |
12120 KB |
Output is correct |
11 |
Correct |
650 ms |
35380 KB |
Output is correct |
12 |
Correct |
671 ms |
43344 KB |
Output is correct |
13 |
Correct |
526 ms |
33996 KB |
Output is correct |
14 |
Correct |
249 ms |
35572 KB |
Output is correct |
15 |
Correct |
725 ms |
37048 KB |
Output is correct |
16 |
Correct |
188 ms |
40700 KB |
Output is correct |
17 |
Correct |
831 ms |
40896 KB |
Output is correct |
18 |
Correct |
186 ms |
41476 KB |
Output is correct |
19 |
Correct |
3 ms |
12120 KB |
Output is correct |
20 |
Correct |
572 ms |
35212 KB |
Output is correct |
21 |
Correct |
706 ms |
43308 KB |
Output is correct |
22 |
Correct |
633 ms |
34384 KB |
Output is correct |
23 |
Correct |
610 ms |
35432 KB |
Output is correct |
24 |
Correct |
687 ms |
34112 KB |
Output is correct |
25 |
Correct |
590 ms |
35112 KB |
Output is correct |
26 |
Correct |
556 ms |
34220 KB |
Output is correct |
27 |
Correct |
264 ms |
35228 KB |
Output is correct |
28 |
Correct |
707 ms |
36840 KB |
Output is correct |
29 |
Correct |
538 ms |
34824 KB |
Output is correct |
30 |
Correct |
556 ms |
35232 KB |
Output is correct |
31 |
Correct |
190 ms |
38256 KB |
Output is correct |
32 |
Correct |
871 ms |
41240 KB |
Output is correct |
33 |
Correct |
188 ms |
42496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12124 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12124 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
12120 KB |
Output is correct |
9 |
Correct |
3 ms |
12124 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
3 ms |
12120 KB |
Output is correct |
13 |
Correct |
593 ms |
28992 KB |
Output is correct |
14 |
Correct |
702 ms |
35852 KB |
Output is correct |
15 |
Correct |
580 ms |
29228 KB |
Output is correct |
16 |
Correct |
260 ms |
29256 KB |
Output is correct |
17 |
Correct |
695 ms |
30400 KB |
Output is correct |
18 |
Correct |
249 ms |
31236 KB |
Output is correct |
19 |
Correct |
762 ms |
36000 KB |
Output is correct |
20 |
Correct |
194 ms |
35320 KB |
Output is correct |
21 |
Correct |
3 ms |
12120 KB |
Output is correct |
22 |
Correct |
650 ms |
35380 KB |
Output is correct |
23 |
Correct |
671 ms |
43344 KB |
Output is correct |
24 |
Correct |
526 ms |
33996 KB |
Output is correct |
25 |
Correct |
249 ms |
35572 KB |
Output is correct |
26 |
Correct |
725 ms |
37048 KB |
Output is correct |
27 |
Correct |
188 ms |
40700 KB |
Output is correct |
28 |
Correct |
831 ms |
40896 KB |
Output is correct |
29 |
Correct |
186 ms |
41476 KB |
Output is correct |
30 |
Correct |
3 ms |
12120 KB |
Output is correct |
31 |
Correct |
6 ms |
12380 KB |
Output is correct |
32 |
Correct |
5 ms |
12380 KB |
Output is correct |
33 |
Correct |
5 ms |
12380 KB |
Output is correct |
34 |
Correct |
5 ms |
12380 KB |
Output is correct |
35 |
Correct |
5 ms |
12380 KB |
Output is correct |
36 |
Correct |
6 ms |
12380 KB |
Output is correct |
37 |
Correct |
5 ms |
12380 KB |
Output is correct |
38 |
Correct |
5 ms |
12376 KB |
Output is correct |
39 |
Correct |
6 ms |
12300 KB |
Output is correct |
40 |
Correct |
11 ms |
12260 KB |
Output is correct |
41 |
Correct |
6 ms |
12452 KB |
Output is correct |
42 |
Correct |
4 ms |
12300 KB |
Output is correct |
43 |
Correct |
6 ms |
12380 KB |
Output is correct |
44 |
Correct |
4 ms |
12380 KB |
Output is correct |
45 |
Correct |
3 ms |
12120 KB |
Output is correct |
46 |
Correct |
572 ms |
35212 KB |
Output is correct |
47 |
Correct |
706 ms |
43308 KB |
Output is correct |
48 |
Correct |
633 ms |
34384 KB |
Output is correct |
49 |
Correct |
610 ms |
35432 KB |
Output is correct |
50 |
Correct |
687 ms |
34112 KB |
Output is correct |
51 |
Correct |
590 ms |
35112 KB |
Output is correct |
52 |
Correct |
556 ms |
34220 KB |
Output is correct |
53 |
Correct |
264 ms |
35228 KB |
Output is correct |
54 |
Correct |
707 ms |
36840 KB |
Output is correct |
55 |
Correct |
538 ms |
34824 KB |
Output is correct |
56 |
Correct |
556 ms |
35232 KB |
Output is correct |
57 |
Correct |
190 ms |
38256 KB |
Output is correct |
58 |
Correct |
871 ms |
41240 KB |
Output is correct |
59 |
Correct |
188 ms |
42496 KB |
Output is correct |
60 |
Correct |
3 ms |
12124 KB |
Output is correct |
61 |
Correct |
606 ms |
37872 KB |
Output is correct |
62 |
Correct |
729 ms |
43660 KB |
Output is correct |
63 |
Correct |
654 ms |
36792 KB |
Output is correct |
64 |
Correct |
653 ms |
37804 KB |
Output is correct |
65 |
Correct |
682 ms |
36800 KB |
Output is correct |
66 |
Correct |
642 ms |
37880 KB |
Output is correct |
67 |
Correct |
620 ms |
36816 KB |
Output is correct |
68 |
Correct |
316 ms |
37900 KB |
Output is correct |
69 |
Correct |
863 ms |
39472 KB |
Output is correct |
70 |
Correct |
693 ms |
37828 KB |
Output is correct |
71 |
Correct |
613 ms |
37284 KB |
Output is correct |
72 |
Correct |
292 ms |
40468 KB |
Output is correct |
73 |
Correct |
1040 ms |
43572 KB |
Output is correct |
74 |
Correct |
226 ms |
42984 KB |
Output is correct |