This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
set<pair<ll, ll>> adj[NMAX];
int dg[NMAX], vis[NMAX];
ll dp[NMAX][2], S[NMAX];
vector<int> D[NMAX];
set<int> X;
multiset<ll> A[NMAX], t[NMAX];
void dfs(int now, int k) {
vis[now] = 1;
ll& ret = dp[now][0] = 0;
ll c = 0;
multiset<ll> C;
for(auto& p : adj[now]) {
ll nx = p.first, w = p.second;
if(vis[nx]) continue;
dfs(nx, k);
ret += dp[nx][0] + w;
ll t = dp[nx][1] - dp[nx][0] - w;
c++;
C.emplace(t);
}
while((int)A[now].size() < k - c) {
auto it = t[now].begin();
assert(*it <= 0);
A[now].emplace(*it); S[now] += *it;
t[now].erase(it);
}
while(A[now].size() && (int)A[now].size() > k - c) {
auto it = --A[now].end(); S[now] -= *it;
assert(*it <= 0);
t[now].emplace(*it);
A[now].erase(it);
}
if(c >= k) assert(A[now].size() == 0);
else assert(A[now].size() + c == k);
ret += S[now];
/*cout << "now is : " << now << ' ' << ret << '\n';
for (ll x : A[now]) cout << x << ' ';
cout << '\n';
for (ll x : C[now]) cout << x << ' ';
cout << '\n';*/
auto it = t[now].begin();
for(int i = 0; i < c && it != t[now].end(); i++) C.emplace(*it++);
if(!c) {
//cout << k << ' ' << now << ' ' << ret << '\n';
//cout << ret << '\n';
dp[now][1] = ret - *A[now].rbegin();
return;
}
for(ll x : A[now]) {
C.emplace(x); ret -= x;
}
it = C.begin();
int sz = k - 1;
while(sz--) ret += min(*it++, 0LL);
dp[now][1] = ret;
ret += min(*it, 0LL);
//cout << ret << '\n';
//cout << "Now is : " << now << '\n';
//for(ll x : C[now]) cout << x << ' ';
//cout << '\n';
return;
}
vector<ll> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
vector<ll> ans(N, 0);
for(int i = 0; i < N - 1; i++) {
adj[U[i]].emplace(V[i], W[i]);
adj[V[i]].emplace(U[i], W[i]);
dg[U[i]]++; dg[V[i]]++;
ans[0] += W[i];
}
for(int i = 0; i < N; i++) {
D[dg[i]].emplace_back(i);
X.emplace(i);
}
for(int k = 1; k < N; k++) {
// 노드 삭제하기
//cout << "NOW TURN IS : " << k << '\n';
for(int x : D[k]) {
X.erase(x);
//cout << "erase : " << x << '\n';
for(auto& p : adj[x]) {
ll nx = p.first, w = p.second;
adj[nx].erase({ x, w });
S[nx] += w; t[nx].emplace(-w);
}
}
// 각 트리에 대해서 탐색
for(int x : X) if(!vis[x]) {
dfs(x, k);
ans[k] += dp[x][0];
}
for(int x : X) vis[x] = 0;
}
return ans;
}
/*
5
0 1 1
0 2 4
0 3 3
2 4 2
*/
/*
4
0 1 5
0 3 5
0 2 10
*/
/*
5
0 1 5
1 2 6
2 3 1
3 4 8
*/
/*
5
0 1 1
0 2 2
0 3 3
0 4 4
*/
//int main(void) {
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//
// freopen("input.txt", "r", stdin);
// int N;
// cin >> N;
// vector<int> U(N), V(N), W(N);
// for(int i = 0; i < N - 1; i++) cin >> U[i] >> V[i] >> W[i];
// vector<ll> ans = minimum_closure_costs(N, U, V, W);
// for(int i = 0; i < N; i++) cout << ans[i] << ' ';
// return 0;
//}
Compilation message (stderr)
In file included from /usr/include/c++/10/cassert:44,
from roads.cpp:2:
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:45:32: warning: comparison of integer expressions of different signedness: 'long long unsigned int' and 'int' [-Wsign-compare]
45 | else assert(A[now].size() + c == k);
| ~~~~~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |