• HDU 4714 Tree2cycle (树形DP)


    题意:给定一棵树,断开一条边或者接上一条边都要花费 1,问你花费最少把这棵树就成一个环。

    析:树形DP,想一想,要想把一棵树变成一个环,那么就要把一些枝枝叶叶都换掉,对于一个分叉是大于等于2的我们一定要把它从父结点上剪下来是最优的,

    因为如果这样剪下来再粘上花费是2(先不管另一端),如果分别剪下来再拼起来,肯定是多花了,因为多了拼起来这一步,知道这,就好做了。先到叶子结点,

    然后再回来计算到底要花多少。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e6 + 5;
    const LL mod = 1e9 + 7;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    struct Edge{
        int next, to;
    };
    Edge edge[maxn<<1];
    int cnt, ans;
    int head[maxn];
    
    void add(int u, int v){
        edge[cnt].to =v;
        edge[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    int dfs(int u, int fa){
        int tmp = 0;
        for(int i = head[u]; ~i; i = edge[i].next){
            int v = edge[i].to;
            if(v == fa)  continue;
            tmp += dfs(v, u);
        }
    
        if(tmp >= 2){
            if(1 == u)  ans += 2 * (tmp - 2);
            else ans += 2 * (tmp - 1);
            return 0;
        }
        return 1;
    }
    
    int main(){
        int T;  cin >> T;
        while(T--){
            scanf("%d", &n);
            int u, v;
            cnt = 0;
            memset(head, -1, sizeof head);
            for(int i = 1; i < n; ++i){
                scanf("%d %d", &u, &v);
                add(u, v);
                add(v, u);
            }
            ans = 1;
            dfs(1, -1);
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    11.关于django的content_type表
    6.re正则表达式
    1.关于python 的hmac加密
    4.瀑布流js
    10.django的一些方法理解
    7.一些比较有用的网站
    准备辞职了,走之前想解决的问题ptr 为空
    Oracle 代码生成小工具免费下载
    Js 中一个判断类型的方法
    jQuery 加上最后自己的验证
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5934983.html
Copyright © 2020-2023  润新知