#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll n,x,wa,wb,y,ans[N],down[N],fr[N],fn[N],dsum,q,sub[N][2],vis[N],rta,rtb,lst[N];
vector<pair<ll,pll> > v[N];
priority_queue<ll> pq;
void PDFS(ll nd,ll Parent){
sub[nd][0]=sub[nd][1]=down[nd]=nd;
for(auto i:v[nd]){
if(i.X==Parent)continue;
fn[i.X]=fn[nd]+i.Y.Y;
fr[i.X]=fr[nd]+i.Y.X;
dsum+=i.Y.X;
PDFS(i.X,nd);
down[nd]=(fr[down[i.X]]>fr[down[nd]]?down[i.X]:down[nd]);
ll id=down[i.X];
if(fr[sub[nd][0]]<=fr[id])swap(id,sub[nd][0]);
if(fr[sub[nd][1]]<=fr[id])swap(id,sub[nd][1]);
}
}
void RDFS(ll nd,ll pa){
lst[nd]=pa;
for(auto i:v[nd]){
if(i.X==pa)continue;
RDFS(i.X,nd);
}
}
void DFS(ll nd,ll pa){
for(auto i:v[nd]){
if(i.X==pa||vis[i.X])continue;
DFS(i.X,nd);
fr[i.X]+=i.Y.X;
fr[nd]=max(fr[nd],fr[i.X]);
}
ll cnt=0;
for(auto i:v[nd]){
if(i.X==pa||vis[i.X])continue;
if(cnt==0&&fr[i.X]==fr[nd])++cnt;
else{
pq.push(fr[i.X]);
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
REP(i,n-1){
cin>>x>>y>>wa>>wb;
v[x].pb(mp(y,mp(wa,wb)));
v[y].pb(mp(x,mp(wb,wa)));
}
PDFS(1,0);
REP1(i,n)ans[i]=INF;
REP1(i,n){
ans[1]=min(ans[1],dsum+fn[i]-fr[i]);
if(dsum+fn[i]+fr[i]-(fr[sub[i][0]]+fr[sub[i][1]])<ans[2]){
rta=sub[i][0];rtb=sub[i][1];
ans[2]=dsum+fn[i]+fr[i]-(fr[sub[i][0]]+fr[sub[i][1]]);
}
}
memset(fn,0,sizeof(fn));
memset(fr,0,sizeof(fr));
dsum=0;
RDFS(rta,0);
vis[rta]=1;
while(rtb!=rta){
vis[rtb]=1;
rtb=lst[rtb];
}
REP1(i,n){
if(vis[i]){
DFS(i,0);
pq.push(fr[i]);
}
}
for(int i=3;i<=n;i++){
if(SZ(pq)==0){
ans[i]=ans[i-1];continue;
}
ans[i]=ans[i-1]-pq.top();
pq.pop();
}
cin>>q;
while(q--){
cin>>x;
cout<<ans[x]+(x>=3?dsum:0)<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
6 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
5 ms |
8172 KB |
Output is correct |
11 |
Correct |
5 ms |
8192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
344 ms |
31340 KB |
Output is correct |
3 |
Correct |
361 ms |
48244 KB |
Output is correct |
4 |
Correct |
323 ms |
30316 KB |
Output is correct |
5 |
Correct |
315 ms |
30764 KB |
Output is correct |
6 |
Correct |
344 ms |
34152 KB |
Output is correct |
7 |
Correct |
287 ms |
29980 KB |
Output is correct |
8 |
Correct |
361 ms |
48872 KB |
Output is correct |
9 |
Correct |
218 ms |
30608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
337 ms |
31464 KB |
Output is correct |
3 |
Correct |
369 ms |
50656 KB |
Output is correct |
4 |
Correct |
343 ms |
30316 KB |
Output is correct |
5 |
Correct |
318 ms |
30880 KB |
Output is correct |
6 |
Correct |
348 ms |
34536 KB |
Output is correct |
7 |
Correct |
232 ms |
30608 KB |
Output is correct |
8 |
Correct |
350 ms |
42976 KB |
Output is correct |
9 |
Correct |
214 ms |
30780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
6 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
5 ms |
8172 KB |
Output is correct |
11 |
Correct |
5 ms |
8192 KB |
Output is correct |
12 |
Correct |
6 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8556 KB |
Output is correct |
14 |
Correct |
7 ms |
8684 KB |
Output is correct |
15 |
Correct |
8 ms |
8556 KB |
Output is correct |
16 |
Correct |
7 ms |
8556 KB |
Output is correct |
17 |
Correct |
7 ms |
8556 KB |
Output is correct |
18 |
Correct |
7 ms |
8556 KB |
Output is correct |
19 |
Correct |
7 ms |
8556 KB |
Output is correct |
20 |
Correct |
7 ms |
8556 KB |
Output is correct |
21 |
Correct |
8 ms |
8556 KB |
Output is correct |
22 |
Correct |
8 ms |
8556 KB |
Output is correct |
23 |
Correct |
9 ms |
8556 KB |
Output is correct |
24 |
Correct |
8 ms |
8556 KB |
Output is correct |
25 |
Correct |
7 ms |
8684 KB |
Output is correct |
26 |
Correct |
7 ms |
8556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
344 ms |
31340 KB |
Output is correct |
3 |
Correct |
361 ms |
48244 KB |
Output is correct |
4 |
Correct |
323 ms |
30316 KB |
Output is correct |
5 |
Correct |
315 ms |
30764 KB |
Output is correct |
6 |
Correct |
344 ms |
34152 KB |
Output is correct |
7 |
Correct |
287 ms |
29980 KB |
Output is correct |
8 |
Correct |
361 ms |
48872 KB |
Output is correct |
9 |
Correct |
218 ms |
30608 KB |
Output is correct |
10 |
Correct |
5 ms |
8172 KB |
Output is correct |
11 |
Correct |
337 ms |
31464 KB |
Output is correct |
12 |
Correct |
369 ms |
50656 KB |
Output is correct |
13 |
Correct |
343 ms |
30316 KB |
Output is correct |
14 |
Correct |
318 ms |
30880 KB |
Output is correct |
15 |
Correct |
348 ms |
34536 KB |
Output is correct |
16 |
Correct |
232 ms |
30608 KB |
Output is correct |
17 |
Correct |
350 ms |
42976 KB |
Output is correct |
18 |
Correct |
214 ms |
30780 KB |
Output is correct |
19 |
Correct |
5 ms |
8172 KB |
Output is correct |
20 |
Correct |
334 ms |
31340 KB |
Output is correct |
21 |
Correct |
377 ms |
57312 KB |
Output is correct |
22 |
Correct |
330 ms |
35500 KB |
Output is correct |
23 |
Correct |
345 ms |
38164 KB |
Output is correct |
24 |
Correct |
321 ms |
35816 KB |
Output is correct |
25 |
Correct |
337 ms |
38244 KB |
Output is correct |
26 |
Correct |
322 ms |
35684 KB |
Output is correct |
27 |
Correct |
320 ms |
37156 KB |
Output is correct |
28 |
Correct |
333 ms |
40660 KB |
Output is correct |
29 |
Correct |
338 ms |
38892 KB |
Output is correct |
30 |
Correct |
311 ms |
36204 KB |
Output is correct |
31 |
Correct |
269 ms |
37276 KB |
Output is correct |
32 |
Correct |
346 ms |
50144 KB |
Output is correct |
33 |
Correct |
222 ms |
37268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8172 KB |
Output is correct |
2 |
Correct |
5 ms |
8172 KB |
Output is correct |
3 |
Correct |
5 ms |
8172 KB |
Output is correct |
4 |
Correct |
5 ms |
8172 KB |
Output is correct |
5 |
Correct |
5 ms |
8172 KB |
Output is correct |
6 |
Correct |
6 ms |
8172 KB |
Output is correct |
7 |
Correct |
5 ms |
8172 KB |
Output is correct |
8 |
Correct |
5 ms |
8172 KB |
Output is correct |
9 |
Correct |
5 ms |
8172 KB |
Output is correct |
10 |
Correct |
5 ms |
8172 KB |
Output is correct |
11 |
Correct |
5 ms |
8192 KB |
Output is correct |
12 |
Correct |
5 ms |
8172 KB |
Output is correct |
13 |
Correct |
344 ms |
31340 KB |
Output is correct |
14 |
Correct |
361 ms |
48244 KB |
Output is correct |
15 |
Correct |
323 ms |
30316 KB |
Output is correct |
16 |
Correct |
315 ms |
30764 KB |
Output is correct |
17 |
Correct |
344 ms |
34152 KB |
Output is correct |
18 |
Correct |
287 ms |
29980 KB |
Output is correct |
19 |
Correct |
361 ms |
48872 KB |
Output is correct |
20 |
Correct |
218 ms |
30608 KB |
Output is correct |
21 |
Correct |
5 ms |
8172 KB |
Output is correct |
22 |
Correct |
337 ms |
31464 KB |
Output is correct |
23 |
Correct |
369 ms |
50656 KB |
Output is correct |
24 |
Correct |
343 ms |
30316 KB |
Output is correct |
25 |
Correct |
318 ms |
30880 KB |
Output is correct |
26 |
Correct |
348 ms |
34536 KB |
Output is correct |
27 |
Correct |
232 ms |
30608 KB |
Output is correct |
28 |
Correct |
350 ms |
42976 KB |
Output is correct |
29 |
Correct |
214 ms |
30780 KB |
Output is correct |
30 |
Correct |
6 ms |
8172 KB |
Output is correct |
31 |
Correct |
8 ms |
8556 KB |
Output is correct |
32 |
Correct |
7 ms |
8684 KB |
Output is correct |
33 |
Correct |
8 ms |
8556 KB |
Output is correct |
34 |
Correct |
7 ms |
8556 KB |
Output is correct |
35 |
Correct |
7 ms |
8556 KB |
Output is correct |
36 |
Correct |
7 ms |
8556 KB |
Output is correct |
37 |
Correct |
7 ms |
8556 KB |
Output is correct |
38 |
Correct |
7 ms |
8556 KB |
Output is correct |
39 |
Correct |
8 ms |
8556 KB |
Output is correct |
40 |
Correct |
8 ms |
8556 KB |
Output is correct |
41 |
Correct |
9 ms |
8556 KB |
Output is correct |
42 |
Correct |
8 ms |
8556 KB |
Output is correct |
43 |
Correct |
7 ms |
8684 KB |
Output is correct |
44 |
Correct |
7 ms |
8556 KB |
Output is correct |
45 |
Correct |
5 ms |
8172 KB |
Output is correct |
46 |
Correct |
334 ms |
31340 KB |
Output is correct |
47 |
Correct |
377 ms |
57312 KB |
Output is correct |
48 |
Correct |
330 ms |
35500 KB |
Output is correct |
49 |
Correct |
345 ms |
38164 KB |
Output is correct |
50 |
Correct |
321 ms |
35816 KB |
Output is correct |
51 |
Correct |
337 ms |
38244 KB |
Output is correct |
52 |
Correct |
322 ms |
35684 KB |
Output is correct |
53 |
Correct |
320 ms |
37156 KB |
Output is correct |
54 |
Correct |
333 ms |
40660 KB |
Output is correct |
55 |
Correct |
338 ms |
38892 KB |
Output is correct |
56 |
Correct |
311 ms |
36204 KB |
Output is correct |
57 |
Correct |
269 ms |
37276 KB |
Output is correct |
58 |
Correct |
346 ms |
50144 KB |
Output is correct |
59 |
Correct |
222 ms |
37268 KB |
Output is correct |
60 |
Correct |
5 ms |
8172 KB |
Output is correct |
61 |
Correct |
365 ms |
40008 KB |
Output is correct |
62 |
Correct |
390 ms |
55900 KB |
Output is correct |
63 |
Correct |
356 ms |
38244 KB |
Output is correct |
64 |
Correct |
367 ms |
40288 KB |
Output is correct |
65 |
Correct |
355 ms |
38116 KB |
Output is correct |
66 |
Correct |
366 ms |
40676 KB |
Output is correct |
67 |
Correct |
353 ms |
37860 KB |
Output is correct |
68 |
Correct |
350 ms |
39584 KB |
Output is correct |
69 |
Correct |
367 ms |
42592 KB |
Output is correct |
70 |
Correct |
374 ms |
40932 KB |
Output is correct |
71 |
Correct |
339 ms |
38816 KB |
Output is correct |
72 |
Correct |
309 ms |
40732 KB |
Output is correct |
73 |
Correct |
381 ms |
50012 KB |
Output is correct |
74 |
Correct |
257 ms |
41232 KB |
Output is correct |