#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct obj{
ll to,c0,c1;// to, back
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n,q;
cin >> n;
vector<vector<obj>> con(n);
ll tot = 0;
for(ll i = 1;i<n;i++){
ll a,b,c,d;
cin >> a >> b >> c >> d;a--;b--;
con[a].push_back({b,c,d});
con[b].push_back({a,d,c});
tot += c+d;
}
cin >> q;
vector<ll> dp(n,0);
{
function<ll(ll,ll)> dfs;
dfs = [&](ll i, ll p){
ll cur = 0;
for(obj l : con[i]){
if (l.to!=p){
cur += l.c1 + dfs(l.to,i);
}
}
return cur;
};
dp[0] = dfs(0,-1);
}
{
function<void(ll,ll,ll)> dfs;
dfs = [&](ll i, ll p, ll v){
dp[i] = v;
for(obj l : con[i]){
if (l.to!=p){
dfs(l.to,i,v+l.c0-l.c1);
}
}
};
dfs(0,-1,dp[0]);
}
while(q--){
ll e;
cin >> e;
if (e==1){
ll ans = 0;
for(ll i : dp) ans = max(i,ans);
ans = tot-ans;
cout << ans << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
130 ms |
26076 KB |
Output is correct |
3 |
Correct |
186 ms |
44068 KB |
Output is correct |
4 |
Correct |
127 ms |
24660 KB |
Output is correct |
5 |
Correct |
125 ms |
25860 KB |
Output is correct |
6 |
Correct |
138 ms |
28836 KB |
Output is correct |
7 |
Correct |
115 ms |
25084 KB |
Output is correct |
8 |
Correct |
167 ms |
44468 KB |
Output is correct |
9 |
Correct |
101 ms |
24120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
130 ms |
26076 KB |
Output is correct |
3 |
Correct |
186 ms |
44068 KB |
Output is correct |
4 |
Correct |
127 ms |
24660 KB |
Output is correct |
5 |
Correct |
125 ms |
25860 KB |
Output is correct |
6 |
Correct |
138 ms |
28836 KB |
Output is correct |
7 |
Correct |
115 ms |
25084 KB |
Output is correct |
8 |
Correct |
167 ms |
44468 KB |
Output is correct |
9 |
Correct |
101 ms |
24120 KB |
Output is correct |
10 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |