zoukankan      html  css  js  c++  java
  • [BZOJ2060][Usaco2010 Nov]Visiting Cows 拜访奶牛

    2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛

    Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 474  Solved: 346 [Submit][Status][Discuss]

    Description

    经过了几周的辛苦工作,贝茜终于迎来了一个假期.作为奶牛群中最会社交的牛,她希望去拜访N(1<=N<=50000)个朋友.这些朋友被标号为1..N.这些奶牛有一个不同寻常的交通系统,里面有N-1条路,每条路连接了一对编号为C1和C2的奶牛(1 <= C1 <= N; 1 <= C2 <= N; C1<>C2).这样,在每一对奶牛之间都有一条唯一的通路. FJ希望贝茜尽快的回到农场.于是,他就指示贝茜,如果对于一条路直接相连的两个奶牛,贝茜只能拜访其中的一个.当然,贝茜希望她的假期越长越好,所以她想知道她可以拜访的奶牛的最大数目.

    Input

    第1行:单独的一个整数N 第2..N行:每一行两个整数,代表了一条路的C1和C2.

    Output

    单独的一个整数,代表了贝茜可以拜访的奶牛的最大数目.

    Sample Input

    7
    6 2
    3 4
    2 3
    1 2
    7 6
    5 6


    INPUT DETAILS:

    Bessie knows 7 cows. Cows 6 and 2 are directly connected by a road,
    as are cows 3 and 4, cows 2 and 3, etc. The illustration below depicts the
    roads that connect the cows:

                           1--2--3--4
                              |
                           5--6--7


    Sample Output

    4

    OUTPUT DETAILS:

    Bessie can visit four cows. The best combinations include two cows
    on the top row and two on the bottom. She can't visit cow 6 since
    that would preclude visiting cows 5 and 7; thus she visits 5 and
    7. She can also visit two cows on the top row: {1,3}, {1,4}, or
    {2,4}.
     
    设$dp[i][0]$表示$i$不选时在以$i$为根的子树中最多能选多少
    $dp[i][1]$表示$i$要选时在以$i$为根的子树中最多能选多少
    转移显然
    #include <cstdio>
    #include <cstdlib> 
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char buf[10000000], *ptr = buf - 1;
    inline int readint(){
        int f = 1, n = 0;
        char ch = *++ptr;
        while(ch < '0' || ch > '9'){
            if(ch == '-') f = -1;
            ch = *++ptr;
        }
        while(ch <= '9' && ch >= '0'){
            n = (n << 1) + (n << 3) + ch - '0';
            ch = *++ptr;
        }
        return f * n;
    }
    const int maxn = 50000 + 10;
    struct Edge{
        int to, next;
        Edge(){}
        Edge(int _t, int _n): to(_t), next(_n){}
    }e[maxn * 2];
    int fir[maxn] = {0}, cnt = 0;
    inline void ins(int u, int v){
        e[++cnt] = Edge(v, fir[u]); fir[u] = cnt;
        e[++cnt] = Edge(u, fir[v]); fir[v] = cnt;
    }
    int dp[maxn][2];
    void dfs(int u, int fa){
        dp[u][0] = 0;
        dp[u][1] = 1;
        for(int v, i = fir[u]; i; i = e[i].next){
            v = e[i].to;
            if(v == fa) continue;
            dfs(v, u);
            dp[u][0] += max(dp[v][0], dp[v][1]);
            dp[u][1] += dp[v][0];
        }
    }
    int main(){
        fread(buf, sizeof(char), sizeof(buf), stdin);
        int N = readint();
        for(int u, v, i = 1; i < N; i++){
            u = readint();
            v = readint();
            ins(u, v);
        }
        srand(19260817);
        int root = rand() * rand() % N + 1;
        dfs(root, 0);
        printf("%d
    ", max(dp[root][0], dp[root][1]));
        return 0;
    }
  • 相关阅读:
    【js】js中的||和&&
    【jQuery】form表单元素序列化为json对象
    【jQuery】serializeArray()与serialize()的区别
    【jQuery】jquery-ui autocomplete智能提示
    【js】批量判断表单中的文本框非空
    【php】基础学习5
    【php】基础学习4
    【php】基础学习3
    SpringMVC与Struts2区别与比较总结
    iOS 改变UILabel部分颜色
  • 原文地址:https://www.cnblogs.com/ruoruoruo/p/7594085.html
Copyright © 2011-2022 走看看