关闭

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

标签: pojdp
2675人阅读 评论(0) 收藏 举报
分类:

题目链接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]));
    }
}
1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)

1、http://poj.org/problem?id=3342 2、题目大意: 现在要邀请n个人中的一些人参加晚宴,要求是有直接上下级关系的人不能同时出席,问最多可以邀请多少人参加,并判断在保证最大...
  • sdjzping
  • sdjzping
  • 2013-10-26 20:51
  • 1150

poj1155 TELE(树形dp+背包)

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4344   Accepted:...
  • d_x_d
  • d_x_d
  • 2015-11-16 20:06
  • 1511

树形dp入门之poj 2342

题目:poj2342Anniversary party 题意:话说一个公司的一些然要去参加一个party,每个人有一个愉悦值,而如果某个人的直接上司在场的话会非常扫兴,所以避免这样的安排,问...
  • y990041769
  • y990041769
  • 2014-07-23 12:57
  • 3806

POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)

POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
  • Tc_To_Top
  • Tc_To_Top
  • 2015-07-22 11:22
  • 678

HDU2412 & POJ3342 Party at Hali-Bula_树形DP

原题: Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S...
  • u011343989
  • u011343989
  • 2014-02-19 15:31
  • 505

POJ - 3342 - Party at Hali-Bula (树形dp + 判断唯一解)

题目链接:http://poj.org/problem?id=3342题解:开一个dp数组,dp[i][0]表示第i个人不去能得到的最大价值,dp[i][1]表示第i个人去能得到的最大价值。 状态转...
  • Infinity_Izayoi
  • Infinity_Izayoi
  • 2016-07-22 13:59
  • 153

POJ3342 Party at Hali-Bula(树形DP)

题目大意: 有n个人要一起聚会,规定直接上司不能和他的直接下属一起出席。问最多能去几人是唯一解吗? 题目思路:树形DP+链试前向星:#include #include #include #incl...
  • qq_35124363
  • qq_35124363
  • 2016-06-13 08:55
  • 197

poj 3342 Party at Hali-Bula 树形dp

poj 3342 Party at Hali-Bula http://poj.org/problem?id=3342 题意:给定一些点,这些点最多只有一个父节点,且只有一个点没有父节点,要选择一个...
  • qianxun136930775
  • qianxun136930775
  • 2012-08-20 10:39
  • 176

POJ3342 Party at Hali-Bula(树的最大独立集-树形DP-刷表法)

题意: 给定一个树,选择若干点,使得选择的结点中任一结点不会和它的子结点同时选择,求能选结点最大数量。同时判断方案数是否为一。 思路:树的最大独立集,用树形dp,dfs一遍找每个结点的父亲,是为了...
  • kalilili
  • kalilili
  • 2015-03-14 11:55
  • 874

树形DP_____Party at Hali-Bula( POJ 3342 )

Description Dear Contestant, I'm going to have a party at my villa at Hali-Bula to celebrate my ...
  • y1196645376
  • y1196645376
  • 2016-08-21 02:08
  • 386
    个人资料
    • 访问:147771次
    • 积分:3494
    • 等级:
    • 排名:第10970名
    • 原创:183篇
    • 转载:2篇
    • 译文:0篇
    • 评论:18条
    博客专栏
    最新评论