Tree
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 30928   Accepted: 10351

Description

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. 

Input

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. 

Output

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

Sample Input

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

Sample Output

8

题目大意:

给定一棵n元树,求有多少点对使得这两点的距离小于等于k。

树分治经典题。

以下删改自sdj222555的CSDN博客。

需要分治。可以看09年漆子超的论文。本题用到的是关于点的分治。

一个重要的问题是,为了防止退化,所以每次都要找到树的重心然后分治下去,所谓重心,就是删掉此结点后,剩下的结点最多的树结点个数最小。

每次分治,我们首先算出重心,为了计算重心,需要进行两次dfs,第一次把以每个结点为根的子树大小求出来,第二次是从这些结点中找重心。

当然在一次dfs过程中也能做到。另外虽然说不记忆化也能做到,但还是用个son数组记忆下吧。

另外,无向图的dfs不需要像我之前一样遇一个点打个vis标记,dfs定义成dfs(u,pa),保存该点的父亲就不会返回去啦。

找到重心后,需要统计所有结点到重心的距离,看其中有多少对小于等于K。

这里采用的方法就是把所有的距离存在一个数组里(不要忘了重心到自己的),进行快速排序,这是nlogn的,然后用一个经典的相向搜索O(n)时间内解决。

但是这些求出来满足小于等于K的里面只有那些路径经过重心的点对才是有效的,也就是说在同一颗子树上的肯定不算数的。所以对每颗子树,把子树内部的满足条件的点对减去。

最后的复杂度是n logn logn    其中每次快排是nlogn 而递归的深度为logn。

代码实现比较复杂,今后有时间还是可以再敲一次。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
typedef long long ll;
const int maxn=;
const int inf=; int n,k; int to[maxn*+];
int w[maxn*+];
int next[maxn*+];
int head[maxn+];
int cnt; void addedge(int a,int b,int c)
{
to[cnt]=b;w[cnt]=c;
next[cnt]=head[a];head[a]=cnt++;
} int ans;
int mins,root;
int son[maxn+];
int vis[maxn+];
int depth[maxn+];
int dis[maxn+],tot; void getroot(int u,int pa,int num)//求重心
{
int maxs=;
son[u]=;
for(int i=head[u];i!=-;i=next[i])
{
int l=to[i];
if(l!=pa&&!vis[l])
{
getroot(l,u,num);
maxs=std::max(maxs,son[l]);
son[u]+=son[l];
}
}
maxs=std::max(maxs,num-son[u]);
if(maxs<mins)
{
mins=maxs;
root=u;
}
} void getdepth(int u,int pa)//通过深度得出需要的dis值
{
for(int i=head[u];i!=-;i=next[i])
{
int l=to[i];
if(l!=pa&&!vis[l])
{
depth[l]=depth[u]+w[i];
dis[tot++]=depth[l];
getdepth(l,u);
}
}
} int calc(int u,int pa,int d)
{
depth[u]=;
tot=;
dis[tot++]=;
getdepth(u,pa);
int ret=;
std::sort(dis,dis+tot);
int i=,j=tot-;
while(i<j)//经典的相向搜索
{
while(dis[i]+dis[j]+d*>k&&i<j)
j--;
ret+=j-i;
i++;
}
return ret;
} void dfs(int x,int num)
{
mins=inf;
getroot(x,-,num);//找重心
ans+=calc(root,-,);//计算整棵树符合条件的对数
for(int i=head[root];i!=-;i=next[i])
{
int l=to[i];
if(!vis[l])
ans-=calc(l,root,w[i]);//减去每棵子树符合条件的对数
}
vis[root]=;//打标记
for(int i=head[root];i!=-;i=next[i])
{
int l=to[i];
if(!vis[l])
dfs(l,son[l]);//向子树递归
}
} int main()
{
while(scanf("%d%d",&n,&k),n||k)
{
memset(head,-,sizeof(head));
cnt=;
for(int i=,a,b,c;i<=n-;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
} memset(vis,,sizeof(vis));
ans=;
dfs(,n); printf("%d\n",ans);
}
return ;
}

poj 1741 Tree (树的分治)的更多相关文章

  1. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  2. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  3. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

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

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

  5. POJ 1741 Tree 树分治

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

  6. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  7. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  8. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  9. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

随机推荐

  1. python_08

    一.作业 ''' 主页: 图标地址.下载次数.大小.详情页地址 详情页: 游戏名.好评率.评论数.小编点评.下载地址.简介.网友评论.1-5张截图链接地址. https://www.wandoujia ...

  2. 新闻网页通用抽取器GNEv0.04版更新,支持提取正文图片与源代码

    GeneralNewsExtractor以下简称GNE是一个新闻网页通用抽取器,能够在不指定任何抽取规则的情况下,把新闻网站的正文提取出来. 我们来看一下它的基本使用方法. 安装 GNE 使用 pip ...

  3. html基础——a标签

    a标签:超链接/锚点链接  实现页面跳转  只占据自己内容大小的位置 超链接: 使用 target="_self":表示在本页面跳转到 href 中的地址 target=" ...

  4. Spring与Shiro整合 登陆操作

    Spring与Shiro整合 登陆操作 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 编写登陆Controller方法  讲解: 首先,如果你登陆失败的时候,它会把你的异常信息丢到 ...

  5. python+selenium +unittest生成HTML测试报告

    python+selenium+HTMLTestRunner+unittest生成HTML测试报告 首先要准备HTMLTestRunner文件,官网的HTMLTestRunner是python2语法写 ...

  6. es6 every的使用

    let arr2 =[1,3,5,7,9,10]; //arr2.every() 数组里面所有的元素都有符合条件,才返回true var b =arr2.every(function (val,ind ...

  7. JAVA中字符串常见操作

    String str1="hello,world";String str2="Hello,World"; 1.字符串的比较:例,System.out.print ...

  8. Net Framework 4个Timer(网络收集整理)

    在 Visual Studio .NET 和 .NET Framework 中有四种计时器控件: (前边三种转载自 http://blog.csdn.net/aptentity/article/det ...

  9. 2. Python环境安装

    Centos 下环境安装 我们通过pyenv来管理python环境,更好的帮助开发者避免在环境上出现各种各样的问题 准备工作 安装之前,确保已经安装了git yum install git -y 安装 ...

  10. kubeadm配置高可用etcd集群

    操作系统为ubuntu18 kubernetes版本为v1.15.1 k8s默认在控制平面节点上的kubelet管理的静态pod中运行单个成员的etcd集群,但这不是高可用的方案. etcd高可用集群 ...