#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;
timer=0;
for(int u=0;u<n;u++){
down[u]=0;
up[u]=0;
maxNotInSub[u]=0;
}
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 |
13 ms |
9720 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
536 ms |
39528 KB |
Output is correct |
3 |
Correct |
479 ms |
50028 KB |
Output is correct |
4 |
Correct |
453 ms |
31364 KB |
Output is correct |
5 |
Correct |
696 ms |
39204 KB |
Output is correct |
6 |
Correct |
427 ms |
41072 KB |
Output is correct |
7 |
Correct |
746 ms |
39080 KB |
Output is correct |
8 |
Correct |
433 ms |
50600 KB |
Output is correct |
9 |
Correct |
665 ms |
39712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Incorrect |
572 ms |
39408 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
9720 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
536 ms |
39528 KB |
Output is correct |
3 |
Correct |
479 ms |
50028 KB |
Output is correct |
4 |
Correct |
453 ms |
31364 KB |
Output is correct |
5 |
Correct |
696 ms |
39204 KB |
Output is correct |
6 |
Correct |
427 ms |
41072 KB |
Output is correct |
7 |
Correct |
746 ms |
39080 KB |
Output is correct |
8 |
Correct |
433 ms |
50600 KB |
Output is correct |
9 |
Correct |
665 ms |
39712 KB |
Output is correct |
10 |
Correct |
10 ms |
9720 KB |
Output is correct |
11 |
Incorrect |
572 ms |
39408 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
9720 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |