#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=2e5+5;
struct dat{
int v;ll a,b;
};vector<dat>g[mxn];
ll dp[mxn]{0},rs[mxn]{0},d[mxn]{0};
void dfs1(int u,int p){
for(auto [v,a,b]:g[u]){
if(v==p)continue;
dfs1(v,u);dp[u]+=b+dp[v];
}
}
void dfs2(int u,int p){
for(auto [v,a,b] :g[u]){
if(v==p)continue;
dp[v]=dp[u]+a-b;dfs2(v,u);
}
}int rt;
ll dfs3(int u,int p){
ll mx1=0,mx2=0;
for(auto [v,a,b] : g[u]){
if(v==p)continue;
dfs3(v,u);
ll tt=d[v]+a;
if(tt>mx1)swap(tt,mx1);
if(tt>mx2)swap(tt,mx2);
}
if(rs[2]<mx1+mx2+dp[u]){
rs[2]=mx1+mx2+dp[u];
rt=u;
}
return d[u]=mx1;
}
vector<ll>di[mxn];
void dfs4(int u,int p,ll t){
di[u].pb(0);
for(auto [v,a,b] : g[u]){
if(v==p)continue;
dfs4(v,u,a);
if(di[u].size()<di[v].size())swap(di[u],di[v]);
if(di[u].back()>di[v].back())swap(di[u].back(),di[v].back());
for(auto x:di[v])di[u].pb(x);
}di[u].back()+=t;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;ll ans=0;
for(int i=1;i<=n-1;i++){
int u,v,a,b;cin>>u>>v>>a>>b;ans+=a+b;
g[u].pb({v,a,b});g[v].pb({u,b,a});
}dfs1(1,1);dfs2(1,1);for(int i=1;i<=n;i++)rs[1]=max(rs[1],dp[i]);
dfs3(1,1);dfs4(rt,rt,0);sort(di[rt].rbegin(),di[rt].rend());
ll cur = di[rt][0]+di[rt][1];
for(int i=2;i<n;i++){
cur+=di[rt][i];
rs[i+1]=dp[rt]+cur;
}int q;cin>>q;
while(q--){
int x;cin>>x;
cout<<ans-rs[x]<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
7 ms |
9816 KB |
Output is correct |
5 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
260 ms |
48576 KB |
Output is correct |
3 |
Correct |
281 ms |
59852 KB |
Output is correct |
4 |
Correct |
215 ms |
47300 KB |
Output is correct |
5 |
Correct |
199 ms |
43516 KB |
Output is correct |
6 |
Correct |
196 ms |
46636 KB |
Output is correct |
7 |
Correct |
176 ms |
41156 KB |
Output is correct |
8 |
Correct |
283 ms |
59728 KB |
Output is correct |
9 |
Correct |
146 ms |
39664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
215 ms |
48580 KB |
Output is correct |
3 |
Correct |
255 ms |
62148 KB |
Output is correct |
4 |
Correct |
184 ms |
47252 KB |
Output is correct |
5 |
Correct |
194 ms |
43520 KB |
Output is correct |
6 |
Correct |
192 ms |
46788 KB |
Output is correct |
7 |
Correct |
136 ms |
39924 KB |
Output is correct |
8 |
Correct |
221 ms |
53448 KB |
Output is correct |
9 |
Correct |
114 ms |
39408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
7 ms |
9816 KB |
Output is correct |
5 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
260 ms |
48576 KB |
Output is correct |
3 |
Correct |
281 ms |
59852 KB |
Output is correct |
4 |
Correct |
215 ms |
47300 KB |
Output is correct |
5 |
Correct |
199 ms |
43516 KB |
Output is correct |
6 |
Correct |
196 ms |
46636 KB |
Output is correct |
7 |
Correct |
176 ms |
41156 KB |
Output is correct |
8 |
Correct |
283 ms |
59728 KB |
Output is correct |
9 |
Correct |
146 ms |
39664 KB |
Output is correct |
10 |
Correct |
5 ms |
9816 KB |
Output is correct |
11 |
Correct |
215 ms |
48580 KB |
Output is correct |
12 |
Correct |
255 ms |
62148 KB |
Output is correct |
13 |
Correct |
184 ms |
47252 KB |
Output is correct |
14 |
Correct |
194 ms |
43520 KB |
Output is correct |
15 |
Correct |
192 ms |
46788 KB |
Output is correct |
16 |
Correct |
136 ms |
39924 KB |
Output is correct |
17 |
Correct |
221 ms |
53448 KB |
Output is correct |
18 |
Correct |
114 ms |
39408 KB |
Output is correct |
19 |
Correct |
4 ms |
9816 KB |
Output is correct |
20 |
Correct |
186 ms |
48320 KB |
Output is correct |
21 |
Correct |
240 ms |
62412 KB |
Output is correct |
22 |
Correct |
179 ms |
47236 KB |
Output is correct |
23 |
Correct |
173 ms |
48324 KB |
Output is correct |
24 |
Correct |
190 ms |
47672 KB |
Output is correct |
25 |
Correct |
242 ms |
48828 KB |
Output is correct |
26 |
Correct |
191 ms |
47292 KB |
Output is correct |
27 |
Correct |
183 ms |
43524 KB |
Output is correct |
28 |
Correct |
190 ms |
46380 KB |
Output is correct |
29 |
Correct |
186 ms |
48068 KB |
Output is correct |
30 |
Correct |
169 ms |
46332 KB |
Output is correct |
31 |
Correct |
147 ms |
40620 KB |
Output is correct |
32 |
Correct |
211 ms |
54176 KB |
Output is correct |
33 |
Correct |
126 ms |
39716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
7 ms |
9816 KB |
Output is correct |
5 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |