#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
#define sz(x) int(x.size())
const int mx = 100'000;
const ll INF = 1'000'000'000'000'000'000LL;
int N;
vi U, V, W;
vi edge[mx];
vi deg(mx, 0);
vi deg_occ[mx];
vi depth(mx);
vi parent(mx, -1);
ll wt_sum = 0;
vi par_wt(mx);
void dfs1(int u, int p)
{
for(int e: edge[u])
{
int v = U[e] + V[e] - u;
int w = W[e];
if(v == p) continue;
par_wt[v] = w;
depth[v] = depth[u] + 1;
parent[v] = u;
dfs1(v, u);
}
}
struct segtree
{
int l;
int r;
ll sm = 0;
int ct = 0;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
l = L;
r = R;
sm = 0;
ct = 0;
}
void insert(int v)
{
cerr << "enter insert\n";
ct++;
sm += v;
if(l != r)
{
if(v <= (l+r)/2)
{
if(left == NULL) left = new segtree(l, (l+r)/2);
left->insert(v);
}
else
{
if(right == NULL) right = new segtree((l+r)/2+1, r);
right->insert(v);
}
}
cerr << "exit insert\n";
}
ll getsum(ll v)
{
cerr << "enter getsum\n";
if(v == 0)
{
return 0;
}
else if(l == r)
{
return ll(l) * v;
}
else
{
cerr << "case 3\n";
if(left != NULL && left->ct >= v) return left->getsum(v);
else return (left == NULL ? 0 : left->sm) + right->getsum(v - (left == NULL ? 0 : left->ct));
}
cerr << "exit getsum\n";
}
};
struct DS
{
int curr_size = 0;
segtree S = segtree(1, 1'000'000'000);
ll minSum(int z)
{
if(z > curr_size) return INF;
else if(z <= 0) return 0;
else return S.getsum(z);
}
void insert(int v)
{
curr_size++;
S.insert(v);
}
int size()
{
return curr_size;
}
};
// struct DS
// {
// multiset<ll> values;
// ll minSum(int z)
// {
// if(z > sz(values)) return INF;
// if(z <= 0) return 0;
// int ct = 0;
// ll res = 0;
// for(ll v : values)
// {
// res += v;
// ct++;
// if(ct == z) break;
// }
// return res;
// }
// void insert(ll v)
// {
// values.insert(v);
// }
// int size()
// {
// return sz(values);
// }
// };
vll minimum_closure_costs(int N_, vi U_, vi V_, vi W_)
{
N = N_;
U = U_;
V = V_;
W = W_;
for(int e = 0; e < N-1; e++)
{
edge[U[e]].push_back(e);
edge[V[e]].push_back(e);
deg[U[e]]++;
deg[V[e]]++;
wt_sum += W[e];
}
for(int i = 0; i < N; i++)
deg_occ[deg[i]].push_back(i);
for(int i = 0; i < N; i++)
{
sort(edge[i].begin(), edge[i].end(), [] (int e, int f)
{
return deg[U[e]] + deg[V[e]] > deg[U[f]] + deg[V[f]];
});
}
dfs1(0, -1);
set<pii> it_list;
for(int i = 0; i < N; i++) it_list.insert({-depth[i], i});
vll res(N, 0);
res[0] = wt_sum;
// root deg <= K-1
vll dp0(N), dp1(N);
//root deg <= K
DS extra_edges[N];
vi intdeg = deg;
for(int i = 1; i < N; i++)
intdeg[i]--;
for(int k = 1; k < N; k++)
{
// cerr << "\n\n\n\n\n";
// cerr << "k = " << k << '\n';
for(int u: deg_occ[k]) //deg_occ[0] is empty
{
// cerr << "eliminating " << u << '\n';
it_list.erase({-depth[u], u});
for(int e : edge[u])
{
int v = U[e] + V[e] - u;
int w = W[e];
if(parent[v] != u)
extra_edges[v].insert(w);
// cerr << "extra edges " << v << " -> insert " << w << '\n';
}
}
for(auto p: it_list)
{
// cerr << "\n\n";
int u = p.second;
// cerr << "u = " << u << '\n';
// cerr << "ee = ";
// for(ll y : extra_edges[u].values) cerr << y << ' ';
// cerr << '\n';
dp0[u] = dp1[u] = INF;
ll basicCost = 0;
vector<ll> upgrades;
for(auto e: edge[u])
{
int v = U[e] + V[e] - u;
ll w = W[e];
if(v == parent[u]) continue;
if(deg[v] <= k) break;
basicCost += dp1[v];
upgrades.push_back(dp0[v] + w - dp1[v]);
}
sort(upgrades.begin(), upgrades.end());
ll upgrade_total = 0;
dp0[u] = min(dp0[u], basicCost + extra_edges[u].minSum(intdeg[u] - k));
dp1[u] = min(dp1[u], basicCost + extra_edges[u].minSum(intdeg[u] - (k-1)));
// cerr << "pre : " << dp0[u] << ' ' << dp1[u] << '\n';
// cerr << basicCost << '\n';
// cerr << "ee size = " << sz(extra_edges[u]) << ", query val = " << intdeg[u] - k << '\n';
for(int cct = 0; cct < sz(upgrades); cct++)
{
// cerr << "cct = " << cct << '\n';
upgrade_total += upgrades[cct];
dp0[u] = min(dp0[u], basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - k - (cct+1)));
dp1[u] = min(dp1[u], basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - (k-1) - (cct+1)));
// cerr << "! " << intdeg[u] - k - (cct+1) << ' ' << intdeg[u] - (k-1) - (cct+1) << '\n';
// cerr << cct << " , " << basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - k - (cct+1)) << ' ' << basicCost + upgrade_total + extra_edges[u].minSum(intdeg[u] - (k-1) - (cct+1)) << '\n';
}
if(u == 0)
{
// cerr << "adding " << dp0[u] << " a to res " << k << '\n';
res[k] += dp0[u];
}
else if(deg[parent[u]] <= k)
{
// cerr << "adding " << min(dp1[u], dp0[u] + par_wt[u]) << " b to res " << k << '\n';
res[k] += min(dp1[u], dp0[u] + par_wt[u]);
}
// cerr << u << " : " << parent[u] << ' ' << dp0[u] << ' ' << dp1[u] << '\n';
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6860 KB |
Output is correct |
2 |
Correct |
386 ms |
12628 KB |
Output is correct |
3 |
Correct |
432 ms |
12972 KB |
Output is correct |
4 |
Correct |
394 ms |
11108 KB |
Output is correct |
5 |
Correct |
30 ms |
7348 KB |
Output is correct |
6 |
Correct |
44 ms |
7476 KB |
Output is correct |
7 |
Correct |
42 ms |
7380 KB |
Output is correct |
8 |
Correct |
340 ms |
10588 KB |
Output is correct |
9 |
Correct |
405 ms |
11340 KB |
Output is correct |
10 |
Correct |
41 ms |
7388 KB |
Output is correct |
11 |
Correct |
44 ms |
7304 KB |
Output is correct |
12 |
Execution timed out |
2089 ms |
41752 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6860 KB |
Output is correct |
2 |
Execution timed out |
2083 ms |
86320 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6860 KB |
Output is correct |
2 |
Correct |
4 ms |
6860 KB |
Output is correct |
3 |
Correct |
4 ms |
6860 KB |
Output is correct |
4 |
Correct |
26 ms |
7276 KB |
Output is correct |
5 |
Correct |
33 ms |
7532 KB |
Output is correct |
6 |
Correct |
57 ms |
7388 KB |
Output is correct |
7 |
Correct |
34 ms |
7496 KB |
Output is correct |
8 |
Correct |
39 ms |
7376 KB |
Output is correct |
9 |
Correct |
47 ms |
7620 KB |
Output is correct |
10 |
Correct |
15 ms |
7244 KB |
Output is correct |
11 |
Correct |
17 ms |
7296 KB |
Output is correct |
12 |
Correct |
17 ms |
7292 KB |
Output is correct |
13 |
Correct |
31 ms |
7372 KB |
Output is correct |
14 |
Correct |
44 ms |
7524 KB |
Output is correct |
15 |
Correct |
43 ms |
7260 KB |
Output is correct |
16 |
Correct |
17 ms |
7152 KB |
Output is correct |
17 |
Correct |
33 ms |
7364 KB |
Output is correct |
18 |
Correct |
47 ms |
7436 KB |
Output is correct |
19 |
Correct |
42 ms |
7356 KB |
Output is correct |
20 |
Correct |
43 ms |
7296 KB |
Output is correct |
21 |
Correct |
4 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6860 KB |
Output is correct |
2 |
Correct |
4 ms |
6860 KB |
Output is correct |
3 |
Correct |
4 ms |
6860 KB |
Output is correct |
4 |
Correct |
26 ms |
7276 KB |
Output is correct |
5 |
Correct |
33 ms |
7532 KB |
Output is correct |
6 |
Correct |
57 ms |
7388 KB |
Output is correct |
7 |
Correct |
34 ms |
7496 KB |
Output is correct |
8 |
Correct |
39 ms |
7376 KB |
Output is correct |
9 |
Correct |
47 ms |
7620 KB |
Output is correct |
10 |
Correct |
15 ms |
7244 KB |
Output is correct |
11 |
Correct |
17 ms |
7296 KB |
Output is correct |
12 |
Correct |
17 ms |
7292 KB |
Output is correct |
13 |
Correct |
31 ms |
7372 KB |
Output is correct |
14 |
Correct |
44 ms |
7524 KB |
Output is correct |
15 |
Correct |
43 ms |
7260 KB |
Output is correct |
16 |
Correct |
17 ms |
7152 KB |
Output is correct |
17 |
Correct |
33 ms |
7364 KB |
Output is correct |
18 |
Correct |
47 ms |
7436 KB |
Output is correct |
19 |
Correct |
42 ms |
7356 KB |
Output is correct |
20 |
Correct |
43 ms |
7296 KB |
Output is correct |
21 |
Correct |
4 ms |
6860 KB |
Output is correct |
22 |
Correct |
4 ms |
6972 KB |
Output is correct |
23 |
Correct |
182 ms |
10484 KB |
Output is correct |
24 |
Correct |
278 ms |
12776 KB |
Output is correct |
25 |
Correct |
237 ms |
11076 KB |
Output is correct |
26 |
Correct |
435 ms |
12416 KB |
Output is correct |
27 |
Correct |
642 ms |
15424 KB |
Output is correct |
28 |
Correct |
623 ms |
13380 KB |
Output is correct |
29 |
Correct |
345 ms |
12900 KB |
Output is correct |
30 |
Correct |
645 ms |
15496 KB |
Output is correct |
31 |
Correct |
565 ms |
12836 KB |
Output is correct |
32 |
Correct |
522 ms |
12512 KB |
Output is correct |
33 |
Correct |
128 ms |
10936 KB |
Output is correct |
34 |
Correct |
146 ms |
11588 KB |
Output is correct |
35 |
Correct |
132 ms |
11028 KB |
Output is correct |
36 |
Correct |
390 ms |
12684 KB |
Output is correct |
37 |
Correct |
413 ms |
13076 KB |
Output is correct |
38 |
Correct |
395 ms |
11156 KB |
Output is correct |
39 |
Correct |
4 ms |
6860 KB |
Output is correct |
40 |
Correct |
4 ms |
6912 KB |
Output is correct |
41 |
Correct |
23 ms |
7268 KB |
Output is correct |
42 |
Correct |
36 ms |
7628 KB |
Output is correct |
43 |
Correct |
74 ms |
7456 KB |
Output is correct |
44 |
Correct |
34 ms |
7500 KB |
Output is correct |
45 |
Correct |
39 ms |
7416 KB |
Output is correct |
46 |
Correct |
47 ms |
7600 KB |
Output is correct |
47 |
Correct |
16 ms |
7260 KB |
Output is correct |
48 |
Correct |
17 ms |
7336 KB |
Output is correct |
49 |
Correct |
17 ms |
7372 KB |
Output is correct |
50 |
Correct |
31 ms |
7288 KB |
Output is correct |
51 |
Correct |
44 ms |
7596 KB |
Output is correct |
52 |
Correct |
42 ms |
7268 KB |
Output is correct |
53 |
Correct |
239 ms |
10672 KB |
Output is correct |
54 |
Correct |
279 ms |
11868 KB |
Output is correct |
55 |
Correct |
620 ms |
13328 KB |
Output is correct |
56 |
Correct |
355 ms |
10608 KB |
Output is correct |
57 |
Correct |
427 ms |
11248 KB |
Output is correct |
58 |
Correct |
17 ms |
7116 KB |
Output is correct |
59 |
Correct |
36 ms |
7372 KB |
Output is correct |
60 |
Correct |
47 ms |
7476 KB |
Output is correct |
61 |
Correct |
44 ms |
7244 KB |
Output is correct |
62 |
Correct |
43 ms |
7308 KB |
Output is correct |
63 |
Correct |
4 ms |
6860 KB |
Output is correct |
64 |
Correct |
288 ms |
11752 KB |
Output is correct |
65 |
Correct |
287 ms |
11756 KB |
Output is correct |
66 |
Correct |
565 ms |
12788 KB |
Output is correct |
67 |
Correct |
538 ms |
12548 KB |
Output is correct |
68 |
Correct |
635 ms |
13516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2085 ms |
80000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2085 ms |
80000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6860 KB |
Output is correct |
2 |
Correct |
386 ms |
12628 KB |
Output is correct |
3 |
Correct |
432 ms |
12972 KB |
Output is correct |
4 |
Correct |
394 ms |
11108 KB |
Output is correct |
5 |
Correct |
30 ms |
7348 KB |
Output is correct |
6 |
Correct |
44 ms |
7476 KB |
Output is correct |
7 |
Correct |
42 ms |
7380 KB |
Output is correct |
8 |
Correct |
340 ms |
10588 KB |
Output is correct |
9 |
Correct |
405 ms |
11340 KB |
Output is correct |
10 |
Correct |
41 ms |
7388 KB |
Output is correct |
11 |
Correct |
44 ms |
7304 KB |
Output is correct |
12 |
Execution timed out |
2089 ms |
41752 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |