[poj 3342]Party at Hali-Bula 树形dp

题目链接http://poj.org/problem?id=3342
Party at Hali-Bula

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5997 Accepted: 2140

Description

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 (1 ≤ n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n-1 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

Source

Tehran 2006

题意:BB将要邀请BCM的员工参加一个party来庆祝他的退休。还是每个人不能同其直接boss一起参加。要求使参加人数最多,并要求判断方案是否唯一。

思路
计算最大的很简单dp[i][0]=求和max(dp[soni][0],dp[soni][1]); dp[i][1]=求和dp[soni][0];

判断开始只傻逼的判dp[1][0]=?dp[1][1];
应用flag[i][0]判断,如果选的子树方案不唯一,该方案不唯一;初始当dp[soni][0]==dp[soni][1]时flag【i】[0]=1;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
using namespace std;
int n;
int tot;
vector<int> lin[205];
map<string,int> q;
string s,s1;
int dp[201][2];
int flag[201][2];
void dfs(int x)
{
    int ret=0;
    int tmp=0;
    int f1=0;
    int f2=0;
    for(int i=0;i<lin[x].size();i++)
    {
        int v=lin[x][i];
        dfs(v); 
        ret+=max(dp[v][0],dp[v][1]);

        if(dp[v][0]==dp[v][1]) f1=1;
        else if(dp[v][0]>dp[v][1]&&flag[v][0]==1) f1=1;
        else if(dp[v][1]>dp[v][0]&&flag[v][1]==1) f1=1;

        tmp+=dp[v][0];
        if(flag[v][0]) f2=1;
    }


    dp[x][0]=ret;
    dp[x][1]=tmp+1;
    if(f2) flag[x][1]=1; 
    if(f1) flag[x][0]=1;
}
int main()
{
    while(scanf("%d",&n))
    {
        if(!n) return 0;
        tot=0;
        cin>>s;
        q.clear();
        q[s]=++tot;
        for(int i=1;i<=n;i++)
        {
            lin[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            cin>>s>>s1;
            if(!q[s])
            {
                q[s]=++tot;
            }
            if(!q[s1])
            {
                q[s1]=++tot;
            }
            lin[q[s1]].push_back(q[s]);
        }
        memset(flag,0,sizeof(flag));
        memset(dp,0,sizeof(dp));
        dfs(1);


        if(dp[1][0]==dp[1][1])
        {
            printf("%d No\n",dp[1][0]);
        }
        else if(dp[1][0]>dp[1][1]&&flag[1][0])
        {
            printf("%d No\n",dp[1][0]);
        }
        else if(dp[1][1]>dp[1][0]&&flag[1][1])
        {
            printf("%d No\n",dp[1][1]);
        }
        else printf("%d Yes\n",max(dp[1][0],dp[1][1]));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章