zoukankan      html  css  js  c++  java
  • uva 1220

    1220 - Party at Hali-Bula

    Time limit: 3.000 seconds

    Dear Contestant,
    I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I decide not to invite both an employee and his/her boss. The organizational hierarchy at BCM is such that nobody has more than one boss, and there is one and only one employee with no boss at all (the Big Boss)! Can I ask you to please write a program to determine the maximum number of guests so that no employee is invited when his/her boss is invited too? I've attached the list of employees and the organizational hierarchy of BCM.
    Best, 
    -Brian Bennett
    P.S. I would be very grateful if your program can indicate whether the list of people is uniquely determined if I choose to invite the maximum number of guests with that condition.

    Input 

    The input consists of multiple test cases. Each test case is started with a line containing an integer n <tex2html_verbatim_mark>(1$ le$n$ le$200) <tex2html_verbatim_mark>, the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n - 1 <tex2html_verbatim_mark>lines contains the name of an employee together with the name of his/her boss. All names are strings of at least one and at most 100 letters and are separated by blanks. The last line of each test case contains a single 0.

    Output 

    For each test case, write a single line containing a number indicating the maximum number of guests that can be invited according to the required condition, and a word Yes or No, depending on whether the list of guests is unique in that case.

    Sample Input 

    6
    Jason
    Jack Jason
    Joe Jack
    Jill Jason
    John Jack
    Jim Jill
    2
    Ming
    Cho Ming
    0
    

    Sample Output 

     4 Yes

    1 No


    其实这道题之前做过一种类似的,就是不判断人数最多时方案是否唯一,当时是队友贪的,并不知道原来这是树形dp啊。

    每个节点选择或不选择直接递归求就可以。

    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <vector>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define ll long long
    #define _cle(m, a) memset(m, a, sizeof(m))
    #define repu(i, a, b) for(int i = a; i < b; i++)
    #define MAXN 205
    vector<int> v[MAXN];
    int n;
    string s1, s2;
    map<string, int> ma;
    int tot;
    struct Num{
      int d, f;
      Num() : d(1), f(1) {}
    }num;
    int get_id(string s)
    {
        if(ma[s]) return ma[s];
        else return ma[s] = ++tot;
    }
    
    Num dp(int u, int flag)
    {
        Num ans;
        if(flag) ans.d = 1;
        else ans.d = 0;
        if(v[u].empty()) return ans;
        int k = v[u].size();
        int d = 0, f = 1;
        if(flag) {
            repu(i, 0, k) {
               Num t = dp(v[u][i], 0);
               d += t.d;
               if(t.f == 0) f = 0;
            }
            ans.d = d + 1;
            ans.f = f;
        }
        else {
            repu(i, 0, k) {
            Num t1, t2;
            t1 = dp(v[u][i], 0);
            t2 = dp(v[u][i], 1);
            if(t1.d == t2.d) f = 0, d += t1.d;
            else if(t1.d > t2.d) {
                if(t1.f == 0) f = 0;
                d += t1.d;
            }
            else {
                if(t2.f == 0) f = 0;
                d += t2.d;
            }
           }
           ans.d = d;
           ans.f = f;
        }
        return ans;
    }
    
    int main()
    {
        while(~scanf("%d", &n) && n)
        {
            ma.clear();
            tot = 0;
            repu(i, 0, n + 2) v[i].clear();
            cin>>s1;
            get_id(s1);
            repu(i, 1, n) {
               cin>>s1>>s2;
               v[get_id(s2)].push_back(get_id(s1));
            }
            Num t1 = dp(1, 1), t2 = dp(1, 0);
            if(t1.d == t2.d) printf("%d No
    ", t1.d);
            else if(t1.d > t2.d) {
                printf("%d ", t1.d);
                if(t1.f) printf("Yes
    ");
                else printf("No
    ");
            }
            else {
                 printf("%d ", t2.d);
                 if(t2.f) printf("Yes
    ");
                 else printf("No
    ");
            }
        }
        return 0;
    }
    View Code
    
    
    
     
  • 相关阅读:
    vscode maven
    clojure + sumblime text SublimeREPL
    .zsh_history
    springboot-自动装配
    任务调度-Quartz
    springcloud alibaba
    canal与kafka的结合使用
    centos7安装Zookeeper
    centos7安装kafka
    vmware+centos7 设置静态ip
  • 原文地址:https://www.cnblogs.com/sunus/p/4575478.html
Copyright © 2011-2022 走看看