Party at Hali-Bula - POJ 3342 树形dp
2014-07-09 20:43
399 查看
Party at Hali-Bula
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 followingn-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
Sample Output
题意:给你N组人,第一组只有一个人,是最大的Boss,后面的组,每组的后一个人是前一个人的老板,并且能够形成一个树形。不能同时邀请有直接关系的老板和雇员,问你最多邀请多少人,并且如果只有一种邀请方式输出Yes。
思路:dp[i][0]表示第i个人不参加的最大人数,dp[i][1]表示第i个人参加的最大人数。way[i][0],way[i][1]分别表示其最大人数的情况下,邀请方式是不是唯一的。然后可以通过雇员来推出他的老板的状态。具体的转移方程见代码吧。
AC代码如下:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5200 | Accepted: 1850 |
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 followingn-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
题意:给你N组人,第一组只有一个人,是最大的Boss,后面的组,每组的后一个人是前一个人的老板,并且能够形成一个树形。不能同时邀请有直接关系的老板和雇员,问你最多邀请多少人,并且如果只有一种邀请方式输出Yes。
思路:dp[i][0]表示第i个人不参加的最大人数,dp[i][1]表示第i个人参加的最大人数。way[i][0],way[i][1]分别表示其最大人数的情况下,邀请方式是不是唯一的。然后可以通过雇员来推出他的老板的状态。具体的转移方程见代码吧。
AC代码如下:
#include<cstdio> #include<cstring> #include<map> #include<vector> #include<string> #include<algorithm> using namespace std; vector<int> vc[210]; map <string ,int > match; int dp[210][2]; bool way[210][2]; char s[210]; void dfs(int a) { int i,j,k; dp[a][0]=0; dp[a][1]=1; for(i=0;i<vc[a].size();i++) { k=vc[a][i]; dfs(k); dp[a][0]+=max(dp[k][0],dp[k][1]); if(dp[k][0]>dp[k][1]) { if(way[k][0]) way[a][0]=1; } else if(dp[k][0]<dp[k][1]) { if(way[k][1]) way[a][0]=1; } else way[a][0]=1; dp[a][1]+=dp[k][0]; if(way[k][0]) way[a][1]=1; } //printf("way %d %d %d\n",a,way[a][0],way[a][1]); } int main() { int n,i,j,k,u,v,num=1,ans; while(~scanf("%d",&n) && n>0) { for(i=0;i<=n;i++) vc[i].clear(); memset(dp,0,sizeof(dp)); memset(way,0,sizeof(way)); match.clear(); scanf("%s",s); match[s]=1; num=1; for(i=2;i<=n;i++) { scanf("%s",s); if(match[s]==0) match[s]=++num; u=match[s]; scanf("%s",s); if(match[s]==0) match[s]=++num; v=match[s]; vc[v].push_back(u); } dfs(1); ans=max(dp[1][0],dp[1][1]); if(dp[1][0]>dp[1][1]) { if(way[1][0]) printf("%d No\n",ans); else printf("%d Yes\n",ans); } else if(dp[1][0]<dp[1][1]) { if(way[1][1]) printf("%d No\n",ans); else printf("%d Yes\n",ans); } else printf("%d No\n",ans); } }
相关文章推荐
- POJ 3342 Party at Hali-Bula(树形DP)
- POJ 3342 - Party at Hali-Bula 树形DP+最优解唯一性判断
- poj 3342 Party at Hali-Bula(树形dp)
- POJ - 3342 Party at Hali-Bula 树形DP
- 树形DP_____Party at Hali-Bula( POJ 3342 )
- [poj 3342]Party at Hali-Bula 树形dp
- poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)
- poj 3342 Party at Hali-Bula 树形dp
- poj 3342 Party at Hali-Bula(树形dp)
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- POJ - 3342 - Party at Hali-Bula (树形dp + 判断唯一解)
- POJ 3342 Party at Hali-Bula 树形DP 最大独立集
- poj 3342 Party at Hali-Bula 树状dp
- pku 3342 Party at Hali Bula 树形dp 解题报告
- HDU 2412 / POJ 3342 Party at Hali-Bula(树形DP+判断多解)
- POJ3342 Party at Hali-Bula 树形DP
- Party at Hali-Bula UVA - 1220 (简单树形dp)
- Party at Hali-Bula(树形dp)
- hdu 2412 Party at Hali-Bula (树形DP)