Tree POJ
来源:互联网 发布:最好的中国象棋软件 编辑:程序博客网 时间:2024/06/09 00:40
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
大致题意:让你找出一颗树中有多少点对,满足它们之间的最短距离小于K
思路:点分治模板
代码如下
#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cstring>using namespace std;const int maxn=1e4+5;struct Edge//链式向前星存边{ int to,cost; int next;}edge[maxn*2];int tol,head[maxn];int vis[maxn];int size[maxn];//子树的大小 int maxv[maxn];//最大孩子节点的size int dis[maxn];int ans,root,MAX;int N,K;int num;void init(){ tol=0; ans=0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis));}void add(int u,int v,int w){ edge[tol].to=v; edge[tol].cost=w; edge[tol].next=head[u]; head[u]=tol++;}//处理子树大小 void dfs_size(int u,int f){ size[u]=1; maxv[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int to=edge[i].to; if(to==f||vis[to]) continue; dfs_size(to,u); size[u]+=size[to]; maxv[u]=max(maxv[u],size[to]); } }//找重心 void dfs_root(int r,int u,int f){ maxv[u]=max(maxv[u],size[r]-size[u]);//size[r]-size[u]是u上面部分的树的尺寸,跟u的最大孩子比,找到最大孩子的最小差值节点 if(maxv[u]<MAX) { MAX=maxv[u]; root=u; } for(int i=head[u];i!=-1;i=edge[i].next) { int to=edge[i].to; if(to==f||vis[to]) continue; dfs_root(r,to,u); }}//求每个点离重心的距离 void dfs_dis(int u,int d,int f){ dis[num++]=d; for(int i=head[u];i!=-1;i=edge[i].next) { int to=edge[i].to; if(to==f||vis[to]) continue; dfs_dis(to,d+edge[i].cost,u); } }//计算以u为根的子树中有多少点对的距离小于等于Kint calc(int u,int d){ int ret=0; num=0; dfs_dis(u,d,0); sort(dis,dis+num); int i=0,j=num-1; while(i<j) { while(dis[i]+dis[j]>K&&i<j) j--; ret+=j-i; i++; } return ret;}void dfs(int u){ MAX=N; dfs_size(u,0); dfs_root(u,u,0); ans+=calc(root,0); vis[root]=1; for(int i=head[root];i!=-1;i=edge[i].next) { int to=edge[i].to; if(!vis[to]) { ans-=calc(to,edge[i].cost); dfs(to); } }}int main() { while(scanf("%d%d",&N,&K)!=EOF&&N&&K) { int u,v,w; init(); for(int i=1;i<N;i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } dfs(1); printf("%d\n",ans); } return 0; }
- POJ Tree
- Tree POJ
- poj-2255-Tree Recovery(tree)
- POJ 3321 Apple Tree
- poj 2255 Tree Recovery
- POJ 1848 Tree
- POJ 1145 Tree Summing
- POJ 3321 Apple Tree
- Tree Recovery---POJ--2255
- POJ 2255 Tree Recovery
- poj 1848 Tree
- poj 1145 Tree Summing
- POJ 2255 Tree Recovery
- poj 3321 Apple Tree
- poj 2367 Genealogical tree
- poj 2367 Genealogical tree
- poj 2367 Genealogical tree
- poj 2255 Tree Recovery
- Vue学习基础之vue-resource和vue axios
- 【Apollo源码分析】系列的第二部分【perception】
- QT_关于自绘菜单项类QWidgetAction
- zookeeper集群部署后启动报错ZooKeeper JMX enabled by default...解决方案二
- 2017多校-10
- Tree POJ
- 排序算法(1)—冒泡排序
- 网关、路由器、网桥,中继器,交换机的区别
- 死锁案例及查看方法
- 用JAVA写贪吃蛇游戏 超详细注释及原理讲解
- 算法系列——从尾到头打印链表(剑指offer)
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest K. Knights of the Old Republic kruskal
- 【面试】HTTP中Get与Post的区别
- AS3 前端网页游戏系统的开发