HDU3534 Tree 求树的直径及其个数
来源:互联网 发布:linux内网ip映射到外网 编辑:程序博客网 时间:2024/06/06 17:38
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534
题目大意:给出一颗n个节点的树,每条边对应一个长度,求出距离最大的两个节点之间长度,并找出一共有多少个顶点对。
实现代码如下:
/*求最长边的时候可以统计一下最长边的个数,那么以u为根节点的子树中最长边为:Len[u]+max(len[v]),个数node[u]*node[v]。*/#include<stdio.h>#include<string.h>const int N=500000;const int inf=0x3fffffff;int head[N],num,Len[N],ans,node[N],k;struct edge{int ed,w,next;}e[N*2];void addedge(int x,int y,int w){e[num].ed=y;e[num].w=w;e[num].next=head[x];head[x]=num++;e[num].ed=x;e[num].w=w;e[num].next=head[y];head[y]=num++;}void dfs(int u,int fa){int i,v,temp;Len[u]=0;//最长边node[u]=1;//最长边的个数for(i=head[u];i!=-1;i=e[i].next){v=e[i].ed;if(v==fa)continue;dfs(v,u);temp=Len[v]+e[i].w;if(temp+Len[u]>ans)//最长边经过v{k=node[v]*node[u];ans=temp+Len[u];}else if(temp+Len[u]==ans)k+=node[v]*node[u];if(Len[u]<temp)//更新最长边{Len[u]=temp;node[u]=node[v];}else if(Len[u]==temp)//更新最长边的个数node[u]+=node[v];}}int main(){int n,i,x,y,w;while(scanf("%d",&n)!=-1){memset(head,-1,sizeof(head));num=0;for(i=1;i<n;i++){scanf("%d%d%d",&x,&y,&w);addedge(x,y,w);}ans=-inf;k=0;dfs(1,0);printf("%d %d\n",ans,k);}return 0;}
0 0
- HDU3534 Tree 求树的直径及其个数
- 求树的直径
- 求树的直径
- 求树的直径
- 求树的直径
- 树形DP(统计直径的条数 HDU3534)
- BFS求树的直径
- HDU4607(求树的直径)
- [toj3517]【求树的直径】
- 求树的直径证明
- hdu4607 (求树的直径)
- 求树的直径算法
- spfa求树的直径
- 求树的直径问题
- [Codeforces734E]Anton and Tree 树的直径
- 求树的直径(poj1383验证)
- [POJ-2631] 求树的直径
- hdu 4607 求树的直径
- tar详解
- No parameters defined during prepareCall()
- 全新升级版LOTRO商店: Turbine & Xsolla
- Android stuido 快捷键
- 九度OJ 1069:查找学生信息 (排序、查找)
- HDU3534 Tree 求树的直径及其个数
- 'ascii' codec can't encode character u'\u9ed1' in position 0: ordinal not in...
- 【读书笔记】HTML5程序设计 --- 第一章 HTML5 概述
- 操作系统与网络实现 之五
- 九度OJ 1070:今年的第几天? (日期计算)
- 数据结构之二叉树链表
- 高效工作——EA使用技巧
- 黑马程序员——OC语言日志——私有变量和私有方法
- Android Adapter深入理解与优化