This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <stack>
#include <set>
#include <map>
#include <assert.h>
#include <queue>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define ll long long
#define pii pair<int, long long>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
#define maxn 100005
#define mod 1000000007
using namespace std;
using namespace __gnu_pbds;
void debug() {
cout << endl;
}
template<class T, class ...U> void debug(T a, U ...b) {
cout << a << " ";debug(b...);
}
template<class T> void pary(T l, T r) {
while (l != r) {
cout << *l << " ";
l++;
}
cout << endl;
}
//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//gp_hash_table<int, int> mp;
//find by order, order of key
#include "roads.h"
unsigned ran() {
static unsigned x = 19;
return ++(x *= 0xdefaced);
}
struct treap{
struct node{
int pri, siz;
ll key = 0, sum;
node * lef = NULL, * rig = NULL;
node() {key = 0, siz = 1, sum = 0, pri = ran(), lef = rig = NULL;}
node(ll x) {key = x, pri = ran(), siz = 1, sum = key, lef = rig = NULL;}
};
void update(node * a) {
if (a) {
a->siz = 1 + (a->lef ? a->lef->siz : 0) + (a->rig ? a->rig->siz : 0);
a->sum = a->key + (a->lef ? a->lef->sum : 0) + (a->rig ? a->rig->sum : 0);
}
}
node * merge(node * a, node * b) {
if (!a) return b;
if (!b) return a;
if (a->pri < b->pri) {
a->rig = merge(a->rig, b);
update(a);
return a;
} else {
b->lef = merge(a, b->lef);
update(b);
return b;
}
}
void split (node * n, ll val, node * &a, node * &b, bool bysize) {
if (!n) {
a = NULL, b = NULL;
return;
}
bool check = n->key < val;
int ls = (n->lef ? n->lef->siz : 0) + 1;
if (bysize) check = ls <= val;
if (check) {
a = n;
split(n->rig, val - (bysize ? ls : 0), a->rig, b, bysize);
update(a);
} else {
b = n;
split(n->lef, val, a, b->lef, bysize);
update(b);
}
}
void print(node * cur) {
if (!cur) return;
print(cur->lef);
debug(cur->key, cur->siz, cur->sum);
print(cur->rig);
}
node * root = NULL;
void ins(ll val) {
node * a, * b = new node(val), * c;
split(root, val, a, c, 0);
root = merge(a, merge(b, c));
}
void del(ll val) {
node * a, * b, * c, * d;
split(root, val, a, b, 0);
split(b, 1, c, d, 1);
root = merge(a, d);
}
ll query(int num) {
node * a, * b;
split(root, num, a, b, 1);
ll ret = a ? a->sum : 0;
root = merge(a, b);
return ret;
}
} wei[maxn];
vector<pii> adj[maxn];
int deg[maxn];
bool mark[maxn], vis[maxn];
ll dp[maxn][2];
void dfs(int n, int par, int k) {
dp[n][0] = dp[n][1] = 0;
vis[n] = 1;
ll orig = 0;
vector<ll> val;
int num = deg[n] - k;
for (auto v:adj[n]) {
if (v.ff != par) {
if (!mark[v.ff]) break;
dfs(v.ff, n, k);
orig += min(dp[v.ff][0], dp[v.ff][1]);
if (dp[v.ff][0] < dp[v.ff][1]) {
val.push_back(max(0LL, dp[v.ff][1] - dp[v.ff][0]));
} else {
num--;
}
} else {
dp[n][1] = v.ss;
}
}
for (ll i:val) wei[n].ins(i);
dp[n][0] = orig + wei[n].query(max(num, 0));
dp[n][1] += orig + wei[n].query(max(num - 1, 0));
for (ll i:val) wei[n].del(i);
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
//if (N >= 5000) return vector<ll> (N, 0);
ll tot = 0;
vector<int> v;
for (int i = 0;i < N;i++) v.push_back(i);
for (int i = 0;i < N - 1;i++) {
adj[U[i]].push_back(make_pair(V[i], W[i]));
adj[V[i]].push_back(make_pair(U[i], W[i]));
deg[U[i]]++, deg[V[i]]++;
tot += W[i];
}
sort(v.begin(), v.end(), [&] (int i, int j) {return deg[i] > deg[j];});
for (int i = 0;i < N;i++) sort(adj[i].begin(), adj[i].end(), [&] (pii i, pii j){return deg[i.ff] > deg[j.ff];});
vector<ll> ans;
for (int k = 0;k <= N - 1;k++) {
vector<int> f;
for (int i = 0;i < N;i++) {
if (deg[v[i]] > k) mark[v[i]] = 1, f.push_back(v[i]);
else break;
}
ll val = 0;
for (int i:f) {
if (!vis[i]) {
dfs(i, -1, k);
val += dp[i][0];
}
}
ans.push_back(val);
for (int i:f) {
mark[i] = 0, vis[i] = 0;
if (deg[i] == k + 1) {
for (auto j:adj[i]) wei[j.ff].ins(j.ss);
}
}
}
return ans;
}
/*
int main() {
int q;
cin >> q;
while (q--) {
int type, val;
cin >> type >> val;
if (type == 1) {
bst.ins(val);
} else if (type == 2) {
bst.del(val);
} else {
cout << bst.query(val) << endl;
}
bst.print(bst.root);
}
}
5
0 1 1
0 2 4
0 3 3
2 4 2
4
0 1 5
2 0 10
0 3 5
10
0 1 1
0 2 1
2 4 1
2 3 1
3 5 1
3 6 1
5 7 1
2 8 1
2 9 1
10
0 1 1
0 2 1
0 3 1
1 4 1
1 5 1
2 6 1
2 7 1
3 8 1
3 9 1
*/
# | 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... |