#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 sum2=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 dfs3(int r,int f,int c=0){
v[r].sum2+=c;
for(auto h:v[r].ch){
if(h.fs!=f){
dfs3(h.fs,r,v[r].sum2+h.sc.fs);
}
}
}
void calc2(){
int mx=0;
for(int i=0;i<n;i++){
if(v[i].ch.size()==1 && v[i].sum>v[mx].sum){
mx=i;
}
}
dfs3(mx,mx);
int z=0;
for(int i=0;i<n;i++){
if(v[i].ch.size()==1 && i!=mx){
ans[2]=max(ans[2],v[i].sum2);
}
}
ans[2]+=v[mx].sum;
}
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;
}
Compilation message
designated_cities.cpp: In function 'void calc2()':
designated_cities.cpp:57:9: warning: unused variable 'z' [-Wunused-variable]
57 | int z=0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
240 ms |
22780 KB |
Output is correct |
3 |
Correct |
336 ms |
30412 KB |
Output is correct |
4 |
Correct |
230 ms |
22732 KB |
Output is correct |
5 |
Correct |
232 ms |
22528 KB |
Output is correct |
6 |
Correct |
260 ms |
24012 KB |
Output is correct |
7 |
Correct |
189 ms |
21560 KB |
Output is correct |
8 |
Correct |
293 ms |
30816 KB |
Output is correct |
9 |
Correct |
124 ms |
20592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
244 ms |
29108 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
240 ms |
22780 KB |
Output is correct |
3 |
Correct |
336 ms |
30412 KB |
Output is correct |
4 |
Correct |
230 ms |
22732 KB |
Output is correct |
5 |
Correct |
232 ms |
22528 KB |
Output is correct |
6 |
Correct |
260 ms |
24012 KB |
Output is correct |
7 |
Correct |
189 ms |
21560 KB |
Output is correct |
8 |
Correct |
293 ms |
30816 KB |
Output is correct |
9 |
Correct |
124 ms |
20592 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Incorrect |
244 ms |
29108 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |