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 ;
    }
  • 相关阅读:
    java第19次
    Unity3D常用的生命周期函数
    Unity3D结合ZXing生成中间带图标的二维码并保存
    Unity查看当前内存使用情况(针对移动端开发)
    Unity 获取鼠标悬停位置下的UI或3D物体对象
    【Unity3D】获取鼠标在三维空间(世界坐标系)的位置
    【Unity3D—C#】按下任意按键,返回按键的名称 以及 KeyCode键码详解
    Unity UGUI获取鼠标在屏幕的准确点击位置
    ML-Agent:通过TF#使用训练完成的模型数据
    对《ml-agent:Win10下环境安装》更新
  • 原文地址:https://www.cnblogs.com/nonames/p/11733482.html
Copyright © 2011-2022 走看看