#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
4988 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
9 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Execution timed out |
2011 ms |
18656 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Execution timed out |
2013 ms |
23776 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
4988 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
9 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Execution timed out |
2029 ms |
5248 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Execution timed out |
2011 ms |
18656 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
4988 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
9 ms |
5120 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
7 ms |
5120 KB |
Output is correct |
12 |
Correct |
7 ms |
4992 KB |
Output is correct |
13 |
Execution timed out |
2011 ms |
18656 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |