#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
struct no{
vector<pair<int,pair<int,int> > > ch;
int sum=0;
};
int n;
int zz=0;
vector<no> v;
vector<int> ans;
void dfs(int r,int f){
for(auto h:v[r].ch){
if(h.fs!=f){
dfs(h.fs,r);
v[r].sum+=v[h.fs].sum+h.sc.sc;
}
}
}
void dfs2(int r,int f,int z=0){
v[r].sum+=z;
ans[1]=max(ans[1],v[r].sum);
for(auto h:v[r].ch){
if(h.fs!=f){
dfs2(h.fs,r,h.sc.fs+v[r].sum-v[h.fs].sum-h.sc.sc);
}
}
}
void calc1(){
dfs(0,0);
ans[1]=max(ans[1],v[0].sum);
dfs2(0,0);
}
void calc2(){
}
signed main(){
AquA;
cin >> n;
v.resize(n);
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
a--;
b--;
int c,d;
cin >> c >> d;
zz+=c+d;
v[a].ch.push_back({b,{c,d}});
v[b].ch.push_back({a,{d,c}});
}
ans.resize(n+1);
calc1();
calc2();
int q;
cin >> q;
for(int i=0;i<q;i++){
int a;
cin >> a;
cout << zz-ans[a] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
204 ms |
27736 KB |
Output is correct |
3 |
Correct |
228 ms |
35268 KB |
Output is correct |
4 |
Correct |
185 ms |
26180 KB |
Output is correct |
5 |
Correct |
197 ms |
27412 KB |
Output is correct |
6 |
Correct |
210 ms |
28860 KB |
Output is correct |
7 |
Correct |
163 ms |
26536 KB |
Output is correct |
8 |
Correct |
242 ms |
35716 KB |
Output is correct |
9 |
Correct |
126 ms |
25508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
204 ms |
27736 KB |
Output is correct |
3 |
Correct |
228 ms |
35268 KB |
Output is correct |
4 |
Correct |
185 ms |
26180 KB |
Output is correct |
5 |
Correct |
197 ms |
27412 KB |
Output is correct |
6 |
Correct |
210 ms |
28860 KB |
Output is correct |
7 |
Correct |
163 ms |
26536 KB |
Output is correct |
8 |
Correct |
242 ms |
35716 KB |
Output is correct |
9 |
Correct |
126 ms |
25508 KB |
Output is correct |
10 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |