zoukankan      html  css  js  c++  java
  • HDU 3534

    Tree

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

    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
     
    通过这几天的看题目,做题目,发现树形DP很有规律,几乎知道思路,就可以写了
     
    题意:
      给你一棵树,求最长路径,并且求出有多少条这样的路径
    分析:
      任意两点只有一条路径最短路径,ok,所以可以用数组来标记当前状态下最长路径是多少,有多少条
     
    AC代码:
     1 # include <bits/stdc++.h>
     2 using namespace std;
     3 const int MAX = 50001;
     4 struct Node
     5 {
     6     int to;
     7     int next;
     8     int len;
     9 }tree[MAX * 2];
    10 int head[MAX], Len[MAX], node[MAX];
    11 int tol = 0;
    12 int Max = 0, jishu = 0;
    13 
    14 void add(int a, int b, int len)
    15 {
    16     tree[tol].to = b;
    17     tree[tol].next = head[a];
    18     tree[tol].len = len;
    19     head[a] = tol++;
    20 }
    21 void dfs(int root, int f)
    22 {
    23     Len[root] = 0;
    24     node[root] = 1;
    25     for(int i = head[root]; i != -1; i = tree[i].next)
    26     {
    27         int son = tree[i].to;
    28         if(son == f)
    29             continue;
    30         dfs(son, root);
    31         
    32         int tep = Len[son] + tree[i].len;  
    33         if(tep + Len[root] > Max)//最长边经过son
    34         {  
    35             jishu = node[son] * node[root];  
    36             Max = tep + Len[root];  
    37         }  
    38         else if(tep + Len[root] == Max)  
    39             jishu += node[son] * node[root];
    40 
    41         if(Len[root] < tep)  
    42         {
    43             Len[root] = tep;  
    44             node[root] = node[son];  
    45         }  
    46         else if(Len[root] == tep)  
    47             node[root] += node[son];      
    48     }    
    49 }
    50 int main()
    51 {
    52     int n;
    53     while(scanf("%d", &n) != EOF)
    54     {
    55         tol = 0;
    56         memset(head, -1, sizeof(head));
    57         
    58         int a, b, len;
    59         for(int i = 1; i < n; i++)
    60         {
    61             scanf("%d%d%d", &a, &b, &len);
    62             add(a, b, len);
    63             add(b, a, len);
    64         }
    65         Max = 0, jishu = 0;
    66         dfs(1, -1);
    67         printf("%d %d
    ", Max, jishu);
    68     }
    69     return 0;
    70 }
    View Code
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    maven 的依赖包的版本更改之后,项目启动报错,相关联的资源没有在tomcat里面加载
    Maven更新后本地仓库jar后缀带有 lastUpdated
    spring+mybatis之声明式事务管理初识(小实例)
    java 详解类加载器的双亲委派及打破双亲委派
    jvm内存溢出区域和排查方法
    JVM中的新生代和老年代(Eden空间、两个Survior空间)
    什么情况下会发生堆内存溢出,栈内存溢出,结合实例说明
    投而死面试
    检查性异常
    多线程系列课程
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5817896.html
Copyright © 2011-2022 走看看