이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 2e5 + 7;
const int Mod = 1e9 + 2022; ///loonf mod sai
const ll INF = 1e14;
const ll BASE = 137;
const int szBL = 350;
struct edges {
int u, v, w;
};
int n;
vector<int> edges;
vector<pii> ke[N];
namespace sub4 {
int Wp[N];
ll dp[N][2];
void dfs (int u, int p, int X) {
vector<ll> vec;
ll Tot = 0;
iter (&id, ke[u]) {
int v, w;
tie(v, w) = id;
if (v != p) {
Wp[v] = w;
dfs(v, u, X);
Tot += dp[v][0];
vec.pb({-dp[v][0] + dp[v][1]});
}
}
int m = SZ(ke[u]);
sort (ALL(vec));
dp[u][0] = Tot;
dp[u][1] = Tot + Wp[u];
rep (i, 0, SZ(vec) - 1) {
if (m - i - 1 >= X) dp[u][0] += vec[i];
else dp[u][0] += min(0LL, vec[i]);
if (m - i - 2 >= X) dp[u][1] += vec[i];
else dp[u][1] += min(0LL, vec[i]);
}
if (m - SZ(vec) > X) dp[u][0] = INF;
}
void solution() {
rep (X, 0, n - 1) {
dfs(1, 0, X);
cout << dp[1][0] <<" ";
}
}
}
ll minimum_closure_costs (int _n, vector<int> _u, vector<int> _v, vector<int> _w) {
n = _n;
rep (i, 1, n - 1) {
int u = _u[i - 1], v = _v[i - 1], w = _w[i - 1];
ke[u].pb({v, w});
ke[v].pb({u, w});
}
if (n <= 2000) sub4 :: solution();
}
void solution() {
cin >> n;
rep (i, 1, n - 1) {
int u, v, w;
cin >> u >> v >> w;
ke[u].pb({v, w});
ke[v].pb({u, w});
}
if (n <= 2000) sub4 :: solution();
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
//int main () {
//// file("noel");
// ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int num_Test = 1;
//// cin >> num_Test;
// while (num_Test--)
// solution();
//}
/*
no bug +8
chu y break hay return se lam hong logic
xet transition cua i va i + 1
construct ket qua
10 2
1 2 4 3 2 1 2 8 6 8
1 10 6 7
1 10 4 6
10 1
34 51 53 15 29 41 97 96 36 5
2 8 7 8
*/
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'long long int minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:81:1: warning: no return statement in function returning non-void [-Wreturn-type]
81 | }
| ^
# | 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... |