#include <bits/stdc++.h>
using namespace std;
#define v first
#define cost1 second.first
#define cost2 second.second
const int N=200001;
vector <pair<int,pair<int,int>>> adj[N];
vector<int> subTrees[N];
int timer,tin[N],tout[N];
long long totalUp,totalDown,down[N],up[N],maxNotInSub[N],maxDown[N],ans[N];
map<long long ,int> Map;
void dfs(int u, int p, int id) {
tin[u] = timer++;
maxDown[u] = down[u];
subTrees[id].push_back(u);
int newId = id;
for (auto nxt : adj[u]) {
int v = nxt.v;
if (v == p)
continue;
up[v] = up[u] + nxt.cost2;
totalUp += nxt.cost2;
down[v] = down[u] + nxt.cost1;
totalDown += nxt.cost1;
dfs(v, u, p == -1 ? ++newId : newId);
maxDown[u] = max(maxDown[u], maxDown[v]);
}
tout[u] = timer - 1;
Map[down[u]]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i < n; i++) {
int u,v,c1,c2;
cin>>u>>v>>c1>>c2;
u--;
v--;
adj[u].push_back({v,{c1,c2}});
adj[v].push_back({u,{c2,c1}});
}
dfs(0, -1, 0);
for (int col = 1; col < n; col++) {
for (int u : subTrees[col]) {
Map[down[u]]--;
if(Map[down[u]]==0)
Map.erase(down[u]);
}
long long max=Map.rbegin()->first;
for (int u : subTrees[col]) {
Map[down[u]]++;
maxNotInSub[u] = max;
}
}
// System.out.println(Arrays.toString(maxNotInSub));
for (int u = 0; u < n; u++) {
ans[1] = max(ans[1], totalUp - up[u] + down[u]);
long long curr = 0;
// u and root
curr = totalUp + down[u];
ans[2] = max(ans[2], curr);
// u and some child for u
curr = totalUp - up[u] + maxDown[u];
ans[2] = max(ans[2], curr);
// u and some node v such that lca(v,u) = root
curr = totalUp + down[u] + maxNotInSub[u];
ans[2] = max(ans[2], curr);
}
int q;
cin>>q;
while (q-- > 0) {
int t;
cin>>t;
cout<<(totalUp + totalDown -ans[t])<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
575 ms |
39756 KB |
Output is correct |
3 |
Correct |
463 ms |
56312 KB |
Output is correct |
4 |
Correct |
407 ms |
36076 KB |
Output is correct |
5 |
Correct |
721 ms |
45468 KB |
Output is correct |
6 |
Correct |
437 ms |
47356 KB |
Output is correct |
7 |
Correct |
686 ms |
45408 KB |
Output is correct |
8 |
Correct |
434 ms |
56880 KB |
Output is correct |
9 |
Correct |
677 ms |
46036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9736 KB |
Output is correct |
2 |
Incorrect |
552 ms |
39744 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9848 KB |
Output is correct |
2 |
Correct |
575 ms |
39756 KB |
Output is correct |
3 |
Correct |
463 ms |
56312 KB |
Output is correct |
4 |
Correct |
407 ms |
36076 KB |
Output is correct |
5 |
Correct |
721 ms |
45468 KB |
Output is correct |
6 |
Correct |
437 ms |
47356 KB |
Output is correct |
7 |
Correct |
686 ms |
45408 KB |
Output is correct |
8 |
Correct |
434 ms |
56880 KB |
Output is correct |
9 |
Correct |
677 ms |
46036 KB |
Output is correct |
10 |
Correct |
10 ms |
9736 KB |
Output is correct |
11 |
Incorrect |
552 ms |
39744 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |