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
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    python字典练习2
    python字典小例子1
    尚硅谷linux学习笔记(待更新)3
    python字典 更新
    尚硅谷linux学习笔记(待更新)
    thymeleaf 无法正常解析页面(本地可以正常访问某个请求,并可以返回页面,但服务端访问出现找不到模板)
    thymeleaf关于Map的取值
    python练习——列表练习1
    如何在thymeleaf和freemarker的url中绑定传递的参数
    使用js获取项目url地址
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5817896.html
Copyright © 2011-2022 走看看