poj 1741Tree
来源:互联网 发布:刷机解网络锁 编辑:程序博客网 时间:2024/06/11 20:13
Tree
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
树点分治例题
大神的论文:《分治算法在树的路径问题中的应用》
(分治思想):把问题分成形式相同,规模更小的子问题。
对于一棵有根树, 树中满足要求的一条路径,必然是以下两种情况之一:
1、经过根节点
2、不经过根节点,在根节点的一棵子树中
对于情况2,可以递归(重复步骤1,2)求解,主要考虑情况1(因题而异,注意去重)。
找根节点就是重点了,因为要尽可能减少深度,所以要找一个使子树中size最大的尽量小(也就是 重心})。然后使重心作为根,继续操作(记得标记是否找过)。
ps:与“最近点对”有异曲同工之妙
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int num,t,K,ans;#define N 10005int to[N+N],head[N],Next[N+N],val[N+N];int a[N],deep[N],size[N];bool flag[N];void add(int u,int v,int z){ ++num; to[num]=v; val[num]=z; Next[num]=head[u]; head[u]=num;}void getsize(int u,int fa){ size[u]=1; for (int i=head[u];i;i=Next[i]) if (to[i]!=fa&&flag[to[i]]){ getsize(to[i],u); size[u]+=size[to[i]]; }}//预处理sizeint getroot(int u,int fa,int ma){ for (int i=head[u];i;i=Next[i]) if (to[i]!=fa&&flag[to[i]]&&size[to[i]]+size[to[i]]>ma) return getroot(to[i],u,ma); return u;}//找重心void getdeep(int u,int fa){ a[t++]=deep[u]; for (int i=head[u];i;i=Next[i]) if (to[i]!=fa&&flag[to[i]]) deep[to[i]]=deep[u]+val[i],getdeep(to[i],u);}int calc(int u,int ss){ t=0; deep[u]=ss; getdeep(u,-1); sort(a,a+t); int l=0,r=t-1,xx=0; while (l<r){ if (a[l]+a[r]>K){--r;continue;} xx+=r-l; ++l; } return xx; }void work(int u){ flag[u]=false; getsize(u,-1); ans+=calc(u,0); for (int i=head[u];i;i=Next[i]) if (flag[to[i]]){ ans-=calc(to[i],val[i]); int rt=getroot(to[i],u,size[to[i]]); work(rt); }}int main(){ int n; while (scanf("%d%d",&n,&K)!=EOF){ if (n==0&&K==0) break; ans=0; num=0; for (int i=1;i<=n;++i) head[i]=0,flag[i]=true; for (int i=1;i<n;++i){ int u,v,z; scanf("%d%d%d",&u,&v,&z); add(u,v,z); add(v,u,z); } getsize(1,-1); int rt=getroot(1,-1,size[1]); work(rt); printf("%d\n",ans); } return 0;}
- POJ 1741 Tree
- POJ 1741 Tree
- 【POJ 1741】Tree
- poj-1741 Tree
- [POJ 1741]Tree
- poj 1741 bzoj1468 Tree
- POJ 1741Tree
- Poj 1741 Tree
- POJ 1741 Tree
- POJ 1741 Tree
- POJ 1741 Tree
- Poj 1741 Tree
- 【POJ】1741 Tree
- POJ 1741 Tree
- poj 1741Tree
- poj 1741 tree
- POJ 1741 Tree 笔记
- POJ 1741 Tree
- 学习Linux命令(11)
- 1033. 旧键盘打字(20) PAT
- 数组学习
- 概要
- THINKPHP 错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'
- poj 1741Tree
- 单词拼写检查
- [opencv] lk光流法小结
- C语言程序设计(18)
- 进程间通信
- 【开发总结】Linux下获取物理网卡带宽最大值
- 怎么才能快速掌握java web进行项目开发
- 2014年第五届蓝桥杯C/C++程序设计本科B组决赛 出栈次序(结果填空)
- linux scp