#include<bits/stdc++.h>
#ifndef LOCAL
#include "roads.h"
#endif
#define ld long double
#define ll long long
#define pb push_back
using namespace std;
const int N = 1e5 + 10;
const ll INF = 1e18;
vector<pair<int, ll>> g[N];
vector<bool> used(N);
vector<ll> arr[N];
int n, k;
ll dp[N][2];
struct Fenwick {
vector<ll> t;
void build(int n) {
t.resize(n);
}
void inc(int i, ll val) {
for (; i < t.size(); i = (i | (i + 1))) {
t[i] += val;
}
}
ll get(int r) {
ll ans = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) {
ans += t[r];
}
return ans;
}
} sum[N], cnt[N];
void add(int v, ll a) {
int j = lower_bound(arr[v].begin(), arr[v].end(), a) - arr[v].begin();
sum[v].inc(j, a);
cnt[v].inc(j, 1);
}
ll get(int v, int need) {
need = cnt[v].get((int)(arr[v].size() - 1)) - need;
if (need <= 0) return 0;
int l = -1, r = arr[v].size();
while (r - l > 1) {
int mid = (r + l) / 2;
if (cnt[v].get(mid) >= need) {
r = mid;
} else {
l = mid;
}
}
return sum[v].get(r - 1) + (arr[v][r] * (need - cnt[v].get(r - 1)));
}
void dfs(int v, int p) {
used[v] = 1;
ll sum = 0;
vector<ll> arr;
for (auto to : g[v]) {
if (to.first != p && g[to.first].size() > k) {
dfs(to.first, v);
sum += dp[to.first][0] + to.second;
arr.pb({dp[to.first][1] - (dp[to.first][0] + to.second)});
}
}
sort(arr.begin(), arr.end());
dp[v][0] = dp[v][1] = INF;
for (int i = 0; i <= min((int)arr.size(), k); i++) {
dp[v][0] = min(dp[v][0], sum + get(v, k - i));
if (i < k) {
dp[v][1] = min(dp[v][1], sum + get(v, k - i - 1));
}
if (i < arr.size()) sum += arr[i];
}
}
vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) {
n = N;
ll res = 0;
for (int i = 0; i < n - 1; i++) {
g[u[i]].pb({v[i], w[i]});
g[v[i]].pb({u[i], w[i]});
res += w[i];
}
vector<vector<int>> vert(n);
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int a, int b){return g[a].size() > g[b].size();});
for (int i = 0; i < n; i++) {
sort(g[i].begin(), g[i].end(), [&](pair<int,int> a, pair<int,int> b){return g[a.first].size() > g[b.first].size();});
for (auto to : g[i]) {
arr[i].pb(to.second);
}
sort(arr[i].begin(), arr[i].end());
arr[i].erase(unique(arr[i].begin(), arr[i].end()), arr[i].end());
sum[i].build((int)arr[i].size());
cnt[i].build((int)arr[i].size());
vert[g[i].size()].pb(i);
}
vector<ll> ans(n);
ans[0] = res;
for (k = 1; k < n; k++) {
for (auto v : vert[k]) {
for (auto to : g[v]) {
add(to.first, to.second);
}
}
for (auto to : ord) {
if (g[to].size() <= k) break;
used[to] = 0;
}
for (auto to : ord) {
if (g[to].size() <= k) break;
if (!used[to]) {
dfs(to, -1);
ans[k] += dp[to][0];
}
}
}
return ans;
}
#ifdef LOCAL
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> U(N - 1), V(N - 1), W(N - 1);
for (int i = 0; i < N - 1; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
std::vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);
for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%lld",closure_costs[i]);
}
printf("\n");
return 0;
}
#endif
Compilation message
roads.cpp: In member function 'void Fenwick::inc(int, long long int)':
roads.cpp:26:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (; i < t.size(); i = (i | (i + 1))) {
| ~~^~~~~~~~~~
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:65:49: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
65 | if (to.first != p && g[to.first].size() > k) {
| ~~~~~~~~~~~~~~~~~~~^~~
roads.cpp:78:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | if (i < arr.size()) sum += arr[i];
| ~~^~~~~~~~~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:114:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if (g[to].size() <= k) break;
| ~~~~~~~~~~~~~^~~~
roads.cpp:118:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
118 | if (g[to].size() <= k) break;
| ~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
12 ms |
10140 KB |
Output is correct |
3 |
Correct |
12 ms |
10112 KB |
Output is correct |
4 |
Correct |
12 ms |
10068 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9736 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
10 ms |
10072 KB |
Output is correct |
9 |
Correct |
15 ms |
10060 KB |
Output is correct |
10 |
Correct |
5 ms |
9744 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Execution timed out |
2077 ms |
22384 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
101 ms |
36476 KB |
Output is correct |
3 |
Correct |
94 ms |
39872 KB |
Output is correct |
4 |
Correct |
96 ms |
42052 KB |
Output is correct |
5 |
Correct |
93 ms |
41976 KB |
Output is correct |
6 |
Correct |
6 ms |
10196 KB |
Output is correct |
7 |
Correct |
7 ms |
10252 KB |
Output is correct |
8 |
Correct |
7 ms |
10196 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9672 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
56 ms |
29112 KB |
Output is correct |
13 |
Correct |
92 ms |
42060 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
80 ms |
38752 KB |
Output is correct |
16 |
Correct |
86 ms |
42044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9700 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9724 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9700 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
6 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9724 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
5 ms |
9684 KB |
Output is correct |
23 |
Correct |
6 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
10196 KB |
Output is correct |
25 |
Correct |
6 ms |
10028 KB |
Output is correct |
26 |
Correct |
7 ms |
10068 KB |
Output is correct |
27 |
Correct |
9 ms |
10116 KB |
Output is correct |
28 |
Correct |
8 ms |
10196 KB |
Output is correct |
29 |
Correct |
7 ms |
10068 KB |
Output is correct |
30 |
Correct |
8 ms |
10196 KB |
Output is correct |
31 |
Correct |
9 ms |
10068 KB |
Output is correct |
32 |
Correct |
9 ms |
10068 KB |
Output is correct |
33 |
Correct |
7 ms |
10196 KB |
Output is correct |
34 |
Correct |
7 ms |
10324 KB |
Output is correct |
35 |
Correct |
6 ms |
10196 KB |
Output is correct |
36 |
Correct |
13 ms |
10144 KB |
Output is correct |
37 |
Correct |
14 ms |
10072 KB |
Output is correct |
38 |
Correct |
12 ms |
10128 KB |
Output is correct |
39 |
Correct |
5 ms |
9684 KB |
Output is correct |
40 |
Correct |
7 ms |
9648 KB |
Output is correct |
41 |
Correct |
5 ms |
9684 KB |
Output is correct |
42 |
Correct |
5 ms |
9684 KB |
Output is correct |
43 |
Correct |
6 ms |
9684 KB |
Output is correct |
44 |
Correct |
5 ms |
9684 KB |
Output is correct |
45 |
Correct |
5 ms |
9748 KB |
Output is correct |
46 |
Correct |
5 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9712 KB |
Output is correct |
48 |
Correct |
5 ms |
9684 KB |
Output is correct |
49 |
Correct |
5 ms |
9684 KB |
Output is correct |
50 |
Correct |
5 ms |
9684 KB |
Output is correct |
51 |
Correct |
5 ms |
9684 KB |
Output is correct |
52 |
Correct |
5 ms |
9684 KB |
Output is correct |
53 |
Correct |
6 ms |
10016 KB |
Output is correct |
54 |
Correct |
7 ms |
10164 KB |
Output is correct |
55 |
Correct |
8 ms |
10196 KB |
Output is correct |
56 |
Correct |
10 ms |
10004 KB |
Output is correct |
57 |
Correct |
12 ms |
10068 KB |
Output is correct |
58 |
Correct |
5 ms |
9636 KB |
Output is correct |
59 |
Correct |
5 ms |
9684 KB |
Output is correct |
60 |
Correct |
6 ms |
9752 KB |
Output is correct |
61 |
Correct |
5 ms |
9684 KB |
Output is correct |
62 |
Correct |
5 ms |
9684 KB |
Output is correct |
63 |
Correct |
5 ms |
9684 KB |
Output is correct |
64 |
Correct |
7 ms |
10068 KB |
Output is correct |
65 |
Correct |
7 ms |
10068 KB |
Output is correct |
66 |
Correct |
7 ms |
10192 KB |
Output is correct |
67 |
Correct |
8 ms |
10068 KB |
Output is correct |
68 |
Correct |
7 ms |
10068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
31812 KB |
Output is correct |
2 |
Correct |
171 ms |
31380 KB |
Output is correct |
3 |
Correct |
222 ms |
33084 KB |
Output is correct |
4 |
Correct |
180 ms |
32516 KB |
Output is correct |
5 |
Correct |
211 ms |
33268 KB |
Output is correct |
6 |
Correct |
681 ms |
31092 KB |
Output is correct |
7 |
Correct |
197 ms |
34504 KB |
Output is correct |
8 |
Execution timed out |
2095 ms |
30500 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
31812 KB |
Output is correct |
2 |
Correct |
171 ms |
31380 KB |
Output is correct |
3 |
Correct |
222 ms |
33084 KB |
Output is correct |
4 |
Correct |
180 ms |
32516 KB |
Output is correct |
5 |
Correct |
211 ms |
33268 KB |
Output is correct |
6 |
Correct |
681 ms |
31092 KB |
Output is correct |
7 |
Correct |
197 ms |
34504 KB |
Output is correct |
8 |
Execution timed out |
2095 ms |
30500 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
12 ms |
10140 KB |
Output is correct |
3 |
Correct |
12 ms |
10112 KB |
Output is correct |
4 |
Correct |
12 ms |
10068 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9736 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
10 ms |
10072 KB |
Output is correct |
9 |
Correct |
15 ms |
10060 KB |
Output is correct |
10 |
Correct |
5 ms |
9744 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Execution timed out |
2077 ms |
22384 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |