#include "roads.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
constexpr int NMAX = 100005;
typedef long long LL;
constexpr LL INF = 1LL * 1e15;
struct muchie {
int x, y;
LL cost;
};
muchie E[NMAX];
vector <int> G[NMAX];
LL dp[NMAX][2];
void Dfs (int k, int Node, int dad = -1) {
for (auto it : G[Node]) {
int to = (E[it].x == Node ? E[it].y : E[it].x);
if (to == dad) continue;
Dfs(k, to, Node);
}
int grad = 0;
vector <LL> V;
LL sum = 0;
for (auto it : G[Node]) {
int to = (E[it].x == Node ? E[it].y : E[it].x);
if (to == dad) continue;
++ grad;
V.push_back(dp[to][1] - dp[to][0]);
sum += dp[to][0];
}
dp[Node][0] = INF;
dp[Node][1] = INF;
sort(V.begin(), V.end());
if (k > 0) {
dp[Node][0] = sum;
for (int i = 0; i < max(0, grad - k + 1); ++ i )
dp[Node][0] += V[i];
for (int i = max(0, grad - k + 1); i < V.size() && V[i] < 0; ++ i )
dp[Node][0] += V[i];
}
if (dad == -1) {
dp[Node][1] = sum;
for (int i = 0; i < max(0, grad - k); ++ i )
dp[Node][1] += V[i];
for (int i = max(0, grad - k); i < V.size() && V[i] < 0; ++ i )
dp[Node][1] += V[i];
}
for (auto it : G[Node]) {
int to = (E[it].x == Node ? E[it].y : E[it].x);
if (to != dad) continue;
dp[Node][1] = sum + E[it].cost;
for (int i = 0; i < max(0, grad - k); ++ i )
dp[Node][1] += V[i];
for (int i = max(0, grad - k); i < V.size() && V[i] < 0; ++ i )
dp[Node][1] += V[i];
break;
}
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,
std::vector<int> V,
std::vector<int> W) {
for (int i = 1; i < N; ++ i ) {
E[i] = {U[i-1], V[i-1], 1LL * W[i-1]};
G[U[i-1]].push_back(i);
G[V[i-1]].push_back(i);
}
vector <LL> ans;
for (int k = 0; k < N; ++ k ) {
Dfs(k, 0);
ans.push_back(dp[0][1]);
}
return ans;
}
Compilation message
roads.cpp: In function 'void Dfs(int, int, int)':
roads.cpp:56:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = max(0, grad - k + 1); i < V.size() && V[i] < 0; ++ i )
| ~~^~~~~~~~~~
roads.cpp:64:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = max(0, grad - k); i < V.size() && V[i] < 0; ++ i )
| ~~^~~~~~~~~~
roads.cpp:76:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i = max(0, grad - k); i < V.size() && V[i] < 0; ++ i )
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
212 ms |
2772 KB |
Output is correct |
3 |
Correct |
233 ms |
2872 KB |
Output is correct |
4 |
Correct |
138 ms |
2872 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
4 ms |
2676 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
110 ms |
2832 KB |
Output is correct |
9 |
Correct |
164 ms |
2984 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2644 KB |
Output is correct |
12 |
Execution timed out |
2100 ms |
9056 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
2087 ms |
24100 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2664 KB |
Output is correct |
5 |
Correct |
5 ms |
2664 KB |
Output is correct |
6 |
Correct |
4 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
2644 KB |
Output is correct |
8 |
Correct |
5 ms |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
4 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2664 KB |
Output is correct |
5 |
Correct |
5 ms |
2664 KB |
Output is correct |
6 |
Correct |
4 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
2644 KB |
Output is correct |
8 |
Correct |
5 ms |
2644 KB |
Output is correct |
9 |
Correct |
4 ms |
2644 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
4 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
3 ms |
2644 KB |
Output is correct |
16 |
Correct |
3 ms |
2644 KB |
Output is correct |
17 |
Correct |
5 ms |
2644 KB |
Output is correct |
18 |
Correct |
4 ms |
2644 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
4 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
121 ms |
2800 KB |
Output is correct |
24 |
Correct |
322 ms |
3020 KB |
Output is correct |
25 |
Correct |
246 ms |
2892 KB |
Output is correct |
26 |
Correct |
231 ms |
2876 KB |
Output is correct |
27 |
Correct |
226 ms |
2900 KB |
Output is correct |
28 |
Correct |
134 ms |
2892 KB |
Output is correct |
29 |
Correct |
256 ms |
2892 KB |
Output is correct |
30 |
Correct |
187 ms |
2876 KB |
Output is correct |
31 |
Correct |
193 ms |
3000 KB |
Output is correct |
32 |
Correct |
134 ms |
2880 KB |
Output is correct |
33 |
Correct |
150 ms |
3136 KB |
Output is correct |
34 |
Correct |
195 ms |
3208 KB |
Output is correct |
35 |
Correct |
158 ms |
3168 KB |
Output is correct |
36 |
Correct |
214 ms |
2888 KB |
Output is correct |
37 |
Correct |
244 ms |
2900 KB |
Output is correct |
38 |
Correct |
134 ms |
2900 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2656 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
4 ms |
2644 KB |
Output is correct |
43 |
Correct |
3 ms |
2644 KB |
Output is correct |
44 |
Correct |
4 ms |
2644 KB |
Output is correct |
45 |
Correct |
5 ms |
2656 KB |
Output is correct |
46 |
Correct |
4 ms |
2644 KB |
Output is correct |
47 |
Correct |
4 ms |
2644 KB |
Output is correct |
48 |
Correct |
6 ms |
2644 KB |
Output is correct |
49 |
Correct |
4 ms |
2644 KB |
Output is correct |
50 |
Correct |
3 ms |
2660 KB |
Output is correct |
51 |
Correct |
5 ms |
2660 KB |
Output is correct |
52 |
Correct |
3 ms |
2644 KB |
Output is correct |
53 |
Correct |
194 ms |
2828 KB |
Output is correct |
54 |
Correct |
333 ms |
2896 KB |
Output is correct |
55 |
Correct |
178 ms |
2916 KB |
Output is correct |
56 |
Correct |
118 ms |
2772 KB |
Output is correct |
57 |
Correct |
173 ms |
2872 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
4 ms |
2644 KB |
Output is correct |
60 |
Correct |
4 ms |
2644 KB |
Output is correct |
61 |
Correct |
3 ms |
2644 KB |
Output is correct |
62 |
Correct |
3 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2644 KB |
Output is correct |
64 |
Correct |
323 ms |
2848 KB |
Output is correct |
65 |
Correct |
313 ms |
2864 KB |
Output is correct |
66 |
Correct |
147 ms |
2892 KB |
Output is correct |
67 |
Correct |
128 ms |
2992 KB |
Output is correct |
68 |
Correct |
145 ms |
2884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2080 ms |
11092 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2080 ms |
11092 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
212 ms |
2772 KB |
Output is correct |
3 |
Correct |
233 ms |
2872 KB |
Output is correct |
4 |
Correct |
138 ms |
2872 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
4 ms |
2676 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
110 ms |
2832 KB |
Output is correct |
9 |
Correct |
164 ms |
2984 KB |
Output is correct |
10 |
Correct |
3 ms |
2644 KB |
Output is correct |
11 |
Correct |
3 ms |
2644 KB |
Output is correct |
12 |
Execution timed out |
2100 ms |
9056 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |