传送门

时隔一个月再次写点分治,比上一次要深入理解很多了。(虽然代码还是写不熟

模板题,不多说

//POJ 1741
//by Cydiater
//2016.9.22
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
#define ll long long
#define up(i,j,n)        for(int i=j;i<=n;i++)
#define down(i,j,n)        for(int i=j;i>=n;i--)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
inline int read(){
    char ch=getchar();int x=0,f=1;
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int N,K,LINK[MAXN],len=0,root,siz[MAXN],sum,max_siz[MAXN],ans,dis[MAXN],head,tail,q[MAXN];
bool vis[MAXN];
struct edge{
    int y,next,v;
}e[MAXN];
namespace solution{
    inline void insert(int x,int y,int v){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;e[len].v=v;}
    void init(){
        if(N==0&&K==0)exit(0);
        len=ans=root=0;
        memset(LINK,0,sizeof(LINK));
        memset(vis,0,sizeof(vis));
        up(i,2,N){
            int x=read(),y=read(),v=read();
            insert(x,y,v);
            insert(y,x,v);
        }
    }
    void make_root(int node,int fa){
        siz[node]=1;max_siz[node]=0;
        for(int i=LINK[node];i;i=e[i].next)if(!vis[e[i].y]&&e[i].y!=fa){
            make_root(e[i].y,node);
            siz[node]+=siz[e[i].y];
            max_siz[node]=max(max_siz[node],siz[e[i].y]);
        }
        max_siz[node]=max(max_siz[node],sum-max_siz[node]);
        if(max_siz[node]<max_siz[root])root=node;
    }
    void get_deep(int node,int fa){
        q[++tail]=dis[node];
        for(int i=LINK[node];i;i=e[i].next)if(!vis[e[i].y]&&e[i].y!=fa){
            dis[e[i].y]=dis[node]+e[i].v;
            get_deep(e[i].y,node);
        }
    }
    int col(int node,int dist){
        int tmp=0;
        dis[node]=dist;head=1;tail=0;
        get_deep(node,0);
        sort(q+1,q+tail+1);
        while(head<tail){
            while(q[head]+q[tail]>K&&head<tail)tail--;
            tmp+=tail-head;
            head++;
        }
        return tmp;
    }
    void work(int node){
        ans+=col(node,0);vis[node]=1;
        for(int i=LINK[node];i;i=e[i].next)
            if(!vis[e[i].y]){
                ans-=col(e[i].y,e[i].v);
                sum=siz[e[i].y];root=0;
                make_root(e[i].y,node);
                work(root);
            }
    }
    void slove(){
        root=N;sum=0;max_siz[0]=oo;
        make_root(1,0);
        work(root);
    }
    void output(){
        printf("%d\n",ans);
    }
}
int main(){
    //freopen("input.in","r",stdin);
    using namespace solution;
    while(scanf("%d %d",&N,&K)!=EOF){
        init();
        slove();
        output();
    }
    return 0;
}

POJ1741:tree的更多相关文章

  1. POJ1741 Tree + BZOJ1468 Tree 【点分治】

    POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...

  2. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  3. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  4. POJ1741 tree 【点分治】

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25286   Accepted: 8421 Description ...

  5. POJ1741 Tree(树的点分治基础题)

    Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...

  6. POJ1741 Tree (点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25772   Accepted: 8566 Description ...

  7. POJ1741——Tree(树的点分治)

    1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...

  8. POJ-1741 Tree (树上点分治)

    题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...

  9. poj1741 Tree(点分治)

    题目链接:http://poj.org/problem?id=1741 题意:求树上两点之间距离小于等于k的点对的数量 思路:点分治模板题,推荐一篇讲的非常好的博客:https://blog.csdn ...

随机推荐

  1. bloom filter

    Bloom filter 是由 Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员. 结    构 二进制 召回率 ...

  2. Android回调

    当A页面跳往B页面做一些操作后,再从B页面回到A页面时,A页面想要回去一些B页面操作的数据时,我们一般会使用回调. 1 public class MainActivity extends Activi ...

  3. mybatis缓存学习笔记

    mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一级缓存为线程内缓存,二级缓存为线程间缓存. 一提缓存,必是查询.缓存的作用就是查询快.写操作只能使得缓 ...

  4. The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。

    项目上右键-->Build Path-->Configuration Build Path -->Add Library -->Server Runtime 选择tomcat

  5. c#模拟表单POST数据,并获取跳转之后的页面

    直接看代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  6. tttttabs

    <div id="fil-page" class="fil-page"> <div class="fil-container&quo ...

  7. Ubuntu下安装IDA pro

    预备 由于IDA pro只能装在32位环境下,如果是64位Ubuntu,需要运行如下命令安装32位的必备库. sudo dpkg --add-architecture i386 sudo apt-ge ...

  8. Shell命令_if

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #if if [ 条件判断式 ] ...

  9. ie-css3.htc 可以让IE低版本浏览器支持CSS3 的一个小工具

    ie-css3.htc 先说道说道这斯是弄啥嘞 ie-css3.htc是一个可以让IE浏览器支持部份CSS3属性的htc文件,不只是box-shadow,它还可以让你的IE浏览器支持圆角属性borde ...

  10. quartz介绍

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...