#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 MaxCurr = 0;
int query[Nmax];
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;
}
ll get_best(int node, int dad, ll now)
{
ll best = now;
for(auto it : v[node])
if(it.to != dad)
best = max(best, get_best(it.to, node, now + it.toy - it.fromy));
return best;
}
void solve0(int root)
{
cost[root] = 0;
ll curr = dfs(root);
MaxCurr = max(MaxCurr, curr);
}
void solve1(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;
}
int q;
cin >> q;
for(i=1; i<=q; ++i) cin >> query[i];
if(q == 1 && query[1] == 1)
{
cout << sum - get_best(1, 0, dfs(1)) << '\n';
return 0;
}
for(i=1; i<=n; ++i) solve1(i);
for(i=1; i<=q; ++i)
cout << sum - ans[query[i]] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 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 |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
299 ms |
17540 KB |
Output is correct |
3 |
Correct |
350 ms |
28380 KB |
Output is correct |
4 |
Correct |
302 ms |
17636 KB |
Output is correct |
5 |
Correct |
267 ms |
17396 KB |
Output is correct |
6 |
Correct |
344 ms |
19320 KB |
Output is correct |
7 |
Correct |
285 ms |
17460 KB |
Output is correct |
8 |
Correct |
384 ms |
28816 KB |
Output is correct |
9 |
Correct |
225 ms |
17896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4992 KB |
Output is correct |
2 |
Execution timed out |
2053 ms |
23756 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 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 |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
4992 KB |
Output is correct |
13 |
Execution timed out |
2051 ms |
5260 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
299 ms |
17540 KB |
Output is correct |
3 |
Correct |
350 ms |
28380 KB |
Output is correct |
4 |
Correct |
302 ms |
17636 KB |
Output is correct |
5 |
Correct |
267 ms |
17396 KB |
Output is correct |
6 |
Correct |
344 ms |
19320 KB |
Output is correct |
7 |
Correct |
285 ms |
17460 KB |
Output is correct |
8 |
Correct |
384 ms |
28816 KB |
Output is correct |
9 |
Correct |
225 ms |
17896 KB |
Output is correct |
10 |
Correct |
6 ms |
4992 KB |
Output is correct |
11 |
Execution timed out |
2053 ms |
23756 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
4992 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 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 |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
4992 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
299 ms |
17540 KB |
Output is correct |
14 |
Correct |
350 ms |
28380 KB |
Output is correct |
15 |
Correct |
302 ms |
17636 KB |
Output is correct |
16 |
Correct |
267 ms |
17396 KB |
Output is correct |
17 |
Correct |
344 ms |
19320 KB |
Output is correct |
18 |
Correct |
285 ms |
17460 KB |
Output is correct |
19 |
Correct |
384 ms |
28816 KB |
Output is correct |
20 |
Correct |
225 ms |
17896 KB |
Output is correct |
21 |
Correct |
6 ms |
4992 KB |
Output is correct |
22 |
Execution timed out |
2053 ms |
23756 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |