[JZOJ4927]第K大

15 篇文章 0 订阅
11 篇文章 0 订阅

题目大意

给定一棵具有 n 个节点的树,每条边(u,v)有权值 w(u,v) 。定义 d(i,j) 表示距离点 i j近的点的距离,注意 d(i,1)=0 。对于每一个点 i ,都给定ki。请你求出每个点的 d(i,ki)

1n104,1w103
本题开O2……


题目分析

裸的点分治。
二分答案,转化为判定性问题,然后使用点剖的重心树来判定。
每个重心维护重心树子树中所有点到其距离的排序,以及到其上级重心的距离的排序,二分查找即可。
时间复杂度 O(nlog2nlog(nw))


代码实现

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cctype>

using namespace std;

inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while (!isdigit(ch)) f=ch=='-'?-1:f,ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f;
}

int buf[30];

inline void write(int x)
{
    if (x<0) putchar('-'),x=-x;
    for (;x;x/=10) buf[++buf[0]]=x%10;
    if (!buf[0]) buf[++buf[0]]=0;
    for (;buf[0];putchar('0'+buf[buf[0]--]));
}

const int N=10050;
const int LGN=15;
const int E=N<<1;
const int EL=N<<1;
const int LGEL=15;

int deep[N],top[N],fa[N],pos[N],last[N],que[N],size[N],crf[N],st[N],en[N];
int tov[E],nxt[E],len[E];
int LOG[EL],euler[EL];
int data[2][N*LGN];
int rmq[EL][LGEL];
bool vis[N];
int n,tot,head,tail,cnt,el,lgel,sum;

void insert(int x,int y,int z){tov[++tot]=y,len[tot]=z,nxt[tot]=last[x],last[x]=tot;}

int core(int src)
{
    for (head=0,fa[que[tail=1]=src]=0,top[src]=0;head!=tail;)
    {
        int x=que[++head],i=last[x],y;
        for (size[x]=1;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&!vis[y]) fa[que[++tail]=y]=x,top[y]=top[x]+len[i];
    }
    for (head=tail;head>1;size[fa[que[head]]]+=size[que[head]],head--);
    int ret=0,res=n+1;
    for (head=1;head<=tail;head++)
    {
        int x=que[head],i=last[x],y,tmp=0;
        for (;i;i=nxt[i]) if ((y=tov[i])!=fa[x]&&!vis[y]) tmp=max(tmp,size[y]);
        tmp=max(tmp,size[src]-size[x]);
        if (tmp<res) ret=x,res=tmp;
    }
    return ret;
}

int build(int c,int f,int delta)
{
    fa[c=core(c)]=0;
    for (head=0,deep[que[tail=1]=c]=0;head!=tail;)
        for (int x=que[++head],i=last[x],y;i;i=nxt[i])
            if ((y=tov[i])!=fa[x]&&!vis[y]) deep[que[++tail]=y]=deep[fa[y]=x]+len[i];
    st[c]=cnt+1,cnt=en[c]=cnt+tail;
    for (head=1;head<=tail;head++) data[0][st[c]+head-1]=deep[que[head]],f?data[1][st[c]+head-1]=top[que[head]]+delta:0;
    sort(data[0]+st[c],data[0]+en[c]+1),f?sort(data[1]+st[c],data[1]+en[c]+1),0:0;
    vis[c]=1;
    for (int i=last[c],y;i;i=nxt[i])
        if (!vis[y=tov[i]]) crf[build(y,c,len[i])]=c;
    return c;
}

void dfs(int x)
{
    rmq[pos[euler[++el]=x]=el][0]=x;
    for (int i=last[x],y;i;i=nxt[i])
        if ((y=tov[i])!=fa[x]) deep[y]=deep[x]+1,top[y]=top[x]+len[i],fa[y]=x,dfs(y),euler[++el]=x,rmq[el][0]=x;
}

void pre()
{
    LOG[1]=0;
    for (int i=2;i<=el;i++) LOG[i]=LOG[i-1]+(1<<LOG[i-1]+1==i);
    lgel=LOG[el];
    for (int j=1;j<=lgel;j++)
        for (int i=1;i+(1<<j)-1<=el;i++)
            rmq[i][j]=deep[rmq[i][j-1]]<deep[rmq[i+(1<<j-1)][j-1]]?rmq[i][j-1]:rmq[i+(1<<j-1)][j-1];
}

int getrmq(int l,int r)
{
    int lgr=LOG[r-l+1];
    return deep[rmq[l][lgr]]<deep[rmq[r-(1<<lgr)+1][lgr]]?rmq[l][lgr]:rmq[r-(1<<lgr)+1][lgr];
}

int lca(int x,int y)
{
    if ((x=pos[x])>(y=pos[y])) swap(x,y);
    return getrmq(x,y);
}

int dist(int x,int y){return top[x]+top[y]-(top[lca(x,y)]<<1);}

int query(bool flag,int x,int y)
{
    int l=st[x],r=en[x],mid,ret=l-1;
    while (l<=r)
    {
        mid=l+r>>1;
        if (data[flag][mid]<=y) ret=mid,l=mid+1;
        else r=mid-1;
    }
    return ret=ret-st[x]+1;
}

int count(int src,int l)
{
    int ret=0;
    for (int x=src,y=crf[x];x;x=y,y=crf[x])
    {
        ret+=query(0,x,l-dist(src,x));
        if (y) ret-=query(1,x,l-dist(src,y));
    }
    return ret;
}

int kth(int x,int k)
{
    int l=0,r=sum,mid,ret=-1;
    while (l<=r)
    {
        mid=l+r>>1;
        if (count(x,mid)>=k) ret=mid,r=mid-1;
        else l=mid+1;
    }
    return ret;
}

int main()
{
    freopen("treekth.in","r",stdin),freopen("treekth.out","w",stdout);
    n=read();
    for (int i=1,x,y,z;i<n;i++) x=read(),y=read(),z=read(),insert(x,y,z),insert(y,x,z),sum+=z;
    build(1,0,0),deep[1]=top[1]=fa[1]=0,dfs(1),pre();
    for (int i=1;i<=n;i++) write(kth(i,read())),putchar('\n');
    fclose(stdin),fclose(stdout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值