#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
int n, preDfs[100100], postDfs[100100], dfsTime, deg[100100], curDeg[100100], par[100100];
vector<pair<int, int>> a[100100];
long long f[100100];
long long g[100100]; // can keep parent edge
vector<int> nodes[100100];
void visit(int x, int k)
{
f[x] = 0;
vector<long long> gain;
for (auto [y, w] : a[x])
if (y != par[x])
{
visit(y, k);
f[x] += f[y] + w;
gain.push_back(g[y] - f[y] - w);
}
sort(gain.begin(), gain.end());
g[x] = f[x];
for (int i = 0; i < k && i < gain.size(); i++)
{
if (gain[i] >= 0)
break;
f[x] += gain[i];
if (i < k - 1)
g[x] += gain[i];
}
f[x] = min(f[x], g[x]);
}
void dfs(int x)
{
preDfs[x] = ++dfsTime;
for (auto [y, _] : a[x])
if (y != par[x])
{
par[y] = x;
dfs(y);
}
postDfs[x] = dfsTime;
}
int solve(int k, set<pair<int, int>> &nodes)
{
for (auto [_, x] : nodes)
curDeg[x] = deg[x];
int res = 0;
for (auto [_, x] : nodes)
if (curDeg[x] > k)
{
res += curDeg[x] - k;
if (par[x] >= 0)
curDeg[par[x]]--;
}
return res;
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
int isWeighted = 0;
for (int i = 0; i < n - 1; i++)
{
a[U[i]].push_back({V[i], W[i]});
a[V[i]].push_back({U[i], W[i]});
deg[U[i]]++;
deg[V[i]]++;
if (W[i] > 1)
isWeighted = 1;
}
for (int i = 0; i < n; i++)
nodes[deg[i]].push_back(i);
set<int> allDegs;
vector<long long> ans(n);
ans[0] = accumulate(W.begin(), W.end(), 0LL);
dfsTime = 0;
par[0] = -1;
dfs(0);
if (n <= 2000)
{
for (int i = 1; i < n - 1; i++)
{
visit(0, i);
ans[i] = f[0];
}
}
else if (!isWeighted)
{
set<pair<int, int>> s;
for (int i = 0; i < n; i++)
s.insert({-preDfs[i], i});
for (int i = 1; i < n - 1; i++)
{
if (nodes[i].empty()) ans[i] = ans[i - 1];
else
{
for (int x : nodes[i])
s.erase({-preDfs[x], x});
ans[i] = solve(i, s);
}
}
}
return ans;
}
Compilation message
roads.cpp: In function 'void visit(int, int)':
roads.cpp:25:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < k && i < gain.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
190 ms |
5280 KB |
Output is correct |
3 |
Correct |
231 ms |
5280 KB |
Output is correct |
4 |
Correct |
116 ms |
5284 KB |
Output is correct |
5 |
Correct |
4 ms |
4988 KB |
Output is correct |
6 |
Correct |
6 ms |
5008 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
103 ms |
5256 KB |
Output is correct |
9 |
Correct |
163 ms |
5264 KB |
Output is correct |
10 |
Correct |
5 ms |
5008 KB |
Output is correct |
11 |
Correct |
5 ms |
4940 KB |
Output is correct |
12 |
Incorrect |
41 ms |
11244 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
63 ms |
17612 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
5012 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
4940 KB |
Output is correct |
6 |
Correct |
8 ms |
4940 KB |
Output is correct |
7 |
Correct |
8 ms |
4940 KB |
Output is correct |
8 |
Correct |
8 ms |
4940 KB |
Output is correct |
9 |
Correct |
6 ms |
5008 KB |
Output is correct |
10 |
Correct |
6 ms |
4940 KB |
Output is correct |
11 |
Correct |
6 ms |
5068 KB |
Output is correct |
12 |
Correct |
6 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5008 KB |
Output is correct |
14 |
Correct |
5 ms |
5008 KB |
Output is correct |
15 |
Correct |
6 ms |
4940 KB |
Output is correct |
16 |
Correct |
5 ms |
5004 KB |
Output is correct |
17 |
Correct |
8 ms |
5052 KB |
Output is correct |
18 |
Correct |
6 ms |
4940 KB |
Output is correct |
19 |
Correct |
7 ms |
4940 KB |
Output is correct |
20 |
Correct |
5 ms |
5008 KB |
Output is correct |
21 |
Correct |
4 ms |
5008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
5012 KB |
Output is correct |
4 |
Correct |
5 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
4940 KB |
Output is correct |
6 |
Correct |
8 ms |
4940 KB |
Output is correct |
7 |
Correct |
8 ms |
4940 KB |
Output is correct |
8 |
Correct |
8 ms |
4940 KB |
Output is correct |
9 |
Correct |
6 ms |
5008 KB |
Output is correct |
10 |
Correct |
6 ms |
4940 KB |
Output is correct |
11 |
Correct |
6 ms |
5068 KB |
Output is correct |
12 |
Correct |
6 ms |
5068 KB |
Output is correct |
13 |
Correct |
5 ms |
5008 KB |
Output is correct |
14 |
Correct |
5 ms |
5008 KB |
Output is correct |
15 |
Correct |
6 ms |
4940 KB |
Output is correct |
16 |
Correct |
5 ms |
5004 KB |
Output is correct |
17 |
Correct |
8 ms |
5052 KB |
Output is correct |
18 |
Correct |
6 ms |
4940 KB |
Output is correct |
19 |
Correct |
7 ms |
4940 KB |
Output is correct |
20 |
Correct |
5 ms |
5008 KB |
Output is correct |
21 |
Correct |
4 ms |
5008 KB |
Output is correct |
22 |
Correct |
4 ms |
5008 KB |
Output is correct |
23 |
Correct |
117 ms |
5164 KB |
Output is correct |
24 |
Correct |
370 ms |
5196 KB |
Output is correct |
25 |
Correct |
228 ms |
5224 KB |
Output is correct |
26 |
Correct |
284 ms |
5248 KB |
Output is correct |
27 |
Correct |
203 ms |
5288 KB |
Output is correct |
28 |
Correct |
144 ms |
5280 KB |
Output is correct |
29 |
Correct |
262 ms |
5228 KB |
Output is correct |
30 |
Correct |
184 ms |
5272 KB |
Output is correct |
31 |
Correct |
165 ms |
5316 KB |
Output is correct |
32 |
Correct |
113 ms |
5272 KB |
Output is correct |
33 |
Correct |
156 ms |
5324 KB |
Output is correct |
34 |
Correct |
207 ms |
5452 KB |
Output is correct |
35 |
Correct |
177 ms |
5452 KB |
Output is correct |
36 |
Correct |
190 ms |
5288 KB |
Output is correct |
37 |
Correct |
224 ms |
5196 KB |
Output is correct |
38 |
Correct |
120 ms |
5296 KB |
Output is correct |
39 |
Correct |
4 ms |
4940 KB |
Output is correct |
40 |
Correct |
4 ms |
4940 KB |
Output is correct |
41 |
Correct |
6 ms |
4940 KB |
Output is correct |
42 |
Correct |
7 ms |
4940 KB |
Output is correct |
43 |
Correct |
6 ms |
4940 KB |
Output is correct |
44 |
Correct |
6 ms |
4940 KB |
Output is correct |
45 |
Correct |
6 ms |
4940 KB |
Output is correct |
46 |
Correct |
7 ms |
4940 KB |
Output is correct |
47 |
Correct |
6 ms |
5004 KB |
Output is correct |
48 |
Correct |
7 ms |
5068 KB |
Output is correct |
49 |
Correct |
7 ms |
5068 KB |
Output is correct |
50 |
Correct |
4 ms |
4940 KB |
Output is correct |
51 |
Correct |
7 ms |
4940 KB |
Output is correct |
52 |
Correct |
6 ms |
4944 KB |
Output is correct |
53 |
Correct |
185 ms |
5204 KB |
Output is correct |
54 |
Correct |
333 ms |
5316 KB |
Output is correct |
55 |
Correct |
174 ms |
5276 KB |
Output is correct |
56 |
Correct |
104 ms |
5232 KB |
Output is correct |
57 |
Correct |
148 ms |
5272 KB |
Output is correct |
58 |
Correct |
4 ms |
5008 KB |
Output is correct |
59 |
Correct |
9 ms |
5056 KB |
Output is correct |
60 |
Correct |
6 ms |
5008 KB |
Output is correct |
61 |
Correct |
5 ms |
4940 KB |
Output is correct |
62 |
Correct |
6 ms |
4940 KB |
Output is correct |
63 |
Correct |
4 ms |
4940 KB |
Output is correct |
64 |
Correct |
296 ms |
5264 KB |
Output is correct |
65 |
Correct |
301 ms |
5248 KB |
Output is correct |
66 |
Correct |
128 ms |
5256 KB |
Output is correct |
67 |
Correct |
115 ms |
5316 KB |
Output is correct |
68 |
Correct |
132 ms |
5272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
19768 KB |
Output is correct |
2 |
Correct |
159 ms |
19400 KB |
Output is correct |
3 |
Incorrect |
130 ms |
20924 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
19768 KB |
Output is correct |
2 |
Correct |
159 ms |
19400 KB |
Output is correct |
3 |
Incorrect |
130 ms |
20924 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4940 KB |
Output is correct |
2 |
Correct |
190 ms |
5280 KB |
Output is correct |
3 |
Correct |
231 ms |
5280 KB |
Output is correct |
4 |
Correct |
116 ms |
5284 KB |
Output is correct |
5 |
Correct |
4 ms |
4988 KB |
Output is correct |
6 |
Correct |
6 ms |
5008 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
103 ms |
5256 KB |
Output is correct |
9 |
Correct |
163 ms |
5264 KB |
Output is correct |
10 |
Correct |
5 ms |
5008 KB |
Output is correct |
11 |
Correct |
5 ms |
4940 KB |
Output is correct |
12 |
Incorrect |
41 ms |
11244 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |