#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<pair<int,ll>> v; v.clear();
for(auto x : adj[s]){
int u = x.fi; ll w = x.se;
if(u!=p) dfs(u,s), v.pb({u,w});
}
sort(all(v), [&](pair<int,ll> a, pair<int,ll>b){
ll x = dp[a.fi][0]-dp[a.fi][1]-a.se;
ll y = dp[b.fi][0]-dp[b.fi][1]-b.se;
return x<y;
});
int x = 0;
for(auto y : v){
int u = y.fi; ll w = y.se;
if(x<k) dp[s][0]+=max(dp[u][0],dp[u][1]+w);
else dp[s][0]+=dp[u][0];
if(x<k-1) dp[s][1]+=max(dp[u][0],dp[u][1]+w);
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;
}
# |
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 |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
27 ms |
2224 KB |
Output is correct |
13 |
Correct |
49 ms |
3456 KB |
Output is correct |
14 |
Correct |
41 ms |
3300 KB |
Output is correct |
15 |
Correct |
45 ms |
3688 KB |
Output is correct |
16 |
Correct |
46 ms |
3680 KB |
Output is correct |
17 |
Correct |
48 ms |
3796 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
33 ms |
3096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
29 ms |
4180 KB |
Output is correct |
3 |
Correct |
34 ms |
4668 KB |
Output is correct |
4 |
Correct |
38 ms |
5000 KB |
Output is correct |
5 |
Correct |
36 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 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 |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
19 ms |
3116 KB |
Output is correct |
13 |
Correct |
31 ms |
4988 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
31 ms |
4564 KB |
Output is correct |
16 |
Correct |
32 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
25 ms |
6624 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
25 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 |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
27 ms |
2224 KB |
Output is correct |
13 |
Correct |
49 ms |
3456 KB |
Output is correct |
14 |
Correct |
41 ms |
3300 KB |
Output is correct |
15 |
Correct |
45 ms |
3688 KB |
Output is correct |
16 |
Correct |
46 ms |
3680 KB |
Output is correct |
17 |
Correct |
48 ms |
3796 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
33 ms |
3096 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
29 ms |
4180 KB |
Output is correct |
22 |
Correct |
34 ms |
4668 KB |
Output is correct |
23 |
Correct |
38 ms |
5000 KB |
Output is correct |
24 |
Correct |
36 ms |
4940 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
352 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
19 ms |
3116 KB |
Output is correct |
32 |
Correct |
31 ms |
4988 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
31 ms |
4564 KB |
Output is correct |
35 |
Correct |
32 ms |
4992 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |