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
    生命不息,奋斗不止,这才叫青春,青春就是拥有热情相信未来。
  • 相关阅读:
    Ubuntu 16.04实现SSH无密码登录/免密登录/自动登录(ssh-keygen/ssh-copy-id)
    简单理解Linux的Loopback接口
    iptables为什么需要增加loopback回环的规则
    [ASP.NET Core 3框架揭秘] 依赖注入[10]:与第三方依赖注入框架的适配
    [ASP.NET Core 3框架揭秘] 依赖注入[9]:实现概述
    [ASP.NET Core 3框架揭秘] 依赖注入[8]:服务实例的生命周期
    [ASP.NET Core 3框架揭秘] 依赖注入[7]:服务消费
    [ASP.NET Core 3框架揭秘] 依赖注入[6]:服务注册
    [ASP.NET Core 3框架揭秘] 依赖注入[5]: 利用容器提供服务
    AOP框架Dora.Interception 3.0 [5]: 基于策略的拦截器注册方式
  • 原文地址:https://www.cnblogs.com/lyf-acm/p/5817896.html
Copyright © 2011-2022 走看看