#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
//mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double wow = (double) ((ull) rng()) / ((ull)(0-1));
return wow * (r - l) + l;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
#include "roads.h"
const ll INF = 1e18 + 69;
struct FenwickTree{
int n;
vector<ll> a;
FenwickTree(int _n = 1){
n = _n;
a.resize(n+1);
}
void update(int i, ll v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
ll get(int i){
ll ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int binary_lift(ll v){
int idx = 0, p = MASK(logOf(n));
while(p > 0){
if (idx + p <= n && v > a[idx + p]){
v -= a[idx += p];
}
p >>= 1;
}
return idx + 1;
}
};
struct SumSet{
int n;
vector<pair<int, int>> val;
FenwickTree bit, sum;
SumSet(vector<pair<int, int>> sigma){
val = sigma;
n = val.size();
bit = FenwickTree(n);
sum = FenwickTree(n);
sort(ALL(val));
}
void add(pair<int, int> u){
int idx = lower_bound(ALL(val), u) - val.begin() + 1;
bit.update(idx, 1);
sum.update(idx, u.first);
}
ll get_sum(int k){
if (k <= 0) return 0;
int idx = bit.binary_lift(k);
if (idx == n + 1) return INF;
return sum.get(idx);
}
};
const int N = 1e5 + 69;
int n;
vector<pair<int, int>> graph[N], active_graph[N];
vector<SumSet> S;
vector<int> good_vertices[N];
vector<array<int, 3>> good_edges[N], bad_edges[N];
int visited[N];
ll dp[N][2];
void dfs(int u, int p, int k){
visited[u] = 1;
dp[u][0] = dp[u][1] = INF;
ll sum = 0;
int sz = (int)graph[u].size() - k;
vector<ll> changes;
for(pair<int, int> v: active_graph[u]) if (!visited[v.second]){
dfs(v.second, u, k);
if (dp[v.second][0] >= INF){
sz--;
sum += dp[v.second][1];
}
else{
sum += dp[v.second][0];
if (dp[v.second][1] < INF)
changes.push_back(dp[v.second][1] - dp[v.second][0] + v.first);
}
}
sort(ALL(changes));
for(int t = 0; t <= 1; ++t){
minimize(dp[u][t], sum + S[u].get_sum(sz - t));
ll _sum = sum;
for(int i = 0; i < (int) changes.size(); ++i){
_sum += changes[i];
minimize(dp[u][t], _sum + S[u].get_sum(sz - t - i - 1));
}
}
}
vector<ll> minimum_closure_costs(int _n, vector<int> U, vector<int> V, vector<int> W) {
n = _n;
for(int i= 0; i<n-1; ++i){
int u = U[i], v = V[i], w = W[i];
graph[u].push_back({w, v});
graph[v].push_back({w, u});
}
for(int i = 0; i < n; ++i){
S.push_back(SumSet(graph[i]));
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < (int) graph[i].size(); ++j)
good_vertices[j].push_back(i);
}
for(int i = 0; i< n-1; ++i){
int mi = min(graph[U[i]].size(), graph[V[i]].size());
array<int, 3> current_edge = {{U[i], V[i], W[i]}};
for(int j = 0; j < mi; ++j)
good_edges[j].push_back(current_edge);
bad_edges[mi].push_back(current_edge);
}
vector<ll> ans(n);
for(int i = 0; i< n; ++i){
for(auto j: bad_edges[i]){
int u, v, w;
u = j[0], v = j[1], w = j[2];
S[u].add({w, v});
S[v].add({w, u});
}
for(auto j: good_edges[i]){
int u, v, w;
u = j[0], v = j[1], w = j[2];
active_graph[u].push_back({w, v});
active_graph[v].push_back({w, u});
}
for(int j: good_vertices[i]) if (visited[j] == 0){
dfs(j, 0, i);
ans[i] += dp[j][0];
}
for(int j: good_vertices[i]) {
active_graph[j].clear();
visited[j] = false;
}
}
return ans;
}
# | 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... |