zoukankan      html  css  js  c++  java
  • hdu 2412(树形dp)

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=2412

    思路:这篇文章讲的很清楚:http://wenku.baidu.com/view/84164e1a227916888486d7d6.html 。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<map>
     6 #include<string>
     7 #include<vector>
     8 using namespace std;
     9 
    10 int dp[222][2];
    11 int flag[222][2];
    12 int n;
    13 vector<vector<int> >G;
    14 
    15 void dfs(int u)
    16 {
    17     dp[u][0]=0;
    18     dp[u][1]=1;
    19     flag[u][0]=1;
    20     flag[u][1]=1;
    21     for(int i=0;i<G[u].size();i++){
    22         int v=G[u][i];
    23         dfs(v);
    24         dp[u][0]+=max(dp[v][0],dp[v][1]);
    25         dp[u][1]+=dp[v][0];
    26         if(dp[v][0]>dp[v][1]&&flag[v][0]==0)flag[u][0]=0;
    27         else if(dp[v][1]>dp[v][0]&&flag[v][1]==0)flag[u][0]=0;
    28         else if(dp[v][0]==dp[v][1])flag[u][0]=0;
    29         if(flag[v][0]==0)flag[u][1]=0;
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     char str[222],str1[222],str2[222];
    36     while(~scanf("%d",&n)&&n){
    37         G.clear();
    38         G.resize(222);
    39         map<string,int>name;
    40         int cnt=0;
    41         scanf("%s",str);
    42         name[str]=++cnt;
    43         for(int i=1;i<n;i++){
    44             scanf("%s%s",str1,str2);
    45             if(name.find(str1)==name.end())name[str1]=++cnt;
    46             if(name.find(str2)==name.end())name[str2]=++cnt;
    47             G[name[str2]].push_back(name[str1]);
    48         }
    49         memset(dp,0,sizeof(dp));
    50         memset(flag,0,sizeof(flag));
    51         dfs(1);
    52         if(dp[1][0]>dp[1][1]&&flag[1][0]==1){
    53             printf("%d Yes
    ",dp[1][0]);
    54         }else if(dp[1][1]>dp[1][0]&&flag[1][1]==1){
    55             printf("%d Yes
    ",dp[1][1]);
    56         }else {
    57             printf("%d No
    ",max(dp[1][0],dp[1][1]));
    58         }
    59     }
    60     return 0;
    61 }
    62 
    63 
    64 
    65         
    66             
    View Code
  • 相关阅读:
    Nginx proxy开启cache缓存
    Nginx Server 配置
    nginx 全局配置
    Nginx 配置文件解析
    Python-Scrapy框架
    Python-shutil模块
    os模块3
    os模块
    python os模块atime ,ctime,mtime意义
    Python-正则表达式
  • 原文地址:https://www.cnblogs.com/wally/p/3308144.html
Copyright © 2011-2022 走看看