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 "roads.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1e5 + 69;
const ll INF = 1e18 + 69;
int n, k;
vector<pair<int, int>> graph[N];
namespace Sub1{
bool check(){
return graph[0].size() == n-1;
}
vector<ll> solve(){
vector<ll> ans;
for(pair<int, int> j: graph[0]) ans.push_back(j.second);
ans.push_back(0);
sort(ALL(ans));
for(int i = 1; i < ans.size(); ++i) ans[i] += ans[i-1];
reverse(ALL(ans));
return ans;
}
}
namespace Sub3{
ll dp[N][2];
void go(int u, int p){
dp[u][0] = dp[u][1] = 0;
vector<ll> diff;
for(pair<int, int> v: graph[u]) if (v.first != p){
go(v.first, u);
dp[u][0] += dp[v.first][0];
dp[u][1] += dp[v.first][0];
diff.push_back(dp[v.first][1] + v.second - dp[v.first][0]);
}
sort(ALL(diff));
for(int i = 0; i< diff.size(); ++i){
if (diff[i] >= 0 && diff.size() - i <= k) break;
dp[u][1] += diff[i];
}
for(int i = 0; i< diff.size(); ++i){
if (diff[i] >= 0 && diff.size() - i < k) break;
dp[u][0] += diff[i];
}
}
vector<ll> solve(){
vector<ll> ans(n);
for(int i = 0; i < n; ++i) for(pair<int, int> j: graph[i]) if (j.first > i) ans[0] += j.second;
for(int i = 1; i<n; ++i){
k = i;
go(0, 0);
ans[i] = dp[0][1];
if (ans[i] == 0) break;
}
return ans;
}
}
vector<ll> minimum_closure_costs(int _n, vector<int> U, vector<int> V, vector<int> W) {
n = _n;
for(int i= 0; i<U.size(); ++i){
int u = U[i], v = V[i], w = W[i];
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}
if (Sub1::check()) return Sub1::solve();
return Sub3::solve();
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int n; cin >> n;
// vector<int> U, V, W;
// for(int i = 1; i<n; ++i){
// int u, v, w; cin >> u >> v >> w;
// U.push_back(u); V.push_back(v); W.push_back(w);
// }
// vector<ll> ans = minimum_closure_costs(n, U, V, W);
// printArr(ans);
// return 0;
// }
Compilation message (stderr)
roads.cpp: In function 'bool Sub1::check()':
roads.cpp:54:32: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | return graph[0].size() == n-1;
| ~~~~~~~~~~~~~~~~^~~~~~
roads.cpp: In function 'std::vector<long long int> Sub1::solve()':
roads.cpp:62:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 1; i < ans.size(); ++i) ans[i] += ans[i-1];
| ~~^~~~~~~~~~~~
roads.cpp: In function 'void Sub3::go(int, int)':
roads.cpp:81:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i = 0; i< diff.size(); ++i){
| ~^~~~~~~~~~~~~
roads.cpp:82:49: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
82 | if (diff[i] >= 0 && diff.size() - i <= k) break;
| ~~~~~~~~~~~~~~~~^~~~
roads.cpp:85:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i = 0; i< diff.size(); ++i){
| ~^~~~~~~~~~~~~
roads.cpp:86:49: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
86 | if (diff[i] >= 0 && diff.size() - i < k) break;
| ~~~~~~~~~~~~~~~~^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:107:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int i= 0; i<U.size(); ++i){
| ~^~~~~~~~~
# | 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... |