#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
using lint = long long;
using intp = pair<int,int>;
using vint = vector<int>;
const int MAX_N=200010;
const lint INF=MAX_N*1000000000LL;
struct Edge
{
int v;
lint w,wr;
};
struct Seg
{
int mi;
lint mx,lazy;
};
int n,q;
vector<Edge> edge[MAX_N];
int sz[MAX_N];
int lock[MAX_N];
int si[MAX_N],ei[MAX_N],vi[MAX_N];
int pa[MAX_N];
lint paw[MAX_N];
Seg seg[MAX_N<<2];
lint ans[MAX_N];
lint getv(int v,int p)
{
lint ret=0;
for(auto e : edge[v])
if(e.v!=p && lock[e.v]==0)
ret+=e.wr+getv(e.v,v);
return ret;
}
int fillsz(int v,int p)
{
sz[v]=1;
for(auto e : edge[v])
if(e.v!=p && lock[e.v]==0)
sz[v]+=fillsz(e.v,v);
return sz[v];
}
int findc(int v,int p,int half)
{
for(auto e : edge[v])
if(e.v!=p && lock[e.v]==0 && sz[e.v]>half)
return findc(e.v,v,half);
return v;
}
void dfs(int v,int p,int &i)
{
vi[i]=v;
si[v]=i++;
for(auto e : edge[v])
if(e.v!=p && lock[e.v]==0)
{
pa[e.v]=v;
paw[e.v]=e.w;
dfs(e.v,v,i);
}
ei[v]=i;
}
void seg_init(int i,int s,int e)
{
seg[i].mx=seg[i].lazy=0;
if(s+1==e)
{
seg[i].mi=vi[s];
return;
}
seg_init(i<<1,s,(s+e)>>1);
seg_init(i<<1|1,(s+e)>>1,e);
}
void update_lazy(int i)
{
seg[i<<1].mx+=seg[i].lazy;
seg[i<<1].lazy+=seg[i].lazy;
seg[i<<1|1].mx+=seg[i].lazy;
seg[i<<1|1].lazy+=seg[i].lazy;
seg[i].lazy=0;
}
void update_(int i,int s,int e,int l,int r,lint v)
{
if(s>=r || e<=l)
return;
if(l<=s && e<=r)
{
seg[i].mx+=v;
seg[i].lazy+=v;
return;
}
update_lazy(i);
update_(i<<1,s,(s+e)>>1,l,r,v);
update_(i<<1|1,(s+e)>>1,e,l,r,v);
if(seg[i<<1].mx>=seg[i<<1|1].mx)
seg[i]={seg[i<<1].mi,seg[i<<1].mx,0};
else
seg[i]={seg[i<<1|1].mi,seg[i<<1|1].mx,0};
}
int zero(int v,int cn)
{
if(paw[v]==0)
return 0;
update_(1,0,cn,si[v],ei[v],-paw[v]);
paw[v]=0;
int r=zero(pa[v],cn);
return (r) ? r : v;
}
void calc(int v,lint addv)
{
addv-=getv(v,0);
fillsz(v,0);
int cent=findc(v,0,sz[v]/2);
addv+=getv(cent,0);
int idx=0;
paw[cent]=0;
dfs(cent,0,idx);
int cn=idx;
seg_init(1,0,cn);
for(int i=0;i<cn;i++)
update_(1,0,cn,si[vi[i]],ei[vi[i]],paw[vi[i]]);
lint val=addv;
for(int i=1;i<=cn;i++)
{
if(i==2 && seg[1].mx!=0)
{
int c1=seg[1].mi;
val+=seg[1].mx;
int p=zero(c1,cn);
update_(1,0,n,si[p],ei[p],-INF);
int c2=seg[1].mi;
val+=seg[1].mx;
update_(1,0,n,si[p],ei[p],INF);
zero(c2,cn);
}
if(i>=3 && seg[1].mx!=0)
{
int c=seg[i].mi;
val+=seg[1].mx;
zero(c,cn);
}
ans[i]=max(ans[i],val);
}
lock[cent]=1;
for(auto e : edge[cent])
if(lock[e.v]==0)
calc(e.v,addv+e.w-e.wr);
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
lint sum=0;
for(int i=1;i<n;i++)
{
int u,v;lint w,wr;
cin >> u >> v >> w >> wr;
edge[u].push_back({v,w,wr});
edge[v].push_back({u,wr,w});
sum+=w+wr;
}
calc(1,getv(1,0));
cin >> q;
while(q--)
{
int en;
cin >> en;
cout << sum-ans[en] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Incorrect |
1 ms |
9020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1018 ms |
46396 KB |
Output is correct |
3 |
Correct |
1536 ms |
54868 KB |
Output is correct |
4 |
Correct |
1097 ms |
43344 KB |
Output is correct |
5 |
Correct |
338 ms |
44548 KB |
Output is correct |
6 |
Correct |
1357 ms |
46220 KB |
Output is correct |
7 |
Correct |
271 ms |
43520 KB |
Output is correct |
8 |
Correct |
1736 ms |
55376 KB |
Output is correct |
9 |
Correct |
194 ms |
44196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
11096 KB |
Output is correct |
2 |
Correct |
1145 ms |
46376 KB |
Output is correct |
3 |
Incorrect |
1498 ms |
58196 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Incorrect |
1 ms |
9020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1018 ms |
46396 KB |
Output is correct |
3 |
Correct |
1536 ms |
54868 KB |
Output is correct |
4 |
Correct |
1097 ms |
43344 KB |
Output is correct |
5 |
Correct |
338 ms |
44548 KB |
Output is correct |
6 |
Correct |
1357 ms |
46220 KB |
Output is correct |
7 |
Correct |
271 ms |
43520 KB |
Output is correct |
8 |
Correct |
1736 ms |
55376 KB |
Output is correct |
9 |
Correct |
194 ms |
44196 KB |
Output is correct |
10 |
Correct |
1 ms |
11096 KB |
Output is correct |
11 |
Correct |
1145 ms |
46376 KB |
Output is correct |
12 |
Incorrect |
1498 ms |
58196 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Incorrect |
1 ms |
9020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |