#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (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 LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
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"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = MASK(18);
int n;
vector<pair<int, int>> graph[N];
int w[N * 2];
ll ans[N];
namespace Sub1{
ll get_sauce(int u, int p){
ll ans = 0;
for(pair<int,int> v: graph[u]) if (v.first != p){
ans += get_sauce(v.first, u) + w[v.second];
}
return ans;
}
ll ans;
void dfs(int u, int p, ll cur){
minimize(ans, cur);
for(pair<int, int> v: graph[u]) if (v.first != p){
dfs(v.first, u, cur - w[v.second] + w[v.second ^ N]);
}
}
ll get_ans(){
ans = get_sauce(1, 0);
dfs(1, 0, ans);
return ans;
}
}
namespace Sub2{
pair<int, int> ans = {-1, -1};
ll optimal_cost = 0;
pair<ll, int> go(int u, int p, ll d){
pair<ll, int> ma = {0, u};
vector<pair<ll, int>> child;
for(pair<int, int> v: graph[u]) if (v.first != p){
pair<ll,int> cur = go(v.first, u, d - w[v.second] + w[v.second ^ N]);
cur.first += w[v.second];
child.push_back(cur);
maximize(ma, cur);
}
sort(ALL(child), greater<pair<ll, int>>());
if (child.size() >= 1){
ll cur_cost = d - child[0].first;
if (minimize(optimal_cost, cur_cost))
ans = {u, child[0].second};
}
if (child.size() >= 2){
ll cur_cost = d - child[0].first - child[1].first;
if (minimize(optimal_cost, cur_cost))
ans = {child[0].second, child[1].second};
}
return ma;
}
pair<int, int> get_diameter(){
go(1, 0, 0);
return ans;
}
}
namespace Sub3{
pair<int,int> parent[N];
void dfs(int u, int p){
for(pair<int,int> v: graph[u]) if (v.first != p){
parent[v.first] = {u, v.second};
dfs(v.first, u);
}
}
ll dp[N];
void go(int u, int p){
for(pair<int, int> v: graph[u])if (v.first != p){
go(v.first, u);
maximize(dp[u], dp[v.first] + w[v.second]);
}
}
vector<ll> sigma;
void prune_height(int u, int p, ll height){
vector<pair<int, int>> child;
for(pair<int,int> v: graph[u]) if (v.first != p){
child.push_back(v);
}
if (child.size() == 0){
sigma.push_back(height);
}
else{
sort(ALL(child), [](pair<int, int> x, pair<int, int> y){
return dp[x.first] > dp[y.first];
});
pair<int, int> v = child[0];
prune_height(v.first, u, height + w[child[0].second]);
for(int i = 1; i < child.size(); ++i){
pair<int, int> v = child[i];
prune_height(v.first, u, w[child[i].second]);
}
}
}
void tom_foolery(pair<int, int> e){
dfs(e.first, 0);
int v = e.second;
while(v != e.first){
pair<int, int> cur = parent[v];
w[cur.second] = w[cur.second ^ N] = 0;
v = cur.first;
}
go(e.first, 0);
prune_height(e.first, 0, 0);
ll sum = 0;
for(ll i: sigma) sum += i;
sort(ALL(sigma));
for(int i = 2; i<= n; ++i){
ans[i] = sum;
if (sigma.size()) {
sum -= sigma.back();
sigma.pop_back();
}
}
}
}
void solve(){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int u, v, c, d; cin >> u >> v >> c >> d;
graph[u].push_back({v, i});
graph[v].push_back({u, i + N});
w[i] = c;
w[i + N] = d;
}
ans[1] = Sub1::get_ans();
pair<int, int> e = Sub2::get_diameter();
Sub3::tom_foolery(e);
int q; cin >> q;
while(q--){
int x; cin >> x;
cout << ans[x] << "\n";
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# | 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... |