题目描述

Give a tree with n vertices,each edge has a length(positive integer less than 1001). 
Define dist(u,v)=The min distance between node u and v. 
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k. 
Write a program that will count how many pairs which are valid for a given tree. 

输入

The input contains several test cases. The first line of each test case contains two integers n, k. (n<=10000) The following n-1 lines each contains three integers u,v,l, which means there is an edge between node u and v of length l. 
The last test case is followed by two zeros. 

输出

For each test case output the answer on a single line.

样例输入

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

样例输出

8


题目大意

多组测试数据,每次输入n、m,和一棵n个点的有边权的树,问你满足x到y距离小于等于m的无序点对(x,y)的个数是多少。

题解

树的点分治模板题,第一次写

考虑到路径只有两种情况,一是经过根节点,二是不经过根节点。

如果不经过根节点,那么一定经过最小公共子树的根节点,可以转化为问题一的子问题。

于是考虑怎么递归解决问题一。

对于根节点进行一次dfs,求出deep,并将其从小到大排序。

避免重复,只需要求出其中deep[x]≤deep[y]且deep[x]+deep[y]≤m的个数。

用i表示左指针,j表示右指针,i从左向右遍历。

如果deep[i]+deep[j]≤m,则点对(i,t)(i<t≤j)都符合题意,将j-i加入答案中,并且i++;否则j--。

然而这样还会重复计算在同一棵子树中的点对,所以再进行下一步dfs之前需要减去重复部分。

但是这样做会TLE。为什么?因为树可能会退化,导致选择链头时时间复杂度极大。

于是每次不能固定选择root,而是以重心作为root去处理,这样能保证时间复杂度再O(nlog2n)以下。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 10010
using namespace std;
int m , head[N] , to[N << 1] , len[N << 1] , next[N << 1] , cnt , si[N] , deep[N] , root , vis[N] , f[N] , sn , d[N] , tot , ans;
void add(int x , int y , int z)
{
to[++cnt] = y , len[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
}
void getroot(int x , int fa)
{
f[x] = 0 , si[x] = 1;
int i;
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa && !vis[to[i]])
getroot(to[i] , x) , si[x] += si[to[i]] , f[x] = max(f[x] , si[to[i]]);
f[x] = max(f[x] , sn - si[x]);
if(f[root] > f[x]) root = x;
}
void getdeep(int x , int fa)
{
d[++tot] = deep[x];
int i;
for(i = head[x] ; i ; i = next[i])
if(to[i] != fa && !vis[to[i]])
deep[to[i]] = deep[x] + len[i] , getdeep(to[i] , x);
}
int calc(int x)
{
tot = 0 , getdeep(x , 0) , sort(d + 1 , d + tot + 1);
int i = 1 , j = tot , sum = 0;
while(i < j)
{
if(d[i] + d[j] <= m) sum += j - i , i ++ ;
else j -- ;
}
return sum;
}
void dfs(int x)
{
deep[x] = 0 , vis[x] = 1 , ans += calc(x);
int i;
for(i = head[x] ; i ; i = next[i])
if(!vis[to[i]])
deep[to[i]] = len[i] , ans -= calc(to[i]) , sn = si[to[i]] , root = 0 , getroot(to[i] , 0) , dfs(root);
}
int main()
{
int n , i , x , y , z;
while(scanf("%d%d" , &n , &m) && (n || m))
{
memset(head , 0 , sizeof(head));
memset(vis , 0 , sizeof(vis));
cnt = 0 , ans = 0;
for(i = 1 ; i < n ; i ++ )
scanf("%d%d%d" , &x , &y , &z) , add(x , y , z) , add(y , x , z);
f[0] = 0x7fffffff , sn = n;
root = 0 , getroot(1 , 0) , dfs(root);
printf("%d\n" , ans);
}
return 0;
}

【poj1741】Tree 树的点分治的更多相关文章

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

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

  2. hdu 4812 D Tree(树的点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total ...

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

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

  4. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...

  5. POJ1741(SummerTrainingDay08-G 树的点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23380   Accepted: 7748 Description ...

  6. hdu4812-D Tree (树的点分治)

    昨天学了下树分治,今天补这道题,还是太不熟练了,写完之后一直超时.后来查出好多错= =比如v,u写倒了,比如+写成了取最值,比如....爆int...查了两个多小时的错..哭...(没想到进首页了 h ...

  7. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  8. 树上点对统计poj1741(树的点分治)

    给定一棵树,边上有权值,要统计有多少对点路径的权值和<=k 分治算法在树的路径中的应用 这个论文里面有分析. 任意两点的路径,要么过根结点,要么在子树中.如果在子树中,那么只要递归处理就行了. ...

  9. POJ 1741 Tree(树的点分治,入门题)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description ...

随机推荐

  1. 20165223 实验一 Java开发环境的熟悉

    目录 一.实验报告封面 二.实验内容 (一)命令行下的JAVA程序开发 (二)IDEA中的JAVA程序开发 (三)自主练习 三.实验总结 四.PSP时间 一.实验报告封面 北京电子科技学院(BESTI ...

  2. 如何搭建高可用redis架构?

    如何搭建高可用redis架构? 温国兵 架构师小秘圈 昨天 作者:温国兵,曾任职于酷狗音乐,现为三七互娱 DBA.目前主要关注领域:数据库自动化运维.高可用架构设计.数据库安全.海量数据解决方案.以及 ...

  3. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  4. C# Winform多窗体&&构造函数传值

    一.多窗体:三种打开窗体的状态: 最最基础的弹窗: //写在按钮的点击事件内: //实例需要弹出的窗口的类: Form2 f2 = new Form2(); f2.Show(); 1.弹窗窗口: // ...

  5. 收藏:win32 控件之 sysLink控件(超链接)

    来源:https://blog.csdn.net/dai_jing/article/details/8683487 手动创建syslink(msdn): CreateWindowEx(, WC_LIN ...

  6. ImageMagick - MAGICK_CODER_MODULE_PATH 测试结果, 很受伤

    //通过查看 ImageMagick 源代码: http://code.metager.de/source/xref/ImageMagick/MagickCore/module.c#552 //首先会 ...

  7. dos下编译java

    dos下运行java程序,不借助其他的IDE,即可编译运行java程序. 工具/原料   电脑 方法/步骤     打开开始,运行cmd,进入dos界面.   分别运行java,和javac,检测jd ...

  8. glob.glob 匹配文件

    glob.glob以列表形式返回匹配的文件路径 只有一个参数:文件的匹配规则 e.g. >>>res_home = '/vip_data_center/test_envs/train ...

  9. java中一维数组的定义和遍历

    public class ArrayDemo1{ public static void main(String[] args){ //1 定义数组 并同时赋值 int[] arr = new int[ ...

  10. aspcms逻辑错误导致后台地址泄露

    访问即可跳转后台地址: URL:http://www.xxx.org.cn/plug/oem/AspCms_OEMFun.asp 注入:plug/comment/commentList.asp?id= ...