//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2e5+5;
vector<pair<ll,pair<ll,ll>>> adj[mxn];
ll n,q;
vector<ll> ans(mxn);
map<pair<int,int>,int> mp;
ll dp1[mxn];
ll dp2[mxn];
void dfs1(int v,int p=-1){;
for(auto u:adj[v]){
if(u.f==p) continue;
dfs1(u.f,v);
dp1[v]+=dp1[u.f];
if(mp.count({u.f,v})) dp1[v]+=u.s.f;
else dp1[v]+=u.s.s;
}
}
void dfs2(int v,int p=-1,ll val=0){
dp2[v]=dp1[v]+val;
for(auto u:adj[v]){
if(u.f==p) continue;
ll sum=dp2[v];
if(mp.count({u.f,v})){
sum-=u.s.f+dp1[u.f];
sum+=u.s.s;
}
else{
sum-=u.s.s+dp1[u.f];
sum+=u.s.f;
}
dfs2(u.f,v,sum);
}
}
ll cal1(){
dfs1(1);
dfs2(1);
return *max_element(dp2+1,dp2+n+1);
}
int main() {_
cin>>n;
ll sum=0;
for(int i=0;i<n-1;i++){
ll a,b,c,d;
cin>>a>>b>>c>>d;
adj[a].push_back({b,{c,d}});
adj[b].push_back({a,{c,d}});
mp[{a,b}]=1;
sum+=c;
sum+=d;
}
ans[1]=sum-cal1();
cin>>q;
for(int i=0;i<q;i++){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
return 0;
}
//maybe its multiset not set
Compilation message
designated_cities.cpp: In function 'void setIO(std::string)':
designated_cities.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
431 ms |
41840 KB |
Output is correct |
3 |
Correct |
496 ms |
62288 KB |
Output is correct |
4 |
Correct |
470 ms |
40448 KB |
Output is correct |
5 |
Correct |
420 ms |
41480 KB |
Output is correct |
6 |
Correct |
465 ms |
45108 KB |
Output is correct |
7 |
Correct |
381 ms |
40544 KB |
Output is correct |
8 |
Correct |
505 ms |
63052 KB |
Output is correct |
9 |
Correct |
323 ms |
39788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Incorrect |
530 ms |
41620 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
431 ms |
41840 KB |
Output is correct |
3 |
Correct |
496 ms |
62288 KB |
Output is correct |
4 |
Correct |
470 ms |
40448 KB |
Output is correct |
5 |
Correct |
420 ms |
41480 KB |
Output is correct |
6 |
Correct |
465 ms |
45108 KB |
Output is correct |
7 |
Correct |
381 ms |
40544 KB |
Output is correct |
8 |
Correct |
505 ms |
63052 KB |
Output is correct |
9 |
Correct |
323 ms |
39788 KB |
Output is correct |
10 |
Correct |
2 ms |
7512 KB |
Output is correct |
11 |
Incorrect |
530 ms |
41620 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Incorrect |
3 ms |
7516 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |