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 <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
typedef long long ll;
struct edge
{
int to, toy, fromy;
};
vector<edge> v[Nmax];
bool used[Nmax];
int t[Nmax];
ll cost[Nmax], act_cost[Nmax], ans[Nmax];
int n;
ll dfs(int node, int dad = 0)
{
t[node] = dad;
ll ans = 0;
for(auto it : v[node])
if(it.to != dad)
{
cost[it.to] = cost[node] + it.toy;
ans += it.fromy + dfs(it.to, node);
}
return ans;
}
void solve(int root)
{
cost[root] = 0;
ll curr = dfs(root);
int i, j;
for(i=1; i<=n; ++i) used[i] = 0, act_cost[i] = cost[i];
ans[1] = max(ans[1], curr);
for(i=2; i<=n; ++i)
{
int now = 1;
for(j=1; j<=n; ++j)
if(!used[j] && act_cost[j] > act_cost[now]) now = j;
curr += act_cost[now];
ans[i] = max(ans[i], curr);
while(now)
{
used[now] = 1;
now = t[now];
}
for(j=1; j<=n; ++j)
{
int x = j;
while(!used[x]) x = t[x];
act_cost[j] = cost[j] - cost[x];
}
}
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
ll sum = 0;
int i;
cin >> n;
for(i=1; i<n; ++i)
{
int x, y, c1, c2;
cin >> x >> y >> c1 >> c2;
v[x].push_back({y, c1, c2});
v[y].push_back({x, c2, c1});
sum += c1 + c2;
}
for(i=1; i<=n; ++i) solve(i);
int q;
cin >> q;
for(i=1; i<=q; ++i)
{
int x;
cin >> x;
cout << sum - ans[x] << '\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... |