• poj1741(点分模板)


    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int maxn=10010;
    int last[maxn],pre[maxn*2],other[maxn*2],w[maxn*2],t,vis[maxn];
    void add(int x,int y,int z){++t;pre[t]=last[x];last[x]=t;other[t]=y;w[t]=z;}
    int cnt,root,d[maxn],siz[maxn],f[maxn],sizz,q[maxn];
    int x,y,z,n,m,k,ans;
    void getroot(int x,int fa){
        siz[x]=1;f[x]=0;
        for(int i=last[x];i;i=pre[i]){
            int v=other[i];
            if(v==fa||vis[v])continue;
            getroot(v,x);
            siz[x]+=siz[v];
            f[x]=max(f[x],siz[v]);
        }
        f[x]=max(f[x],sizz-siz[x]);
        if(f[x]<f[root])root=x;
    }
    void getdis(int x,int fa){
        q[++cnt]=d[x];
        for(int i=last[x];i;i=pre[i]){
            int v=other[i];
            if(v==fa||vis[v])continue;
            d[v]=d[x]+w[i];
            getdis(v,x);
        }
    }
    int calc(int x,int init){
        cnt=0;d[x]=init;
        getdis(x,0);
        sort(q+1,q+cnt+1);
        int res=0;
        for(int l=1,r=cnt;l<r;){
            if(q[l]+q[r]<=k){res+=r-l;l++;}
            else r--;
        }
        return res;
    }
    void solve(int x){
        ans+=calc(x,0);
        vis[x]=1;
        for(int i=last[x];i;i=pre[i]){
            int v=other[i];
            if(vis[v])continue;
            ans-=calc(v,w[i]);
            f[0]=sizz=siz[v];
            root=0;
            getroot(v,0);
            solve(root);
        }
    }
    int main(){
        while(scanf("%d%d",&n,&k)!=EOF){
            if(n==0&&k==0)break;
            memset(last,0,sizeof(last));
            memset(vis,0,sizeof(vis));
            t=0;
            for(int i=1;i<n;++i){
                scanf("%d%d%d",&x,&y,&z);
                add(x,y,z);add(y,x,z);
            }
            f[0]=sizz=n;ans=root=0;
            getroot(1,0);
            solve(root);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    【MySQL】MySQL之备份
    【MySQL】MySQL之导入和导出
    MySQL逻辑备份之MySQLDump
    【Perl】Linux下安装Perl
    【MySQL】MySQL NDB Cluster维护
    【MySQL】MySQL NDB Cluster安装
    Linux服务器开启ssh服务,实现ssh远程登陆!
    PDO异常处理
    PHP异常处理
    PDO中的事务处理
  • 原文地址:https://www.cnblogs.com/dibaotianxing/p/8066994.html
Copyright © 2020-2023  润新知