Tree

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

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

题目链接:http://poj.org/problem?id=1741
题目大意:

给定一棵N个节点、边上带权的树,再给出一个K,询问有多少个数对(i,j)满足i<j,且i与j两点在树上的距离小于等于K。

数据规模:

多组测试数据,每组数据满足N≤10000,1≤边上权值≤1000,1≤K≤10^9。

出处:

楼天城男人必做8题之一……

思路:

最容易想到的算法是:从每个点出发遍历整棵树,统计数对个数。

由于时间复杂度O(N^2),明显是无法满足要求的。

对于一棵有根树, 树中满足要求的一个数对所对应的一条路径,必然是以下两种情况之一:

1、经过根节点

2、不经过根节点,也就是说在根节点的一棵子树中

对于情况2,可以递归求解,下面主要来考虑情况1。

设点i的深度为Depth[i],父亲为Parent[i]。

若i为根,则Belong[i]=-1,若Parent[i]为根,则Belong[i]=i,否则Belong[i]=Belong[Parent[i]]。

这三个量都可以通过一次BFS求得。

我们的目标是要统计:有多少对(i,j)满足i<j,Depth[i]+Depth[j]<=K且Belong[i]<>Belong[j]

如果这样考虑问题会变得比较麻烦,我们可以考虑换一种角度:

设X为满足i<j且Depth[i]+Depth[j]<=K的数对(i,j)的个数

设Y为满足i<j,Depth[i]+Depth[j]<=K且Belong[i]=Belong[j]数对(i,j)的个数

那么我们要统计的量便等于X-Y

求X、Y的过程均可以转化为以下问题:

已知A[1],A[2],...A[m],求满足i<j且A[i]+A[j]<=K的数对(i,j)的个数

对于这个问题,我们先将A从小到大排序。

设B[i]表示满足A[i]+A[p]<=K的最大的p(若不存在则为0)。我们的任务便转化为求出A所对应的B数组。那么,若B[i]>i,那么i对答案的贡献为B[i]-i。

显然,随着i的增大,B[i]的值是不会增大的。利用这个性质,我们可以在线性的时间内求出B数组,从而得到答案。

综上,设递归最大层数为L,因为每一层的时间复杂度均为“瓶颈”——排序的时间复杂度O(NlogN),所以总的时间复杂度为O(L*NlogN)

然而,如果遇到极端情况——这棵树是一根链,那么随意分割势必会导致层数达到O(N)级别,对于N=10000的数据是无法承受的。因此,我们在每一棵子树中选择“最优”的点分割。所谓“最优”,是指删除这个点后最大的子树尽量小。这个点可以通过树形DP在O(N)时间内求出,不会增加时间复杂度。这样一来,即使是遇到一根链的情况时,L的值也仅仅是O(logN)的。

因此,改进后算法时间复杂度为O(Nlog^2N),可以AC。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int n,m,root,t,ans,allnode,siz[N],K,head[N],vis[N],d[N];
int deep[N];//路径长度//deep[0]子节点个数
int f[N];//重心 struct edg{int to,next,v;}e[N * ];//前向星存边
void add(int u,int v,int w) {e[t].to=v;e[t].next=head[u];e[t].v=w;head[u]=t++;}//加边 //获取重心
void getroot(int x,int fa) {
siz[x] = ;
f[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
getroot(to,x);
siz[x] += siz[to];
f[x] = max(f[x] , siz[to]);
}
f[x] = max(allnode-siz[x] , f[x]);
if(f[x] < f[root]) root = x;
} void getdeep(int x,int fa) {//获取子树所有节点与根的距离
deep[++deep[]] = d[x];
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
d[to] = d[x] + e[i].v;
getdeep(to,x);
}
}
int cal(int x,int now) {//计算当前以重心x的子树下,所有情况的答案
d[x]=now;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int all = ;
for(int l=,r=deep[];l<r;) {
if(deep[l]+deep[r] <= K) {all += r-l;l++;}
else r--;
}
return all;
} void work(int x) {//以x为重心进行计算
vis[x] = ;
ans+=cal(x,);
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
ans -= cal(to,e[i].v);
allnode = siz[to];
root=;
getroot(to,x);
work(root);
}
} int main()
{
while(~scanf("%d%d",&n,&K)) {
if(!n&&!m) break;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
t = ;
for(int i=;i<n;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c) , add(b,a,c);
}
root=ans=;
allnode=n;f[]=inf;
getroot(,);
work(root);
printf("%d\n",ans);
}
}

最新文章

  1. .net中 页面包含子页面 类似include的功能--(记录九)
  2. MMORPG大型游戏设计与开发(服务器 AI 控制器)
  3. 一道关于Promise应用的面试题
  4. Rails : 产品环境(生产环境)的部署
  5. node代码片段
  6. iOS中POST异步请求
  7. Javascript的一个生产PDF的库: unicode和中文问题的解决
  8. 省市区三级联动(二)JS部分简单版
  9. 通过AOP 实现异常统一管理
  10. custom activities
  11. iozone文件系统测试 与EXCEL 制图
  12. Bloom Filter 算法具体解释
  13. Android应用之基本的组件(一)
  14. 频繁模式挖掘中Apriori、FP-Growth和Eclat算法的实现和对比
  15. jquery easyui datagrid改变某行的值
  16. SELinux策略语言--客体类别和许可
  17. 发现一个好工具RenderDoc
  18. Open系列相关概念汇总
  19. perl trick
  20. This Jenkins instance appears to be offline

热门文章

  1. UIImage类方法总结及UIImage生成方法对比
  2. SQL Server中varchar和nvarchar的区别
  3. window下mysql数据备份
  4. inode 详解
  5. My Go Resolutions for 2017(from Russ cox&#39;s blog)
  6. jQuery Ajax post多个值传参
  7. ADO.NET查询和操作数据库
  8. sp_tableoption
  9. [Java] 在 jar 文件中读取 resources 目录下的文件
  10. java基础,集合,HashMap,源码解析