D - Party at Hali-Bula(树上DP)

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

题意

一个公司的人去吃饭,老板和员工不能同时在,一个员工只有一个上级老板,有一个大老板(树根)
很明显是一棵树,求最多可以有多少人去吃饭。

思路

树上dp
老板不去,员工可以去,也可以不去
老板去,员工不去
一共就这几种情况
状态转移方程:(dp[a][0] 表示a不去有多少客人,dp[a][1] 表示a去有多少客人)
老板不去,员工可以去,也可以不去,找最优
root是老板,p是员工

dp[root][0]+=max(dp[p][1],dp[p][0]);

老板去,员工一定不去

	dp[root][1]+=dp[p][0];

还有一个十分关键的是如何判断
客人列表是否是唯一的。
用了u数组,u[a][0]=0表示dp[a][0]解是不唯一的,u[a][0]=1表示dp[a][0]解是唯一的
u[a][1]=0表示dp[a][1]解是不唯一的,u[a][1]=1表示dp[a][1]解是唯一的
其中如果老板不去,而员工去人数多,那我们在 dp[root][0]+=max(dp[p][1],dp[p][0]); 加的其实是
dp[p][1],我们要判断它是不是解不唯一,他如果不唯一那老板的解也是不唯一的。看代码

else if((dp[p][1]>dp[p][0])&&u[p][1]==0)
		{
				u[root][0]=0;//dp[root][0]这个解也是不唯一的
		}

其实你这个答案中其中有一小步的客人有多种选法,那这个答案就是有多种选法的
看AC代码,如果有不明白的直接问就好,欢迎交流,关键是看懂深搜的递归

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<stdio.h>
#include<string.h> 
#include<limits.h>
#include<vector>
#include<queue>
#include<map>
#define maxn  1000
using namespace std;

vector<int>v[maxn];//用来存图

int n;
int dp[maxn][2];
int u[maxn][2];
 
void add(int b1,int b2)
{

 
 v[b1].push_back(b2);//
}


void f(int root)
{
	int i,j,r;
	r=v[root].size();
	
	u[root][1]=1;
	u[root][0]=1;
	dp[root][1]=1;
	dp[root][0]=0;	
	if(r==0)
	{
	return;
	}
	
	for(i=0;i<v[root].size();i++)
	{
		int p=v[root][i];
		
		f(p);
		if(dp[p][1]==dp[p][0])
		{
			u[root][0]=0;
		}
		else if((dp[p][1]>dp[p][0])&&u[p][1]==0)
		{
				u[root][0]=0;
		}
		else if((dp[p][1]<dp[p][0])&&u[p][0]==0){
			
			u[root][0]=0;
		}
		
		if(u[p][0]==0)
		{
			u[root][1]=0;
		}
	
		dp[root][0]+=max(dp[p][1],dp[p][0]);
		dp[root][1]+=dp[p][0];
	}
}
int main()
{
	int i,j,k,a1,a2,t;
	char ch[120];
	while(scanf("%d",&n)&&n)
	{
		t=1;
		map<string,int>loc;//是为了把字符串转换为数字,不明白的建议学一下c++map
	//printf("%d\n",n);
  	     memset(dp,0,sizeof(dp));
  	     memset(u,0,sizeof(u));
  	     for( i=0;i<=n*2;i++)
      {
          v[i].clear();
       }
  	     
  	     scanf("%s",ch);
  	     if(loc[ch]==0)
  	     {
  	     	loc[ch]=t++;
		   }
      
	   for(i=1;i<n;i++)
	  {
         scanf("%s",ch);
         if(loc[ch]==0)
  	     {
  	     	loc[ch]=t++;
		}
		   a1=loc[ch];
	     scanf("%s",ch);
	     if(loc[ch]==0)
  	     {
  	     	loc[ch]=t++;
		}
		a2=loc[ch];
		
		add(a2,a1);	
	   }
	
	f(1);
	if(dp[1][0]>dp[1][1])
	{
		if(u[1][0]==0)
		{
			printf("%d No\n",dp[1][0]);
		}
		else{
			printf("%d Yes\n",dp[1][0]);
		}
	}
	else if(dp[1][0]<dp[1][1])
	{
			if(u[1][1]==0)
		{
			printf("%d No\n",dp[1][1]);
		}
		else{
			printf("%d Yes\n",dp[1][1]);
		}
	}
	else{
			printf("%d No\n",dp[1][1]);
	}
	//printf("%d\n",max(dp[1][0],dp[1][1]));
	   
	}
	return 0;
	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值