#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
vector<array<long long, 2> > g[MAXN];
long long n, k;
vector<array<long long, 2> > dp(MAXN);
// dp[node][0] -- parent weight not selected
// dp[node][1] -- parent weight selected
void dfs(long long node, long long par, long long par_wt)
{
// if(k == 1) cout << "node = " << node << ", par = " << par << ", par_wt = " << par_wt << "\n";
if(g[node].size() == 1 && par != -1) // base case
{
dp[node][0] = 0;
dp[node][1] = par_wt;
return;
}
long long sum = 0;
vector<long long> diff;
for(auto [to, wt]: g[node])
{
if(to != par)
{
dfs(to, node, wt);
sum += dp[to][0];
diff.push_back(dp[to][1] - dp[to][0]);
}
}
sort(diff.begin(), diff.end());
reverse(diff.begin(), diff.end());
long long gg = diff.size();
for(long long i = diff.size() - 1; i >= 0; i--)
{
if(diff[i] < 0)
{
gg = i;
}
}
// if(k == 1) cout << "node = " << node << ", diff.size() = " << diff.size() << "\n";
long long children_siz = g[node].size() - 1;
// no. of children + parent <= k
if(children_siz + 1 <= k)
{
for(long long i = diff.size() - 1; i >= gg; i--)
{
sum += diff[i];
}
dp[node][0] = sum;
dp[node][1] = sum + par_wt;
return;
}
// if(k == 1) cout << "node has reached here\n";
// keeping edges between k - 1 children and parent
long long buffer_value = 0;
for(long long i = diff.size() - 1; i >= min(gg, k - 1); i--)
{
buffer_value += diff[i];
}
dp[node][0] = sum + buffer_value;
// keeping edges between k children only
buffer_value = par_wt;
for(long long i = diff.size() - 1; i >= min(k, gg); i--)
{
buffer_value += diff[i];
}
dp[node][1] = sum + buffer_value;
}
long long calculatedp()
{
for(long long i = 0; i <= n; i++)
{
dp[i][0] = 0;
dp[i][1] = 0;
}
dfs(0, -1, 0);
if(k == 1)
{
// cout << "dp values\n";
// for(int i = 0; i < n; i++)
// {
// cout << dp[i][0] << " " << dp[i][1] << "\n";
// }
}
return min(dp[0][0], dp[0][1]);
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W)
{
n = N;
long long sum = 0;
for(long long i = 0; i < U.size(); i++)
{
sum += W[i];
// cout << "U[i] = " << U[i] << ", V[i] = " << V[i] << ", W[i] = " << W[i] << "\n";
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
}
vector<long long> ans;
ans.push_back(sum);
for(long long i = 1; i < N; i++)
{
k = i;
ans.push_back(calculatedp());
}
return ans;
}
Compilation message
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:95:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(long long i = 0; i < U.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
152 ms |
4392 KB |
Output is correct |
3 |
Correct |
186 ms |
4404 KB |
Output is correct |
4 |
Correct |
71 ms |
4404 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
68 ms |
4376 KB |
Output is correct |
9 |
Correct |
111 ms |
4396 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Execution timed out |
2078 ms |
9544 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Execution timed out |
2093 ms |
21768 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
4 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4180 KB |
Output is correct |
6 |
Correct |
4 ms |
4132 KB |
Output is correct |
7 |
Correct |
5 ms |
4180 KB |
Output is correct |
8 |
Correct |
5 ms |
4180 KB |
Output is correct |
9 |
Correct |
6 ms |
4180 KB |
Output is correct |
10 |
Correct |
5 ms |
4180 KB |
Output is correct |
11 |
Correct |
4 ms |
4236 KB |
Output is correct |
12 |
Correct |
4 ms |
4180 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
4 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
5 ms |
4176 KB |
Output is correct |
18 |
Correct |
5 ms |
4180 KB |
Output is correct |
19 |
Correct |
3 ms |
4232 KB |
Output is correct |
20 |
Correct |
3 ms |
4180 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
4 ms |
4180 KB |
Output is correct |
5 |
Correct |
4 ms |
4180 KB |
Output is correct |
6 |
Correct |
4 ms |
4132 KB |
Output is correct |
7 |
Correct |
5 ms |
4180 KB |
Output is correct |
8 |
Correct |
5 ms |
4180 KB |
Output is correct |
9 |
Correct |
6 ms |
4180 KB |
Output is correct |
10 |
Correct |
5 ms |
4180 KB |
Output is correct |
11 |
Correct |
4 ms |
4236 KB |
Output is correct |
12 |
Correct |
4 ms |
4180 KB |
Output is correct |
13 |
Correct |
3 ms |
4180 KB |
Output is correct |
14 |
Correct |
4 ms |
4180 KB |
Output is correct |
15 |
Correct |
3 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
5 ms |
4176 KB |
Output is correct |
18 |
Correct |
5 ms |
4180 KB |
Output is correct |
19 |
Correct |
3 ms |
4232 KB |
Output is correct |
20 |
Correct |
3 ms |
4180 KB |
Output is correct |
21 |
Correct |
3 ms |
4180 KB |
Output is correct |
22 |
Correct |
3 ms |
4180 KB |
Output is correct |
23 |
Correct |
101 ms |
4356 KB |
Output is correct |
24 |
Correct |
259 ms |
4448 KB |
Output is correct |
25 |
Correct |
174 ms |
4384 KB |
Output is correct |
26 |
Correct |
170 ms |
4404 KB |
Output is correct |
27 |
Correct |
138 ms |
4448 KB |
Output is correct |
28 |
Correct |
74 ms |
4544 KB |
Output is correct |
29 |
Correct |
205 ms |
4396 KB |
Output is correct |
30 |
Correct |
125 ms |
4424 KB |
Output is correct |
31 |
Correct |
116 ms |
4412 KB |
Output is correct |
32 |
Correct |
70 ms |
4436 KB |
Output is correct |
33 |
Correct |
111 ms |
4564 KB |
Output is correct |
34 |
Correct |
154 ms |
4632 KB |
Output is correct |
35 |
Correct |
127 ms |
4644 KB |
Output is correct |
36 |
Correct |
151 ms |
4420 KB |
Output is correct |
37 |
Correct |
171 ms |
4436 KB |
Output is correct |
38 |
Correct |
71 ms |
4436 KB |
Output is correct |
39 |
Correct |
2 ms |
4180 KB |
Output is correct |
40 |
Correct |
3 ms |
4132 KB |
Output is correct |
41 |
Correct |
3 ms |
4180 KB |
Output is correct |
42 |
Correct |
5 ms |
4180 KB |
Output is correct |
43 |
Correct |
3 ms |
4236 KB |
Output is correct |
44 |
Correct |
4 ms |
4180 KB |
Output is correct |
45 |
Correct |
5 ms |
4180 KB |
Output is correct |
46 |
Correct |
4 ms |
4180 KB |
Output is correct |
47 |
Correct |
4 ms |
4180 KB |
Output is correct |
48 |
Correct |
4 ms |
4180 KB |
Output is correct |
49 |
Correct |
4 ms |
4180 KB |
Output is correct |
50 |
Correct |
3 ms |
4180 KB |
Output is correct |
51 |
Correct |
3 ms |
4180 KB |
Output is correct |
52 |
Correct |
3 ms |
4180 KB |
Output is correct |
53 |
Correct |
144 ms |
4384 KB |
Output is correct |
54 |
Correct |
247 ms |
4428 KB |
Output is correct |
55 |
Correct |
103 ms |
4424 KB |
Output is correct |
56 |
Correct |
71 ms |
4372 KB |
Output is correct |
57 |
Correct |
109 ms |
4424 KB |
Output is correct |
58 |
Correct |
3 ms |
4180 KB |
Output is correct |
59 |
Correct |
5 ms |
4180 KB |
Output is correct |
60 |
Correct |
4 ms |
4180 KB |
Output is correct |
61 |
Correct |
3 ms |
4180 KB |
Output is correct |
62 |
Correct |
3 ms |
4236 KB |
Output is correct |
63 |
Correct |
3 ms |
4180 KB |
Output is correct |
64 |
Correct |
228 ms |
4408 KB |
Output is correct |
65 |
Correct |
236 ms |
4432 KB |
Output is correct |
66 |
Correct |
89 ms |
4420 KB |
Output is correct |
67 |
Correct |
70 ms |
4412 KB |
Output is correct |
68 |
Correct |
79 ms |
4368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2079 ms |
11756 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2079 ms |
11756 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
152 ms |
4392 KB |
Output is correct |
3 |
Correct |
186 ms |
4404 KB |
Output is correct |
4 |
Correct |
71 ms |
4404 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
3 ms |
4180 KB |
Output is correct |
8 |
Correct |
68 ms |
4376 KB |
Output is correct |
9 |
Correct |
111 ms |
4396 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Execution timed out |
2078 ms |
9544 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |