zoukankan      html  css  js  c++  java
  • CF293E Close Vertices 点分治+单调队列+树状数组

    E. Close Vertices

    You've got a weighted tree, consisting of n vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.

    Two vertices are close if there exists a path of length at most l between them and a path of weight at most w between them. Count the number of pairs of vertices v, u (v < u), such that vertices v and u are close.

    Input

    The first line contains three integers n, l and w (1 ≤ n ≤ 105, 1 ≤ l ≤ n, 0 ≤ w ≤ 109). The next n - 1 lines contain the descriptions of the tree edges. The i-th line contains two integers pi, wi (1 ≤ pi < (i + 1), 0 ≤ wi ≤ 104), that mean that the i-th edge connects vertex (i + 1)and pi and has weight wi.

    Consider the tree vertices indexed from 1 to n in some way.

    Output

    Print a single integer — the number of close pairs.

    Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

    题目大意

    给出一个树,每条边有权值,求经过少于L条边,权值和少于W的路径总数。

    题解

    点分治裸题,主要是处理答案这里。首先处理以root为根时每个点的到根的边数和权值,然后以边数作为树状数组下标,对于边权值大于要求的-1,查询1-L区间的和就是所求答案。然后又因为将这些边用权值排序,满足单调性,用单调队列进行维护即可。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define int long long
    using namespace std;
    const int maxn=1e5+10;
    const int maxm=1e7+10;
    const int INF=0x3f3f3f3f; 
    int read(){
        int x=0,f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,ml;
    int head[maxn],cnt;
    struct edge{int to,nxt,w;}e[maxn*2];
    void add(int x,int y,int w){
        e[++cnt]=(edge){y,head[x],w};
        head[x]=cnt;
    }
    int vis[maxn];
    int size[maxn],son[maxn];
    int S,m;
    int mx,root;
    struct stu{
        int w,num;
    }dis[maxn];
    int c[maxn];
    bool cmp(stu a,stu b){return a.w==b.w?a.num<b.num:a.w<b.w;}
    void getroot(int x,int f){
        size[x]=1;son[x]=0;
        for(int i=head[x];i;i=e[i].nxt){
            int u=e[i].to;
            if(u==f||vis[u])continue;
            getroot(u,x);
            size[x]+=size[u];
            son[x]=max(son[x],size[u]);
        }
        son[x]=max(son[x],S-size[x]);
        if(son[x]<mx){mx=son[x];root=x;}  
    }
    int tot,len,W;
    void add(int x,int w){
        while(x<=ml){
            c[x]+=w;
            x+=x&(-x);
        }
    }
    int ask(int x){
        int res=0;
        while(x>0){
            res+=c[x];
            x-=x&(-x);
        }
        return res;
    }
    void getdis(int x,int f,int v,int t){
        if(f==0&&v==0&&t==0){}
        else {dis[++tot].w=v,dis[tot].num=t;}
        for(int i=head[x];i;i=e[i].nxt){
            int u=e[i].to;
            if(u==f||vis[u])continue;
            getdis(u,x,v+e[i].w,t+1);
        }
    }
    int ans;
    int calc(int x,int v,int t){
        int res=0;tot=0;
        ml=0;
        getdis(x,0,v,t);
        sort(dis+1,dis+tot+1,cmp);
        if(v==0&&t==0)
            for(int i=1;i<=tot;i++){
                if(dis[i].w<=W&&len>=dis[i].num)res++;
                else if(dis[i].w>W)break;
            }
        int j=tot;
        for(int i=1;i<=tot;i++)ml=max(ml,dis[i].num);
        for(int i=1;i<=ml;i++)c[i]=0;
        for(int i=1;i<=tot;i++)add(dis[i].num,1);
        for(int i=1;i<=tot;i++){
            while(i<j&&dis[i].w+dis[j].w>W){
                add(dis[j].num,-1);
                j--;
            }
            if(i>=j)break;
            add(dis[i].num,-1);
            res+=ask(min(ml,len-dis[i].num));
        }
        return res;
    }
    void dfs(int x){
        ans+=calc(x,0,0);
        vis[x]=1;
        for(int i=head[x];i;i=e[i].nxt){
            int u=e[i].to;
            if(vis[u])continue;
            ans-=calc(u,e[i].w,1);
            root=0;S=size[u];mx=INF;getroot(u,0);
            dfs(root);
        }
    }
    #undef int 
    int main()
    #define int long long
    {
        n=read();len=read();W=read(); 
        for(int i=1;i<n;i++){
            int y=read(),w=read();
            add(i+1,y,w);add(y,i+1,w);
        }
        S=n;mx=INF;getroot(1,0);
        dfs(root);
        cout<<ans;
        return 0;
    }
    
  • 相关阅读:
    jQuery获取各种标签的文本和value值
    python中base64编码与解码
    Django 的 slug url 正则匹配
    JavaScript base64 加密解密
    HTML引入外部JS文件
    CSS样式的引入方式
    Django 配置404页面
    selenium 基本操作
    python 启动pydoc查看文档
    selenium webdriver
  • 原文地址:https://www.cnblogs.com/Nan-Cheng/p/9735172.html
Copyright © 2011-2022 走看看