zoukankan      html  css  js  c++  java
  • 树形dp

    https://ac.nowcoder.com/acm/contest/1126/A

    链接:https://ac.nowcoder.com/acm/contest/1126/A
    来源:牛客网

    题目描述

    After many weeks of hard work, Bessie is finally getting a vacation! After many weeks of hard work, Bessie is finally getting a vacation! numbered 1..N. The cows have set up quite an unusual road network with exactly N-1 roads connecting pairs of cows C1 and C2 (1 <= C1 <= N; 1 <= C2 <= N; C1 != C2) in such a way that there exists a unique path of roads between any two cows.
    FJ wants Bessie to come back to the farm soon; thus, he has instructed Bessie that if two cows are directly connected by a road, she may not visit them both. Of course, Bessie would like her vacation to be as long as possible, so she would like to determine the maximum number of cows she can visit.

    输入描述:

    * Line 1: A single integer: N
    * Lines 2..N: Each line describes a single road with two space-separated integers: C1 and C2

    输出描述:

    * Line 1: A single integer representing the maximum number of cows that Bessie can visit.
    示例1

    输入

    复制
    7 
    6 2 
    3 4 
    2 3 
    1 2 
    7 6 
    5 6 
    

    输出

    复制
    4

    说明

    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.
    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}.

    https://www.cnblogs.com/seaupnice/p/9471700.html
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 998244353
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    const int Maxn = 50000+5;
    
    vector<int> G[Maxn];
    int vi[Maxn] , dp[Maxn][2];
    
    int dfs(int u)
    {
        dp[u][1] = 1 ;
        for(int i = 0 ; i < G[u].size() ; i++)
        {
            int v = G[u][i];
            if(!vi[v])
            {
                vi[v] = 1 ;
                dfs(v);
                dp[u][0] += max(dp[v][0] , dp[v][1]);
                dp[u][1] += dp[v][0];
            }
        }
    }
    
    int main()
    {
        int n ;
        scanf("%d" , &n);
        for(int i = 1 ; i < n ; i++)
        {
            int x , y ;
            scanf("%d%d" , &x , &y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        memset(vi , 0 , sizeof(vi));
        memset(dp , 0 , sizeof(dp));
        vi[1] = 1 ;
        dfs(1);
        cout << max(dp[1][0] , dp[1][1]) << endl ;
    
        return 0 ;
    }
  • 相关阅读:
    Web服务器讲解与JavaWeb应用部署(本机,以Tomcat为例)
    DNS与DNS劫持原理、IP、域名、服务器访问浅讲
    MyArrayList——自己实现ArrayList
    Map排序——按key排序,按value排序
    储存对象并按对象某属性排序的几种方法
    常用集合类使用方法
    避免窗口切换闪烁——卡片式布局的使用方法
    [Netbeans]为面板设置背景图片
    JDBC编程之预编译SQL与防注入式攻击以及PreparedStatement的使用教程
    JDBC编程之事务的使用教程
  • 原文地址:https://www.cnblogs.com/nonames/p/11733482.html
Copyright © 2011-2022 走看看