树形dp--hdu 3534 Tree
来源:互联网 发布:上海淘宝摄影实景基地 编辑:程序博客网 时间:2024/06/06 19:33
Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 906 Accepted Submission(s): 268
Problem Description
In the Data structure class of HEU, the teacher asks one problem: How to find the longest path of one tree and the number of such longest path?
Input
There are several test cases. The first line of each case contains only one integer N, means there are N nodes in the tree. N-1 lines follow, each line has three integers w,v and len, indicate that there is one edge between node w and v., and the length of the edge is len.
Output
For each test case, output the length of longest path and its number in one line.
Sample Input
41 2 1002 3 502 4 5041 2 1002 3 503 4 50
Sample Output
150 2200 1
Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU
题意:给出一颗树(n个顶点,n-1条边)
求最长的路径,以及最长路径的条数。
路径无非就是连接两个点直接的路。
因为是一颗树,所以连接两个点肯定是唯一的路径。
其实就是求两点间距离的最大值,以及这个最大值有多少个。
首先统计出结点到叶子结点的最长距离和次长距离。
然后找寻经过这个点的,在这个为根结点的子树中的最长路径个数目。
思路参考于kuangbin博客
#include "stdio.h" //poj 3534 Tree 树形dp#include "string.h"#define N 100100struct node{ int x,y; int weight; int next;}edge[2*N];int idx,head[N];void Init(){ idx = 0; memset(head,-1,sizeof(head));}void Add(int x,int y,int weight){ edge[idx].x = x; edge[idx].y = y; edge[idx].weight = weight; edge[idx].next = head[x]; head[x] = idx++;}int maxn[N]; //最长距离int smaxn[N];//次长距离int maxn_num[N]; //最长距离数目int smaxn_num[N]; //次长距离数目int path[N]; //经过点i的最长距离长度int num[N]; //经过点i的最长距离数目void DFS(int x,int father){ int i,y; maxn[x] = smaxn[x] = 0; maxn_num[x] = smaxn_num[x] = 0; for(i=head[x]; i!=-1; i=edge[i].next) { y = edge[i].y; if(y==father) continue; DFS(y,x); if(maxn[x] < maxn[y]+edge[i].weight) { smaxn[x] = maxn[x]; smaxn_num[x] = maxn_num[x]; maxn[x] = maxn[y]+edge[i].weight; maxn_num[x] = maxn_num[y]; } else if(maxn[x]==maxn[y]+edge[i].weight) maxn_num[x] += maxn_num[y]; else if(smaxn[x] < maxn[y]+edge[i].weight) { smaxn[x] = maxn[y]+edge[i].weight; smaxn_num[x] = maxn_num[y]; } else if(smaxn[x] == maxn[y]+edge[i].weight) smaxn_num[x] += maxn_num[y]; } if(maxn_num[x]==0) //叶子节点,赋初值 { maxn[x] = smaxn[x] = 0; maxn_num[x] = smaxn_num[x] = 1; path[x] = 0; num[x] = 1; return ; } int c1; //统计以x为根节点的最长距离数目 int c2; //统计以x为根节点的次长距离数目 c1 = c2 = 0; for(i=head[x]; i!=-1; i=edge[i].next) { y=edge[i].y; if(y==father) continue; if(maxn[x] == maxn[y]+edge[i].weight) c1++; else if(smaxn[x] == maxn[y]+edge[i].weight) c2++; } path[x] = 0; num[x] = 0; if(c1>=2) { int tmp = 0; path[x] = maxn[x]*2; for(i=head[x]; i!=-1; i=edge[i].next) { y = edge[i].y; if(y==father) continue; if(maxn[x]==maxn[y]+edge[i].weight) { num[x] += tmp*maxn_num[y]; tmp += maxn_num[y]; } } } else if(c1>=1 && c2>=1) { path[x] = maxn[x]+smaxn[x]; for(i=head[x]; i!=-1; i=edge[i].next) { y = edge[i].y; if(y==father) continue; if(maxn[x]==maxn[y]+edge[i].weight) { num[x] = smaxn_num[x]*maxn_num[y]; } } } else { path[x] = maxn[x]; num[x] = maxn_num[x]; }}int main(){ int n; int i,j; int x,y,k; while(scanf("%d",&n)!=EOF) { Init(); for(i=1; i<n; ++i) { scanf("%d %d %d",&x,&y,&k); Add(x,y,k); Add(y,x,k); } DFS(1,-1); int dist=0,snum=0; for(i=1; i<=n; ++i) { if(path[i]>dist) { dist = path[i]; snum = num[i]; } else if(path[i]==dist) { snum += num[i]; } } printf("%d %d\n",dist,snum); } return 0;}
0 0
- 树形dp--hdu 3534 Tree
- HDU 3534 Tree 树形dp统计
- hdu 3534 Tree(树形dp)
- HDU 3534 Tree(经典树形dp)
- 树中最长路径 树形DP HDU 3534 Tree
- hdu 3534 (树形DP)
- HDU 5293 Tree chain problem【树形DP】
- hdu 5379Mahjong tree 树形dp
- hdu 5379 Mahjong tree(树形dp)
- HDU 5416 CRB and Tree (树形DP)
- HDU 5416 CRB and Tree (树形dp)
- hdu 5379 Mahjong tree 树形DP入门
- [树形DP FWT] HDU 5909 Tree Cutting
- HDU 6035 Colorful Tree [树形dp]
- hdu 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree (树形dp)
- HDU 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree(树形dp)
- ZooKeeper做独立服务器运行(下)
- 招聘要求看技术发展
- echo命令
- 多线程编程实例:不带缓冲的多线程文件复制(使用队列,互斥,条件变量)
- 用来判断是否重复录入
- 树形dp--hdu 3534 Tree
- JVM学习笔记1(类加载器)
- HDU 2176(基础尼姆)从n堆石堆中取石子
- 使用CGLIB实现AOP功能核心代码
- 修改密码
- 最小表示法模板
- 五月三号训练赛
- 架构与耦合-Strust2 使用配置文件管理常量
- Springmvc构造RESTful详细讲解