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 "roads.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const ll N = 1e5 + 5, oo = 1e18 + 7, mod = 1e9 + 7;
ll n;
set<ii> Adj[N];
ll k;
ll dp[N][2];
bool vis[N];
map<ii, int> mp;
int cnt[N];
vector<ll> bit1[N], bit2[N];
void upd(int u, int pos, int val){
//cout << "UPD " << u << " " << pos << " " << val << "\n";
int pos1 = pos, pos2 = pos;
for(; pos1 < bit1[u].size(); pos1 += pos1 & -pos1) bit1[u][pos1]++;
for(; pos2 < bit2[u].size(); pos2 += pos2 & -pos2) bit2[u][pos2] += val;
}
ll get(int u, int pos){
int itr = 0;
ll ans = 0;
for(int i = 19; i >= 0; i--){
if(itr + (1LL << i) >= bit1[u].size()) continue;
if(pos >= bit1[u][itr + (1LL << i)]){
pos -= bit1[u][itr + (1LL << i)];
ans += bit2[u][itr + (1LL << i)];
itr += (1LL << i);
}
}
return ans;
}
void dfs(int u, int p, int we){
vis[u] = 1;
//int we = 0;
for(auto it : Adj[u]){
int v = it.fi;
if(v == p){
//we = it.se;
continue;
}
//assert(Adj[v].size() > k);
//if(Adj[v].size() <= k) continue;
dfs(v, u, it.se);
}
vector<ll> vc;
ll sum = 0;
for(auto it : Adj[u]){
int v = it.fi;
if(v == p) continue;
//cout << "EDG " << u << " " << v << "\n";
sum += dp[v][0];
if(dp[v][0] > dp[v][1]) vc.pb(dp[v][0] - dp[v][1]);
}
sort(vc.begin(), vc.end());
reverse(vc.begin(), vc.end());
sum += get(u, cnt[u]);
ll sum2 = 0, opt = -oo;
//cout << cnt[u] << "\n";
for(int i = 0; i <= vc.size(); i++){
if((k - 1) < i) break;
opt = max(opt, sum2 + get(u, k - 1 - i));
if(i == vc.size()) break;
sum2 += vc[i];
}
//cout << "OK " << cnt[u] << " " << get(u, cnt[u]) << " " << sum << " " << opt << "\n";
dp[u][1] = sum - opt;
sum2 = 0, opt = -oo;
//cout << vc.size() << "\n";
for(int i = 0; i <= vc.size(); i++){
if(k < i) break;
opt = max(opt, sum2 + get(u, k - i));
if(i == vc.size()) break;
sum2 += vc[i];
}
dp[u][0] = sum - opt + we;
//cout << u << " " << dp[u][0] << " " << dp[u][1] << "\n";
/*
for(int i = 0; i < min((ll)vc.size(), (ll)k - 1); i++){
sum2 += max(0LL, vc[i]);
}*/
/*
dp[u][0] = sum - sum2 - (((ll)vc.size() < k || !k) ? 0 : max(0LL, vc[k - 1])) + we;
dp[u][1] = sum - sum2;*/
//cout << u << " " << dp[u][0] << " " << dp[u][1] << "\n";
}
bool ck_star(){
int tol = 0;
for(int i = 1; i <= n; i++) tol += (Adj[i].size() >= 2);
return (tol == 1);
}
bool ck_line(){
for(int i = 1; i <= n; i++) if(Adj[i].size() > 2) return 0;
return 1;
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W){
n = N;
//cout << U.size() << " " << V.size() << " " << W.size() << "\n";
//exit(0);
for(int i = 0; i < (n - 1); i++){
U[i]++, V[i]++;
Adj[U[i]].insert({V[i], W[i]});
Adj[V[i]].insert({U[i], W[i]});
}
set<ii> nodes;
for(int i = 1; i <= n; i++) nodes.insert({Adj[i].size(), i});
for(int i = 1; i <= n; i++){
bit1[i].resize(Adj[i].size() + 1);
bit2[i].resize(Adj[i].size() + 1);
int cnt = 0;
vector<ii> temp;
for(auto it : Adj[i]) temp.pb({it.se, it.fi});
sort(temp.begin(), temp.end(), greater<ii>());
for(auto it : temp){
if(it.fi <= 0) break;
cnt++;
mp[{i, it.se}] = cnt;
}
}
/*
if(ck_star()){
sort(W.begin(), W.end(), greater<int>());
vector<ll> ans(n);
ll sum = 0;
for(int i = n - 1; i >= 0; i--){
ans[i] = sum;
if(i) sum += W[i - 1];
}
return ans;
}*/
int mx_deg = 0;
for(int i = 1; i <= n; i++) mx_deg = max(mx_deg, (int)Adj[i].size());
vector<ll> ans;
//cout << "OK\n";
for(int i = 0; i < mx_deg; i++){
k = i;
while(nodes.size() && (*nodes.begin()).fi <= k){
int u = (*nodes.begin()).se;
for(auto it : Adj[u]){
int v = it.fi, w = it.se;
cnt[v]++;
if(mp.find({v, u}) != mp.end()) upd(v, mp[{v, u}], w);
//Adj[u].erase({v, w});
Adj[v].erase({u, w});
}
Adj[u].clear();
//cout << u << " " << i << "\n";
nodes.erase(nodes.begin());
}
for(auto it : nodes) vis[it.se] = 0;
ll answ = 0;
//cout << i << "\n";
for(auto it : nodes){
//assert(Adj[it.se].size() > k);
if(!vis[it.se]){
//cout << i << " " << it.se << "\n";
dfs(it.se, it.se, 0);
answ += min(dp[it.se][0], dp[it.se][1]);
}
}
ans.pb(answ);
/*
k = i;
dfs(1, 1);
ans.pb(min(dp[1][0], dp[1][1]));*/
}
for(int i = mx_deg; i < n; i++) ans.pb(0);
return ans;
}
/*
void process(){
int n;
vector<int> a, b, c;
cin >> n;
for(int i = 1; i <= 3 * n - 3; i++){
int x;
cin >> x;
if(i <= (n - 1)) a.pb(x);
else if(i <= 2 * (n - 1)) b.pb(x);
else c.pb(x);
}
vector<long long> temp = minimum_closure_costs(n, a, b, c);
for(auto it : temp) cout << it << " ";
}
signed main(){
ios_base::sync_with_stdio(0);
freopen("roads.inp", "r", stdin);
freopen("roads.out", "w", stdout);
process();
}*/
Compilation message (stderr)
roads.cpp: In function 'void upd(int, int, int)':
roads.cpp:33:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(; pos1 < bit1[u].size(); pos1 += pos1 & -pos1) bit1[u][pos1]++;
| ~~~~~^~~~~~~~~~~~~~~~
roads.cpp:34:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(; pos2 < bit2[u].size(); pos2 += pos2 & -pos2) bit2[u][pos2] += val;
| ~~~~~^~~~~~~~~~~~~~~~
roads.cpp: In function 'long long int get(int, int)':
roads.cpp:41:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(itr + (1LL << i) >= bit1[u].size()) continue;
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 0; i <= vc.size(); i++){
| ~~^~~~~~~~~~~~
roads.cpp:82:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if(i == vc.size()) break;
| ~~^~~~~~~~~~~~
roads.cpp:89:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = 0; i <= vc.size(); i++){
| ~~^~~~~~~~~~~~
roads.cpp:92:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | if(i == vc.size()) break;
| ~~^~~~~~~~~~~~
# | 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... |