题目:

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[][0](表示不选当前这个节点)或者dp[][1](表示选当前这个节点)这两个状态中的一个,所以你就想,,(假设父节点u子节点v)如果我当前节点用的是dp[u][1],那么对于子节点就制造不出矛盾,如果我用的是dp[u][0],那么我们就可以看子节点是否有矛盾(因为这时候子节点是可以选择的),如果我选和不选都可以取到max,那么矛盾就出来了。(有的同学会问,你这一个点取到max有啥用,万一我不取这一分支呢,但是其实是不存在这种顾虑的,因为你是dp啊,有更大的值肯定会选啊,,其实究其原因还是那句“最终的答案中一定每个顶点都用到了dp[][0]或者dp[][1]这两个状态中的一个”,,所以这俩状态哪个更大我肯定会取这个值,,(好吧其实还是用一个标记数组直接递推上去比较靠谱))

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int n;
const int INF=505;
int dp[INF][2];
int vis[INF];//标记所访问过的点,每个点只需访问一次
int mp[INF][2];//mp[j][1]为1,表示选当前这个节点,方案唯一,反之则不唯一。
//mp[j][0]为1,表示不选这个节点时,方案唯一,反之则不唯一
vector<int>v[INF];
void dfs(int x)
{dp[x][1]=1;mp[x][1]=1;mp[x][0]=1;vis[x]=1;int m=v[x].size() ;int i,j,k;for(i=0;i<m;i++){j=v[x][i];if(vis[j]==0){dfs(j);dp[x][0]+=max(dp[j][0],dp[j][1]);//父亲节点没选,子节点选不选均可,所以取两者较大者 dp[x][1]+=dp[j][0];//父亲节点选了,子节点不能再选 if(dp[j][0]==dp[j][1])//会造成方案不唯一 {mp[x][0]=0;}if(mp[j][0]==0){mp[x][1]=0;}}}
}
int main()
{while(1){scanf("%d",&n);if(n==0)break;int i,j,k,val;for(i=0;i<INF;i++){v[i].clear() ;}string a1,a2;cin>>a1;map<string,int>q;int cnt=1;q[a1]=cnt++;for(i=1;i<n;i++){cin>>a1>>a2;if(!q.count(a1) ){q[a1]=cnt++;}if(!q.count(a2) ){q[a2]=cnt++;}v[q[a2]].push_back(q[a1]); }memset(dp,0,sizeof(dp));memset(vis,0,sizeof(vis));dfs(1);//注意只有两种情况下方案唯一 if(dp[1][0]>dp[1][1]&&mp[1][0]==1){printf("%d Yes\n",dp[1][0]);}else if(dp[1][1]>dp[1][0]&&mp[1][1]==1){printf("%d Yes\n",dp[1][1]);}else printf("%d No\n",max(dp[1][0],dp[1][1]));}return 0;
}

POJ 3342- Party at Hali-Bula (树形dp+判断是否唯一)相关推荐

  1. POJ 2342 | HDU 1520 Anniversary party 树形DP(入门题)

    传送门:POJ 2342 题目大意: 有若干人参加一个聚会,如果两个人之间有直接的上下属关系,则只能去一个.每个人都有个高兴值,问高兴值之和最大是多少? 思路: 之前一直觉得树形DP比较难,现在发现树 ...

  2. 【POJ - 1947】Rebuilding Roads (树形dp,背包问题,树形背包dp)

    题干: The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1. ...

  3. 【POJ - 2378】Tree Cutting(树形dp,树的重心变形)

    题干: After Farmer John realized that Bessie had installed a "tree-shaped" network among his ...

  4. POJ 1848 (一道不错的树形dp)

    题意:N个点的一颗树.问最少添加多少条边可以让每个点都在一个(且仅一个)环中. 不得不佩服,这题dp设计出来的人...偶是弱菜,只能膜拜了. 这位大牛的解说,很详细:http://hi.baidu.c ...

  5. POJ 1655 求树的重心(树形dp)

    题目链接 树的重心: 若树上的一个节点满足其所有的子树中最大的子树节点数最少,那么这个点就是这棵树的重心. 1.任选一个点为根,只要统计出每个点的子树大小,就能很快求出每个点子树节点的数量的最大值. ...

  6. Party at Hali-Bula(树形DP+判断方案数是否唯一)

    Party at Hali-Bula UVA - 1220 题意:  公司里有n(n<=200)个人形成一个树状结构, 要求尽量选多的人,但不能同时选择一个人和他的直属上司,文最多能选多少人,以 ...

  7. pku 1463 Strategic game 树形DP

    http://poj.org/problem?id=1463 对于树形DP不大来感啊,才开始做的时候考虑成覆盖全部点了,致使我拓扑做了,WA了好几次.感觉是树形DP好像以前做过类似的题目可是就是想不出 ...

  8. POJ 3342 树形DP+Hash

    这是很久很久以前做的一道题,可惜当时WA了一页以后放弃了. 今天我又重新捡了起来.(哈哈1A了) 题意: 没有上司的舞会+判重 思路: hash一下+树形DP 题目中给的人名hash到数字,再进行运算 ...

  9. POJ 1155 TELE【树形DP】

    POJ 1155 TELE http://poj.org/problem?id=1155 大意:某电台要广播一场比赛,该电台网络是由N个网点组成的一棵树,其中M个点为客户端, 其余点为转发站.客户端i ...

最新文章

  1. poj1738 an old stone game
  2. python编程在哪里写-python入门该从哪里开始?
  3. OpenCV 3.x Lib 源码结构简介
  4. golang函数多值返回示例
  5. 陶哲轩实分析定义8.5.5的一个注记
  6. 笔记本电脑摄像头不能用_聊一款想代替笔记本电脑的产品
  7. 【转】Asp.net的生命周期之应用程序生命周期
  8. 杭电 1060 Leftmost Digit
  9. php sql注入审计,php审计基础一:sql注入
  10. 一个网络传输框架——zeroMQ 调研笔记
  11. 史上最详细的Hadoop环境搭建
  12. 安卓bochs安装linux教程,Ubuntu 14.04 LTS 安装和配置Bochs
  13. 一图看懂16个英语时态
  14. 百度地图api-基本用法总结
  15. (五)青龙面板 企业微信应用推送+详细教程【2022年5月20日】
  16. 【战术性mark】JS 复制内容到剪贴板
  17. 如果有智慧公交可视化平台,《开端》还能无限重启吗?
  18. 扫描二维码启动微信打开特定页面
  19. goldengate中添加同步表的操作
  20. 如何为餐饮商家打造代运营解决方案?

热门文章

  1. Xshell+docker镜像/容器+tensorflow环境下的模型训练全过程
  2. C++学习笔记之pragma once的理解
  3. 制定(图5-33)所示夹锥套的加工工艺,设计钻6-8.5孔的钻床夹具 课程设计
  4. 纯CSS3制作皮卡丘动画壁纸
  5. CSS Scrollbar
  6. ubuntu20.04 LTS 手把手安装教程
  7. 软考报名后,这些细节也不容忽略
  8. 允许ajax跨域请求
  9. 苹果笔记本的end键_Mac 笔记本电脑:按键位置和功能
  10. 流放之路服务器连接中断,流放之路客户端崩溃报错怎么办