[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⑴ 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的员工参加1个party来庆祝他的退休。还是每一个人不能同其直接boss1起参加。要求使参加人数最多,并要求判断方案是不是唯1。

思路
计算最大的很简单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]判断,如果选的子树方案不唯1,该方案不唯1;初始当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]));
}
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » [poj 3342]Party at Hali-Bula 树形dp

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡