Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1574    Accepted Submission(s): 511

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

4
1 2 100
2 3 50
2 4 50
4
1 2 100
2 3 50
3 4 50
 

Sample Output

150 2
200 1
 

Source

  1. //2017-08-16
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = ;
  10. const int INF = 0x3f3f3f3f;
  11.  
  12. //链式前向星存图
  13. int head[N], tot;
  14. struct Edge{
  15. int to, next, w;
  16.  
  17. }edge[N<<];
  18.  
  19. void add_edge(int u, int v, int w){
  20. edge[tot].w = w;
  21. edge[tot].to = v;
  22. edge[tot].next = head[u];
  23. head[u] = tot++;
  24. }
  25.  
  26. void init(){
  27. tot = ;
  28. memset(head, -, sizeof(head));
  29. }
  30.  
  31. //dp[u]记录以u为根的子树,过u往下的最长路径。
  32. //cnt[u]记录子树u上最长路径的数目。
  33. int dp[N], cnt[N], ans, num;
  34.  
  35. void dfs(int u, int fa){
  36. dp[u] = ;
  37. cnt[u] = ;
  38. for(int i = head[u]; i != -; i = edge[i].next){
  39. int v = edge[i].to;
  40. int w = edge[i].w;
  41. if(v == fa)continue;
  42. dfs(v, u);
  43. if(dp[u]+dp[v]+w > ans){
  44. ans = dp[u]+dp[v]+w;
  45. num = cnt[u]*cnt[v];
  46. }else if(dp[u]+dp[v]+w == ans)
  47. num += cnt[u]*cnt[v];
  48. if(dp[u] < dp[v]+w){
  49. dp[u] = dp[v]+w;
  50. cnt[u] = cnt[v];
  51. }else if(dp[u] == dp[v]+w)
  52. cnt[u] += cnt[v];
  53. }
  54. }
  55.  
  56. int main()
  57. {
  58. //freopen("input.txt", "r", stdin);
  59. int n;
  60. while(scanf("%d", &n)!=EOF){
  61. int u, v, w;
  62. init();
  63. for(int i = ; i < n-; i++){
  64. scanf("%d%d%d", &u, &v, &w);
  65. add_edge(u, v, w);
  66. add_edge(v, u, w);
  67.  
  68. }
  69. ans = -INF;
  70. num = ;
  71. dfs(, );
  72. printf("%d %d\n", ans, num);
  73. }
  74.  
  75. return ;
  76.  
  77. }

HDU3534(SummerTrainingDay13-C tree dp)的更多相关文章

  1. 96. Unique Binary Search Trees (Tree; DP)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. TYOI Day1 travel:Tree dp【处理重复走边】

    题意: 给你一棵树,n个节点,每条边有长度. 然后有q组询问(u,k),每次问你:从节点u出发,走到某个节点的距离mod k的最大值. 题解: 对于无根树上的dp,一般都是先转成以1为根的有根树,然后 ...

  4. HDU 4359 Easy Tree DP?

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. Codeforces 442D Adam and Tree dp (看题解)

    Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...

  6. HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. Partial Tree(DP)

    Partial Tree http://acm.hdu.edu.cn/showproblem.php?pid=5534 Time Limit: / MS (Java/Others) Memory Li ...

  8. DP Intro - Tree DP Examples

    因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...

  9. HDU 5534/ 2015长春区域H.Partial Tree DP

    Partial Tree Problem Description In mathematics, and more specifically in graph theory, a tree is an ...

随机推荐

  1. Linux系统日志分析与管理(14)

    当你的 Linux 系统出现不明原因的问题时,你需要查阅一下系统日志才能够知道系统出了什么问题了,所以说了解系统日志是很重要的事情,系统日志可以记录系统在什么时间.哪个主机.哪个服务.出现了什么信息等 ...

  2. Linux Shell脚本编程基础(11)

    实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核,不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序.Shel编程语言具有普通编程 ...

  3. Linux系统VIM编辑器管理(2)

    VI/VIM模式概述 在 Linux 的世界中,绝大部分的配置文件都是以 ASCII 的纯文本形态存在,因此利用简单的文字编辑软件就能够修改设定了,与微软的 Windows 系统不同的是,如果你用惯了 ...

  4. react中组件的渲染

    1.封装props对象 2.调用组件函数,得到返回的react元素 3.ReactDom把React元素转成真实的DOM元素并且插入到目标容器内部

  5. Maven - Maven速成

    Maven Maven是一个项目构建和管理工具,有助于开发者快速完成项目的配置,快速建立开发环境,从而提高开发效率. 管理项目构建(build)的生命周期(清理.编译.测试.打包.发布.部署.报告等) ...

  6. Data - Hadoop单机配置 - 使用Hadoop2.8.0和Ubuntu16.04

    系统版本 anliven@Ubuntu1604:~$ uname -a Linux Ubuntu1604 4.8.0-36-generic #36~16.04.1-Ubuntu SMP Sun Feb ...

  7. odoo配置界面设置字段默认值

    转自国外牛人博客:http://ludwiktrammer.github.io/odoo/custom-settings-odoo.html Defining custom settings in O ...

  8. 字符、字符串和文本的处理之String类型

    .Net Framework中处理字符和字符串的主要有以下这么几个类: (1).System.Char类 一基础字符串处理类 (2).System.String类 一处理不可变的字符串(一经创建,字符 ...

  9. charles撰写工具/compose和Compose New

    撰写工具/compose和Compose New compose 是在原有的请求基础上,修改: compose New 是新出一个弹窗,自己手动一个个的去写: 可以写各种状态:– URL:– Meth ...

  10. Android中Serializable和Parcelable序列化对象详解

    学习内容: 1.序列化的目的 2.Android中序列化的两种方式 3.Parcelable与Serializable的性能比较 4.Android中如何使用Parcelable进行序列化操作 5.P ...