• HDU 4714 Tree2cycle (树形DP)


    Tree2cycle

    Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
    Total Submission(s): 324    Accepted Submission(s): 54


    Problem Description
    A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of cost respectively. The nodes are labeled from 1 to N. Your job is to transform the tree to a cycle(without superfluous edges) using minimal cost.

    A cycle of n nodes is defined as follows: (1)a graph with n nodes and n edges (2)the degree of every node is 2 (3) each node can reach every other node with these N edges.
     
    Input
    The first line contains the number of test cases T( T<=10 ). Following lines are the scenarios of each test case.
    In the first line of each test case, there is a single integer N( 3<=N<=1000000 ) - the number of nodes in the tree. The following N-1 lines describe the N-1 edges of the tree. Each line has a pair of integer U, V ( 1<=U,V<=N ), describing a bidirectional edge (U, V).
     
    Output
    For each test case, please output one integer representing minimal cost to transform the tree to a cycle.
     
    Sample Input
    1 4 1 2 2 3 2 4
     
    Sample Output
    3
    Hint
    In the sample above, you can disconnect (2,4) and then connect (1, 4) and (3, 4), and the total cost is 3.
     
    Source
     
    Recommend
    liuyiding
     

    简单树形DP来一发

    用dp1表示形成一颗链,而且该点在端点。

    dp2表示形成一颗链需要的最少步数

     1 /* *******************************************
     2 Author       : kuangbin
     3 Created Time : 2013年09月08日 星期日 12时00分01秒
     4 File Name    : 1009.cpp
     5 ******************************************* */
     6 #pragma comment(linker, "/STACK:1024000000,1024000000")
     7 #include <stdio.h>
     8 #include <algorithm>
     9 #include <iostream>
    10 #include <string.h>
    11 #include <vector>
    12 #include <queue>
    13 #include <set>
    14 #include <map>
    15 #include <string>
    16 #include <math.h>
    17 #include <stdlib.h>
    18 #include <time.h>
    19 using namespace std;
    20 
    21 const int MAXN = 1000010;
    22 vector<int>vec[MAXN];
    23 int dp1[MAXN];
    24 int dp2[MAXN];
    25 void dfs(int u,int pre)
    26 {
    27     int sz = vec[u].size();
    28     int sum1 = 0;
    29     int maxn = 0, maxid = -1;
    30     int smaxn = 0, smaxid = -1;
    31     for(int i = 0;i < sz;i++)
    32     {
    33         int v = vec[u][i];
    34         if(v == pre)continue;
    35         dfs(v,u);
    36         sum1 += dp2[v]+2;
    37         int tmp = dp1[v] - (dp2[v] + 2);
    38         tmp = -tmp;
    39         if(tmp > smaxn)
    40         {
    41             smaxn = tmp;
    42             smaxid = v;
    43             if(smaxn > maxn)
    44             {
    45                 swap(smaxn,maxn);
    46                 swap(smaxid,maxid);
    47             }
    48         }
    49     }
    50     dp1[u] = sum1 - maxn;
    51     dp2[u] = sum1 - maxn - smaxn;
    52 
    53 }
    54 int main()
    55 {
    56     //freopen("in.txt","r",stdin);
    57     //freopen("out.txt","w",stdout);
    58     int T;
    59     int n;
    60     scanf("%d",&T);
    61     while(T--)
    62     {
    63         scanf("%d",&n);
    64         int u,v;
    65         for(int i = 1;i <= n;i++)
    66             vec[i].clear();
    67         for(int i = 1;i < n;i++)
    68         {
    69             scanf("%d%d",&u,&v);
    70             vec[u].push_back(v);
    71             vec[v].push_back(u);
    72         }
    73         dfs(1,-1);
    74         cout<<dp2[1]+1<<endl;
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    1289 大鱼吃小鱼
    install ios开发环境
    Xcode_5
    嵌入式学习_AD学习篇
    课务IOS概述_1
    动态规划入门(2):01背包问题
    Python记之薄暮笔记
    线段树进阶:权值线段树
    动态规划入门(1):最长递增子序列
    python记之Hello world!
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3308694.html
Copyright © 2020-2023  润新知