传送门

只是来贴一个点分治的板子(年轻时候写的丑别介意)。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 10010
#define INF 1e9+10
struct node{int v,c,next;}e[N*2];
int first[N],m;
int son[N],f[N];
bool in[N];
int d[N],dep[N];
int n,sum,root,k,ans;
inline void add(int u,int v,int cost){//邻接表存储边
    e[++m].next=first[u];
    first[u]=m;
    e[m].v=v;
    e[m].c=cost;
}
void getroot(int v,int fa){//找出当前树的重心
    son[v]=1;
    f[v]=0;
    for(int i=first[v];i;i=e[i].next)
        if(e[i].v!=fa&&!in[e[i].v]){
            getroot(e[i].v,v);
            son[v]+=son[e[i].v];
            f[v]=max(f[v],son[e[i].v]);
        }
    f[v]=max(f[v],sum-son[v]);
    if(f[v]<f[root])root=v;//更新根节点
}
void getdeep(int v,int fa){//预处理到当前根节点的距离
    dep[++dep[0]]=d[v];
    for(int i=first[v];i;i=e[i].next)
        if(e[i].v!=fa&&!in[e[i].v]){
            d[e[i].v]=d[v]+e[i].c;
            getdeep(e[i].v,v);
        }
}
int cal(int v,int cost){//用双指针法求出当前子树信息
    d[v]=cost;
    dep[0]=0;
    getdeep(v,0);
    sort(dep+1,dep+dep[0]+1);
    int l=1,r=dep[0],sum=0;
    while(l<r){//双指针
        if(dep[l]+dep[r]<=k){
            sum+=r-l;
            ++l;
        }
        else --r;
    }
    return sum;
}
void solve(int v){//求出子问题答案
    ans+=cal(v,0);
    in[v]=1;
    for(int i=first[v];i;i=e[i].next)
        if(!in[e[i].v]){
            ans-=cal(e[i].v,e[i].c);//去重
            sum=son[e[i].v];
            root=0;
            getroot(e[i].v,0);
            solve(root);
        }
}
int main(){
    int u,v,w;
    while(scanf("%d%d",&n,&k)&&n&&k){
        ans=root=m=0;
        memset(in,0,sizeof(in));
        memset(first,0,sizeof(first));
        for(int i=1;i<n;++i){
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        f[0]=INF;//赋极大初值来找重心
        sum=n;
        getroot(1,0);//找重心
        solve(root);//点分治求解
        printf("%d\n",ans);
    }
    return 0;
}

2018.07.31 POJ1741Tree(点分治)的更多相关文章

  1. 2018.07.31洛谷P1552 [APIO2012]派遣(可并堆)

    传送门 貌似是个可并堆的模板题,笔者懒得写左偏堆了,直接随机堆水过.实际上这题就是维护一个可合并的大根堆一直从叶子合并到根,如果堆中所有数的和超过了上限就一直弹直到所有数的和不超过上限为止,最后对于当 ...

  2. 2018.07.31 bzoj4569: [Scoi2016]萌萌哒(并查集+倍增)

    传送门 对于每个限制,使用倍增的二进制拆分思想,用并查集数组fa[i][j]" role="presentation" style="position: rel ...

  3. http://www.cnblogs.com/ycxyyzw/archive/2012/07/31/2616951.html

    http://www.cnblogs.com/ycxyyzw/archive/2012/07/31/2616951.html

  4. China Cloud Computing Conference(2018.07.24)

    时间:2018.07.24地点:北京国家会议中心

  5. AI Summit(2018.07.19)

    AI Summit 时间:2018.07.19地点:北京丽都皇冠假日酒店

  6. China Internet Conference(2018.07.12)

    中国互联网大会 时间:2018.07.12地点:北京国家会议中心

  7. <2013 07 31> 没有必然的理由

    <2013 07 31> 没有必然的理由 没有必然的理由 人类从野蛮走向文明 也可能,从野蛮走向更野蛮 没有必然的理由 人群从疯狂走向理智 也可能,从疯狂走向更疯狂 没有必然的理由 你我从 ...

  8. agentzh 的 Nginx 教程(版本 2019.07.31)

    agentzh 的 Nginx 教程(版本 2019.07.31) agentzh 的 Nginx 教程(版本 2019.07.31) https://openresty.org/download/a ...

  9. 2018.07.20 洛谷P4178 Tree(点分治)

    传送门 又一道点分治. 直接维护子树内到根的所有路径长度,然后排序+双指针统计答案. 代码如下: #include<bits/stdc++.h> #define N 40005 using ...

随机推荐

  1. php 3DES|DES 加密解密(通用)

    <?php //set_include_path(get_include_path().PATH_SEPARATOR.'phpseclib'); include('Crypt/DES.php') ...

  2. JVM jstat 详解

    http://www.ityouknow.com/java/2017/03/01/jvm-overview.html 本文基于JDK1.8 https://blog.csdn.net/maosijun ...

  3. 前端开发-2-HTML

    1.开发环境 市面上有很多的HTML编辑器可以选择,常见的Hbuild.Sublime Text.Dreamweare都可以用来开发HTML. 当然PyCharm也支持HTML开发. 2.文件后缀名规 ...

  4. js脚本代码调试小技巧

    以前写js代码调试代码查看数据是否正确的时候不知道F12(开发者工具),都是alert(xxx)或者console.log(xxx), 现在知道还可以用document.write或者try...ca ...

  5. redis之RDB持久化与AOF持久化

    Redis是一个键值对数据库服务器,服务器中通常包含着任意个非空数据库,而每个非空数据库中又可以包含任意个键值对,为了方便起见,我们将服务器中的非空数据库以及它们的键值对统称为数据库状态. 因为Red ...

  6. unity 加载读取外部XML

    cfg.xml <rootNode> <category name="网站"> <item name="mainPage"> ...

  7. 【344】Jupyter relevant problems

    参考:Jupyter Notebook Tutorial: The Definitive Guide 参考:ipython notebook 如何修改一开始打开的文件夹路径? Ref: Install ...

  8. Process子类

    创建新的进程还能够使用类的方式,可以自定义一个类,继承Process类,每次实例化这个类的时候,就等同于实例化一个进程对象,请看下面的实例: from multiprocessing import P ...

  9. uboot的配置及编译

    1. 先执行配置命令 make board_name_config 再执行编译命令 make all 2. 通过在Makefile中找到 board_name_config 目标,可以查看为了得到目标 ...

  10. linux 下 安装nginx及压力测试

    linux 编译安装nginx,配置自启动脚本 下载nginx: wget http://nginx.org/download/nginx-1.8.0.tar.gz下载openssl : wget h ...