【BZOJ3784】树上的路径

5 10
1 2 1
1 3 2
2 4 3
2 5 4

7
7
6
5
4
4
3
3
2
1

HINT

N<=50000,M<=Min(300000,n*(n-1) /2 )

#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <queue>
#define mp(A,B,C,D)	make_pair(make_pair(A,B),make_pair(C,D))
using namespace std;
const int maxn=50010;
typedef pair<int,int> pii;
int n,m,cnt,maxx,tot,root,nm;
int lp[800010],rp[800010],v[800010],vis[maxn],Log[800010];
priority_queue<pair<pii,pii> > pq;
int rd()
{
int ret=0,f=1;	char gc=getchar();
while(gc<'0'||gc>'9')	{if(gc=='-')f=-f;	gc=getchar();}
while(gc>='0'&&gc<='9')	ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int ms(int a,int b)
{
return v[a]>v[b]?a:b;
}
{
}
void getr(int x,int fa)
{
siz[x]=1;
int i,mx=0;
{
if(vis[to[i]]||to[i]==fa)	continue;
getr(to[i],x),siz[x]+=siz[to[i]],mx=max(mx,siz[to[i]]);
}
if(maxx>max(mx,tot-siz[x]))	root=x,maxx=max(mx,tot-siz[x]);
}
void getd(int x,int fa,int dep)
{
v[++nm]=dep,lp[nm]=lp[nm-1],rp[nm]=rp[nm]?rp[nm]:rp[nm-1];
{
if(vis[to[i]]||to[i]==fa)	continue;
getd(to[i],x,dep+val[i]);
}
}
void dfs(int x)
{
vis[x]=1;
int i;
v[++nm]=0,lp[nm]=nm,rp[nm]=nm-1;
{
if(vis[to[i]])	continue;
rp[nm+1]=nm,getd(to[i],x,val[i]);
}
{
if(vis[to[i]])	continue;
tot=siz[to[i]],maxx=1<<30,getr(to[i],x),dfs(root);
}
}
int query(int a,int b)
{
if(a>b)	return 0;
int k=Log[b-a+1];
return ms(sm[a][k],sm[b-(1<<k)+1][k]);
}
int main()
{
n=rd(),m=rd();
int i,j,a,b,c,d,x,y;
tot=n,maxx=1<<30,getr(1,0),dfs(root);
for(i=1;i<=nm;i++)	sm[i][0]=i;
for(i=2;i<=nm;i++)	Log[i]=Log[i>>1]+1;
for(j=1;(1<<j)<nm;j++)
for(i=1;i+(1<<j)-1<=nm;i++)
sm[i][j]=ms(sm[i][j-1],sm[i+(1<<j-1)][j-1]);
for(i=1;i<=nm;i++)
{
if(lp[i]>rp[i])	continue;
pq.push(mp(v[i]+v[query(lp[i],rp[i])],i,lp[i],rp[i]));
}
for(i=1;i<=m;i++)
{
pii t1=pq.top().first,t2=pq.top().second;
pq.pop();
printf("%d\n",t1.first),x=t1.second,a=t2.first,b=t2.second,y=query(a,b);
c=query(a,y-1),d=query(y+1,b);
if(c)	pq.push(mp(v[x]+v[c],x,a,y-1));
if(d)	pq.push(mp(v[x]+v[d],x,y+1,b));
}
return 0;
}

| 欢迎来原网站坐坐！ >原文链接<

posted @ 2017-06-23 19:40  CQzhangyu  阅读(721)  评论(0编辑  收藏  举报