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. Spring Boot中使用Swagger2构建RESTful API文档

    在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...

  2. Shell - 简明Shell入门04 - 判断语句(If)

    示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一个参数赋值给变量var if test $var # test - check file types and compare va ...

  3. linq查询时查询语句中附带多个查询时“已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭”

    主要原因是因为EF采用的 DataReader来进行数据的存储,此时connection使用的是同一个. 例如: list = _tzNewsService.GetAll().Where(w => ...

  4. Ubuntu 14.04TLS和CentOS-6(64bit)上安装Nginx

    Ubuntu 14.04上安装Nginx [参考地址]https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on- ...

  5. 安装Centos7 随手记

    1.老笔记本安装Centos7 配置:酷睿I3  内存8G 2.原有系统Win7 将原来的硬盘空间,调整出60G 给Centos7 用. 3.安装Centos7 图形介面的,和windows安装过程类 ...

  6. PyCharm下载与激活

    1.集成开发环境(IDE:Integrated Development Environment)PyCharm下载地址:https://www.jetbrains.com/pycharm/downlo ...

  7. Storm的acker确认机制

    Storm的acker消息确认机制... ack/fail消息确认机制(确保一个tuple被完全处理) 在spout中发射tuple的时候需要同时发送messageid,这样才相当于开启了消息确认机制 ...

  8. php -- 获取函数参数

    ----- 015-parameter.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&q ...

  9. 《松本行弘的程序世界》读书笔记(上)——面向对象、程序块、设计模式、ajax

    1. 前言 半个月之前买了这本书,还是经园子里的一位网友推荐的.到现在看了一半多,基础的都看完了,剩下的几章可做高级部分来看.这本书看到现在,可以说感触很深,必须做一次读书笔记! 关于这本书,不了解的 ...

  10. Vue笔记:在项目中使用 SCSS

    背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...