This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const int inf=INT_MAX;
const ll inff=1e18;
const ll mod=1e9+7;
#define pii pair<int,int>
#define mkp make_pair
#define F first
#define S second
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define int ll
#ifdef HNO2
#define IOS
#else
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#endif // HNO2
int x[maxn*2],y[maxn*2],w[maxn*2];
int indeg[maxn];
vector <int> G[maxn];
int ans[maxn];
int tmpsum=0;
pii maxx[maxn];
int root,root2;
void dfs(int now,int p)
{
for (int i:G[now])
if ((x[i]^y[i]^now)!=p)
{
ans[1]+=w[i];
dfs((x[i]^y[i]^now),now);
}
}
void dfs2(int now,int p)
{
ans[1]=min(ans[1],tmpsum);
for (int i:G[now])
if ((x[i]^y[i]^now)!=p)
{
tmpsum-=(w[i]-w[i^1]);
dfs2((x[i]^y[i]^now),now);
tmpsum+=(w[i]-w[i^1]);
}
}
void dfs3(int now,int p)
{
pair<pii,pii> nowmax=mkp(mkp(-inff,-inff),mkp(-inff,-inff));
if (sz(G[now])==1)
{
maxx[now]=mkp(0,now);
return;
}
for (int i:G[now])
if ((x[i]^y[i]^now)!=p)
{
int I=(x[i]^y[i]^now);
tmpsum-=(w[i]-w[i^1]);
dfs3(I,now);
//renew
pii nowpair=mkp(maxx[I].F+w[i],maxx[I].S);
if (nowpair>=nowmax.F) nowmax.S=nowmax.F,nowmax.F=nowpair;
else if (nowpair>=nowmax.S) nowmax.S=nowpair;
//
tmpsum+=(w[i]-w[i^1]);
}
if (nowmax.F.F!=-inff&&nowmax.S.F!=-inff&&ans[2]>tmpsum-nowmax.F.F-nowmax.S.F)
ans[2]=tmpsum-nowmax.F.F-nowmax.S.F,root=nowmax.F.S,root2=nowmax.S.S;
maxx[now]=nowmax.F;
}
void dfs4(int now,int p)
{
for (int i:G[now])
if ((x[i]^y[i]^now)!=p)
{
tmpsum+=w[i];
dfs4((x[i]^y[i]^now),now);
}
}
int pp[maxn],weight[maxn],used[maxn];
int L[maxn],R[maxn],rL[maxn];
int Cnt=1;
struct segtree
{
pii seg[maxn*4];
int tag[maxn*4];
void Build(int now,int l,int r)
{
if (l==r)
{
seg[now]=mkp(0,l);
return;
}
int m=(l+r)>>1;
Build(now*2,l,m);
Build(now*2+1,m+1,r);
seg[now]=max(seg[now*2],seg[now*2+1]);
}
void push(int now,int l,int r)
{
if (tag[now]==0||l==r)
{
tag[now]=0;
return;
}
seg[now*2].F+=tag[now];
tag[now*2]+=tag[now];
seg[now*2+1].F+=tag[now];
tag[now*2+1]+=tag[now];
tag[now]=0;
}
void modify(int now,int l,int r,int ql,int qr,int v)
{
//cout<<now<<' '<<l<<' '<<r<<' '<<ql<<' '<<qr<<endl;
if (l>=ql&&r<=qr)
{
seg[now].F+=v;
tag[now]+=v;
return;
}
int m=(l+r)>>1;
push(now,l,r);
if (ql<=m) modify(now*2,l,m,ql,qr,v);
if (qr>m) modify(now*2+1,m+1,r,ql,qr,v);
seg[now]=max(seg[now*2],seg[now*2+1]);
}
pii query()
{
return seg[1];
}
}seg;
void dfs5(int now,int p)
{
pp[now]=p;
L[now]=Cnt++;
rL[L[now]]=now;
for (int i:G[now])
if ((x[i]^y[i]^now)!=p)
{
weight[(x[i]^y[i]^now)]=w[i];
dfs5((x[i]^y[i]^now),now);
}
R[now]=Cnt-1;
}
void use(int xx,int n)
{
while (!used[xx])
{
used[xx]=1;
seg.modify(1,1,n,L[xx],R[xx],-weight[xx]);
xx=pp[xx];
}
}
int32_t main()
{
IOS
int n;
cin>>n;
for (int i=1;i<=n;i++) ans[i]=inff;
int _=0;
for (int i=1;i<=n-1;i++)
{
int X,Y,Z,W;
cin>>X>>Y>>Z>>W;
x[_]=X,y[_]=Y,w[_]=Z;
x[_^1]=Y,y[_^1]=X,w[_^1]=W;
G[X].pb(_);
G[Y].pb(_^1);
_+=2;
indeg[X]++;
indeg[Y]++;
}
//1: calculate ans[1]
ans[1]=0;
dfs(1,-1);
tmpsum=ans[1];
dfs2(1,-1);
//
//2: ans[2]
if (n==2)
{
ans[2]=0;
int q;
cin>>q;
while (q--)
{
int X;
cin>>X;
cout<<ans[X]<<endl;
}
return 0;
}
for (int i=1;i<=n;i++)
if (indeg[i]>=2)
{
tmpsum=0;
dfs4(i,-1);
dfs3(i,-1);
break;
}
//
//3: using ans[2] to calculate ans[i]
seg.Build(1,1,n);
dfs5(root,-1);
for (int i=1;i<=n;i++)
if (i!=root) seg.modify(1,1,n,L[i],R[i],weight[i]);
used[root]=1;
use(root2,n);
//cout<<"Hi"<<endl;
seg.modify(1,1,n,L[root],L[root],-inff);
//cout<<seg.query().F<<endl;
seg.modify(1,1,n,L[root2],L[root2],-inff);
//cout<<seg.query().F<<endl;
for (int i=3;i<=n;i++)
{
ans[i]=ans[i-1];
pii ret=seg.query();
ans[i]-=ret.F;
use(rL[ret.S],n);
seg.modify(1,1,n,ret.S,ret.S,-inff);
}
//
int q;
cin>>q;
while (q--)
{
int X;
cin>>X;
cout<<ans[X]<<endl;
}
//
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |