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

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

Source

 
题意:一棵有n个结点的树,求距离不超过k的点对
思路:树分治。n比较大,直接枚举所有点对肯定是不行的。按照重心把树分成若干子树,那么所有的点对一定属于 1)点u、v属于同一子树的点对; 2)点u、v属于不同子树的定点对; 3)重心s和其他点组成点对。1)情况可以通过递归得到。2)情况,只要先求出每个点到重心s的距离,就可以统计出和不超过k的点对数。而3)情况,添加一个0的顶点,就成为了情况2)。需要注意的是,需要避免重复统计,即应该在1)中统计的属于同一子树的点对,要避免在2)中进行统计。递归深度最多为log(n)层,每层总共有n个结点。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<bitset>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define bug(x) cout<<"bug"<<x<<endl;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int> P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
int w;
int next;
};
edge es[M];
int cut,head[N];
int si[N],maxx[N];
bool vis[N];
int deep[N];
int k;
int root,ans;
void init()
{
cut=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w)
{
cut++;
es[cut].from=u,es[cut].to=v;
es[cut].w=w;
es[cut].next=head[u];
head[u]=cut;
}
int getroot(int u,int fa,int n)
{
si[u]=,maxx[u]=;
for(int i=head[u]; i!=-; i=es[i].next)
{
int v=es[i].to;
if(v==fa||vis[v]) continue;
si[u]+=getroot(v,u,n);
maxx[u]=max(maxx[u],si[v]);
}
maxx[u]=max(maxx[u],n-si[u]);
if(maxx[u]<maxx[root]) root=u;
return si[u];
}
void getdeep(int u,int fa,int d)
{
deep[++deep[]]=d;
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa||vis[e.to]) continue;
getdeep(e.to,u,d+e.w);
}
}
int cal(int u,int fa,int d)
{
deep[]=;
getdeep(u,fa,d);
sort(deep+,deep+deep[]+);
int l=,r=deep[];
int res=;
while(l<r)
{
if(deep[l]+deep[r]<=k) res+=r-l,l++;
else r--;
}
return res;
}
void solve(int u)
{
vis[u]=true;
ans+=cal(u,,);///统计符合情况的点对数
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(vis[e.to]) continue;
ans-=cal(e.to,,e.w);///删除同一子树的点对数
root=;
getroot(e.to,,si[e.to]);
solve(root);///递归同一子树
}
}
int main()
{
int n;
while(scanf("%d%d",&n,&k)!=EOF)
{
if(n==&&k==) break;
init();
for(int i=; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
memset(vis,false,sizeof(vis));
root=,maxx[]=inf;
ans=;
getroot(,,n);
solve(root);
printf("%d\n",ans);
}
return ;
}

树分治

POJ 1741.Tree 树分治 树形dp 树上点对的更多相关文章

  1. POJ 1741 Tree 树分治

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

  2. 树的点分治 (poj 1741, 1655(树形dp))

    poj 1655:http://poj.org/problem?id=1655 题意: 给无根树,  找出以一节点为根,  使节点最多的树,节点最少. 题解:一道树形dp,先dfs 标记 所有节点的子 ...

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

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

  4. poj 1741 Tree (树的分治)

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

  5. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  6. [codeforces161D]Distance in Tree(点分治/树形dp)

    题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...

  7. POJ 1741 Tree ——点分治

    [题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...

  8. 『You Are Given a Tree 整体分治 树形dp』

    You Are Given a Tree Description A tree is an undirected graph with exactly one simple path between ...

  9. Tree POJ - 1741【树分治】【一句话说清思路】

    因为该博客的两位作者瞎几把乱吹(" ̄︶ ̄)人( ̄︶ ̄")用彼此的智慧总结出了两条全新的定理(高度复杂度定理.特异根特异树定理),转载请务必说明出处.(逃 Pass:anuonei, ...

随机推荐

  1. htm5-websocket实现数据查询应用

    htm5-websocket实现数据查询应用   在之前的文章讲述了使用Websocket调用远程方式的功能,在这基础我们可以简单地使用WebSocket进行数据处理方面的应用;只需要在方法执行相关的 ...

  2. uniDAC的安装和使用

    1.解压后把UniDAC文件夹 2.在UniDAC\Source\Delphi7文件夹中找到Make.bat文件,鼠标右键“编辑”确认DELPHI7的安装路径是否正确(建议:设置成绝对路径了,防止因为 ...

  3. 如何查看k8s存在etcd中的数据(转)

    原文 https://yq.aliyun.com/articles/561888 一直有这个冲动, 想知道kubernetes往etcd里放了哪些数据,是如何组织的. 能看到,才有把握知道它的实现和细 ...

  4. C语言 链表(Dev C++/分文件版)

    头文件:quechain.h struct Question { int _id; struct Question* pre; struct Question* next; }; void chain ...

  5. hadoop2.6.4集群笔记

    ---恢复内容开始--- 一,linux下的准备工作 1,修改主机名: vi /etc/sysconfig/network 2,修改ip vi /etc/sysconfig/network-scrip ...

  6. QTP 学习 - 检查点

  7. kettle实现同构单表增量同步

    job如下: 如下转换:获取区服列表,将id列表保存到结果(内存) job: 同构数据库单表抽取(每个输入执行一次) 同构数据库单表抽取(job) 的具体实现如下: 转换:获取数据库连接ID 从结果获 ...

  8. python3 pyinstaller生成exe文件过程问题解决记录

    1.使用pip安装pyinstaller 2.在cmd打开需生成可执行文件的python文件所在文件夹 3.使用命令pyinstaller -F -w **.py (代码中有import其他模块的,只 ...

  9. js day01

    一)什么是JavaScript[以下简称JS] JS是 (1)基于对象 JS本身就有一些现成的对象可供程序员使用,例如:Array,Math,String... JS并不排除你可以自已按一定的规则创建 ...

  10. RAD Studio 10.3 来了

    官方原版下载链接:HTTP FTP 官方更新说明:http://docwiki.embarcadero.com/RADStudio/Rio/en/What's_New [官方更新说明简译]1.Delp ...