#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
using ll = long long;
const int mxN = (int)2e3+10;
const ll LINF = (ll)1e18;
int n, k;
ll dp[mxN][2];
vector<pair<int,ll>> adj[mxN];
void dfs(int s, int p){
vector<int> v; v.clear();
for(auto x : adj[s]){
int u = x.fi; ll w = x.se;
if(u!=p) dfs(u,s), dp[u][0]+=w, v.pb(u);
}
sort(all(v), [&](int a, int b){
return dp[a][1]-dp[a][0]<dp[b][1]-dp[b][0];
});
for(int x = 0; auto u : v){
if(x<k) dp[s][0]+=min(dp[u][0],dp[u][1]);
else dp[s][0]+=dp[u][0];
if(x<k-1) dp[s][1]+=min(dp[u][0],dp[u][1]);
else dp[s][1]+=dp[u][0];
x++;
}
}
vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
vector<ll> ans(N,0ll);
ans[0] = accumulate(all(W),0ll);
if(accumulate(all(U),0ll)==0ll){
sort(all(W)); reverse(all(W)); int i = 0;
ll tot = accumulate(all(W),0ll);
for(auto &u : ans) u=tot,tot-=W[i++];
return ans;
}
bool isLine = 1;
for(int i = 0; i < N-1; i++)
isLine &= (U[i]==i and V[i]==i+1);
if(isLine){
ll dp[N-1][2];
dp[0][0] = dp[0][1] = 0;
for(int i = 1; i < N; i++){
dp[i][0] = min(dp[i-1][1],dp[i-1][0])+W[i-1];
dp[i][1] = dp[i-1][0];
}
ans[1] = min(dp[N-1][0],dp[N-1][1]);
return ans;
}
for(int i = 0; i < N-1; i++){
adj[U[i]].pb({V[i],W[i]});
adj[V[i]].pb({U[i],W[i]});
}
for(k = 1; k < n; k++)
memset(dp,0,sizeof(dp)), dfs(0,-1), ans[k]=dp[0][0];
return ans;
}
Compilation message
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:27:20: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
27 | for(int x = 0; auto u : v){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
21 ms |
2132 KB |
Output is correct |
13 |
Correct |
34 ms |
3464 KB |
Output is correct |
14 |
Correct |
41 ms |
3388 KB |
Output is correct |
15 |
Correct |
45 ms |
3696 KB |
Output is correct |
16 |
Correct |
45 ms |
3688 KB |
Output is correct |
17 |
Correct |
39 ms |
3748 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
31 ms |
3092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
32 ms |
4168 KB |
Output is correct |
3 |
Correct |
38 ms |
4660 KB |
Output is correct |
4 |
Correct |
51 ms |
4988 KB |
Output is correct |
5 |
Correct |
39 ms |
5036 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
21 ms |
3156 KB |
Output is correct |
13 |
Correct |
34 ms |
5000 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
31 ms |
4480 KB |
Output is correct |
16 |
Correct |
33 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
356 KB |
Output is correct |
7 |
Correct |
2 ms |
356 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
356 KB |
Output is correct |
17 |
Correct |
3 ms |
356 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
356 KB |
Output is correct |
7 |
Correct |
2 ms |
356 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
356 KB |
Output is correct |
17 |
Correct |
3 ms |
356 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
356 KB |
Output is correct |
23 |
Correct |
107 ms |
492 KB |
Output is correct |
24 |
Correct |
238 ms |
580 KB |
Output is correct |
25 |
Correct |
177 ms |
532 KB |
Output is correct |
26 |
Correct |
179 ms |
548 KB |
Output is correct |
27 |
Correct |
190 ms |
604 KB |
Output is correct |
28 |
Correct |
93 ms |
568 KB |
Output is correct |
29 |
Correct |
208 ms |
468 KB |
Output is correct |
30 |
Correct |
149 ms |
568 KB |
Output is correct |
31 |
Correct |
154 ms |
568 KB |
Output is correct |
32 |
Correct |
94 ms |
564 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
276 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
360 KB |
Output is correct |
42 |
Correct |
3 ms |
340 KB |
Output is correct |
43 |
Correct |
3 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
340 KB |
Output is correct |
45 |
Correct |
3 ms |
340 KB |
Output is correct |
46 |
Correct |
3 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
356 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
143 ms |
516 KB |
Output is correct |
54 |
Correct |
247 ms |
644 KB |
Output is correct |
55 |
Correct |
135 ms |
576 KB |
Output is correct |
56 |
Correct |
1 ms |
368 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
5 ms |
340 KB |
Output is correct |
60 |
Correct |
3 ms |
340 KB |
Output is correct |
61 |
Correct |
0 ms |
340 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
229 ms |
588 KB |
Output is correct |
65 |
Correct |
235 ms |
496 KB |
Output is correct |
66 |
Correct |
99 ms |
556 KB |
Output is correct |
67 |
Correct |
94 ms |
548 KB |
Output is correct |
68 |
Correct |
102 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
6624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
27 ms |
6624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
21 ms |
2132 KB |
Output is correct |
13 |
Correct |
34 ms |
3464 KB |
Output is correct |
14 |
Correct |
41 ms |
3388 KB |
Output is correct |
15 |
Correct |
45 ms |
3696 KB |
Output is correct |
16 |
Correct |
45 ms |
3688 KB |
Output is correct |
17 |
Correct |
39 ms |
3748 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
31 ms |
3092 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
32 ms |
4168 KB |
Output is correct |
22 |
Correct |
38 ms |
4660 KB |
Output is correct |
23 |
Correct |
51 ms |
4988 KB |
Output is correct |
24 |
Correct |
39 ms |
5036 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
21 ms |
3156 KB |
Output is correct |
32 |
Correct |
34 ms |
5000 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
31 ms |
4480 KB |
Output is correct |
35 |
Correct |
33 ms |
4948 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
356 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
340 KB |
Output is correct |
41 |
Correct |
3 ms |
356 KB |
Output is correct |
42 |
Correct |
2 ms |
356 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
356 KB |
Output is correct |
52 |
Correct |
3 ms |
356 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
0 ms |
356 KB |
Output is correct |
58 |
Correct |
107 ms |
492 KB |
Output is correct |
59 |
Correct |
238 ms |
580 KB |
Output is correct |
60 |
Correct |
177 ms |
532 KB |
Output is correct |
61 |
Correct |
179 ms |
548 KB |
Output is correct |
62 |
Correct |
190 ms |
604 KB |
Output is correct |
63 |
Correct |
93 ms |
568 KB |
Output is correct |
64 |
Correct |
208 ms |
468 KB |
Output is correct |
65 |
Correct |
149 ms |
568 KB |
Output is correct |
66 |
Correct |
154 ms |
568 KB |
Output is correct |
67 |
Correct |
94 ms |
564 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
276 KB |
Output is correct |
75 |
Correct |
0 ms |
340 KB |
Output is correct |
76 |
Correct |
2 ms |
360 KB |
Output is correct |
77 |
Correct |
3 ms |
340 KB |
Output is correct |
78 |
Correct |
3 ms |
340 KB |
Output is correct |
79 |
Correct |
3 ms |
340 KB |
Output is correct |
80 |
Correct |
3 ms |
340 KB |
Output is correct |
81 |
Correct |
3 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
356 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
1 ms |
340 KB |
Output is correct |
87 |
Correct |
1 ms |
340 KB |
Output is correct |
88 |
Correct |
143 ms |
516 KB |
Output is correct |
89 |
Correct |
247 ms |
644 KB |
Output is correct |
90 |
Correct |
135 ms |
576 KB |
Output is correct |
91 |
Correct |
1 ms |
368 KB |
Output is correct |
92 |
Correct |
1 ms |
340 KB |
Output is correct |
93 |
Correct |
1 ms |
340 KB |
Output is correct |
94 |
Correct |
5 ms |
340 KB |
Output is correct |
95 |
Correct |
3 ms |
340 KB |
Output is correct |
96 |
Correct |
0 ms |
340 KB |
Output is correct |
97 |
Correct |
0 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
340 KB |
Output is correct |
99 |
Correct |
229 ms |
588 KB |
Output is correct |
100 |
Correct |
235 ms |
496 KB |
Output is correct |
101 |
Correct |
99 ms |
556 KB |
Output is correct |
102 |
Correct |
94 ms |
548 KB |
Output is correct |
103 |
Correct |
102 ms |
496 KB |
Output is correct |
104 |
Runtime error |
27 ms |
6624 KB |
Execution killed with signal 11 |
105 |
Halted |
0 ms |
0 KB |
- |