#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'
+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 2e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,q;
vector<pair<int, ii>> adj[N];
int ans[N], sum = 0, mn = inf, add = inf, root = 1;
int dfs(int u, int p, int cur)
{
int mx1 = -inf, mx2 = -inf;
for (auto i : adj[u])
{
int v = i.fi, a = i.se.fi, b = i.se.se;
if (v == p) continue;
sum += a;
int tmp = dfs(v, u, cur+b-a) + a;
if (mx1 < tmp) mx2 = mx1, mx1 = tmp;
else if (mx2 < tmp) mx2 = tmp;
}
add = min(add, cur);
if (mn > cur-mx1-mx2 && adj[u].size() != 1)
{
root = u;
mn = cur-mx1-mx2;
}
return max(mx1, 0ll);
}
vector<int> delta[N];
int best[N];
void dfs1(int u, int p)
{
int mxsz = 0;
int id = -1, id1 = -1, id2 = -1, mx1 = -inf, mx2 = -inf;
for (auto i : adj[u])
{
int v = i.fi, a = i.se.fi, b = i.se.se;
if (v == p) continue;
sum += a;
dfs1(v, u);
best[v] += a;
if (delta[v].size() > mxsz) mxsz = delta[v].size(), id = v;
if (best[v] > best[u])
{
id2 = id1, mx2 = mx1;
mx1 = best[v], id1 = v;
best[u] = mx1;
} else if (best[v] > mx2)
{
mx2 = best[v], id2 = v;
}
}
if (id != -1) swap(delta[u], delta[id]);
for (auto i : adj[u])
{
int v = i.fi, a = i.se.fi, b = i.se.se;
if (v == p) continue;
if (v != id)
{
for (auto j : delta[v]) delta[u].pb(j);
}
if (v != id1 && (p != -1 || v != id2)) delta[u].pb(best[v]);
}
if (!p && id2 != -1) best[u] += mx2;
}
void solve()
{
cin>>n;
for (int i = 1; i < n; i++)
{
int u,v,a,b;
cin>>u>>v>>a>>b;
adj[u].pb({v,{a,b}}); adj[v].pb({u,{b,a}});
}
for (int i = 1; i <= n; i++) if (adj[i].size() > 1) {root = i; break;}
dfs(root, 0, 0);
ans[1] = sum + add;
sum = 0;
dfs1(root, 0);
ans[2] = sum - best[root];
sort(all(delta[root]));
for (int i = 3; i <= n; i++)
{
if (!delta[root].size()) ans[i] = ans[i-1];
else
{
ans[i] = ans[i-1] - delta[root].back();
delta[root].pop_back();
}
}
cin>>q;
while (q--)
{
int x; cin>>x;
cout<<ans[x]<<endl;
}
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
# | 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... |