zoukankan      html  css  js  c++  java
  • poj3342 Party at Hali-Bula

    树形dp题,状态转移方程应该很好推,但一定要细心。

    http://poj.org/problem?id=3342

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <string>
     5 #include <map>
     6 using namespace std;
     7 const int maxn = 200 + 10;
     8 char buf[maxn];
     9 struct Edge{
    10     int to, next;
    11 }edge[maxn << 1];
    12 int head[maxn], N;
    13 map<string, int> mapi;
    14 int n, k;
    15 int dp[maxn][2];
    16 int o[maxn][2];
    17 int get_str(char *dest){
    18     int cnt = 0;
    19     char ch;
    20     do{
    21         ch = getchar();
    22     }while(ch != EOF && (ch == ' ' || ch == '
    '));
    23     if(ch == EOF) return 0;
    24     dest[cnt++] = ch;
    25     while((ch = getchar()) != ' ' && ch != '
    ' && ch != EOF) dest[cnt++] = ch;
    26     dest[cnt] = '';
    27     return cnt;
    28 }
    29 
    30 
    31 void addEdge(int u, int v){
    32     edge[N].next = head[u];
    33     edge[N].to = v;
    34     head[u] = N++;
    35 }
    36 
    37 void dfs(int u, int fa){
    38     dp[u][1] = 1;
    39     int tem1 = 0, tem2 = 0;
    40     for(int i = head[u]; i + 1; i = edge[i].next){
    41         int v = edge[i].to;
    42         if(v == fa) continue;
    43         dfs(v, u);
    44         dp[u][1] += dp[v][0];
    45         o[u][1] |= o[v][0];
    46         int d = dp[v][1] > dp[v][0] ? 1 : (dp[v][1] == dp[v][0] ? -1 : 0);
    47         if(d == -1){
    48             dp[u][0] += dp[v][0];
    49             o[u][0] = 1;
    50         }else{
    51             dp[u][0] += dp[v][d];
    52             o[u][0] |= o[v][d];
    53         }
    54     }
    55 }
    56 
    57 int main(){
    58     //freopen("in.txt", "r", stdin);
    59     while(~scanf("%d", &n) && n){
    60         k = 0;
    61         int base = 0;
    62         get_str(buf);
    63         mapi.clear();
    64         mapi[string(buf)] = ++base;
    65         N = 0;
    66         memset(head, -1, sizeof head);
    67         for(int i = 1, x, y; i < n; i++){
    68             get_str(buf);
    69             if(mapi.find(string(buf)) == mapi.end()) mapi[string(buf)] = ++base, x = base;
    70             else x = mapi[string(buf)];
    71             get_str(buf);
    72             if(mapi.find(string(buf)) == mapi.end()) mapi[string(buf)] = ++base, y = base;
    73             else y = mapi[string(buf)];
    74             addEdge(x, y);
    75             addEdge(y, x);
    76         }
    77         memset(dp, 0, sizeof dp);
    78         memset(o, 0, sizeof o);
    79         dfs(1, 0);
    80         int ans = -1;
    81         bool ok = 0;
    82         for(int i = 1; i <= n; i++) for(int j = 0; j <= 1; j++){
    83             if(dp[i][j] > ans) ans = dp[i][j], ok = o[i][j];
    84             else if(dp[i][j] == ans) ok = 1;
    85         }
    86         printf("%d %s
    ", ans, ok ? "No" : "Yes");
    87     }
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    树状数组与线段树基础
    阿里云服务器低价购买及域名解析
    ENVI5.3安装教程(含软件下载)
    05 ArcGIS JS API 4.12版本监听view-change事件
    jQuery的AJAX请求成功,但是跳转到error的解决方法
    04 ArcGIS JS API 4.12加载天地图
    02 Portal for ArcGIS 10.7安装部署教程(linux环境)
    10 webpack4.0学习笔记——热替换_devServer
    09 webpack4.0学习笔记——配置文件_HtmlWebpackPlugin使用
    08 webpack4.0学习笔记——配置文件_DefinePlugin使用
  • 原文地址:https://www.cnblogs.com/astoninfer/p/4873360.html
Copyright © 2011-2022 走看看