1220 - Party at Hali-Bula

Time limit: 3.000 seconds

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 <tex2html_verbatim_mark>(1n200) <tex2html_verbatim_mark>, the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n - 1 <tex2html_verbatim_mark>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啊。

每个节点选择或不选择直接递归求就可以。

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define MAXN 205
vector<int> v[MAXN];
int n;
string s1, s2;
map<string, int> ma;
int tot;
struct Num{
int d, f;
Num() : d(), f() {}
}num;
int get_id(string s)
{
if(ma[s]) return ma[s];
else return ma[s] = ++tot;
} Num dp(int u, int flag)
{
Num ans;
if(flag) ans.d = ;
else ans.d = ;
if(v[u].empty()) return ans;
int k = v[u].size();
int d = , f = ;
if(flag) {
repu(i, , k) {
Num t = dp(v[u][i], );
d += t.d;
if(t.f == ) f = ;
}
ans.d = d + ;
ans.f = f;
}
else {
repu(i, , k) {
Num t1, t2;
t1 = dp(v[u][i], );
t2 = dp(v[u][i], );
if(t1.d == t2.d) f = , d += t1.d;
else if(t1.d > t2.d) {
if(t1.f == ) f = ;
d += t1.d;
}
else {
if(t2.f == ) f = ;
d += t2.d;
}
}
ans.d = d;
ans.f = f;
}
return ans;
} int main()
{
while(~scanf("%d", &n) && n)
{
ma.clear();
tot = ;
repu(i, , n + ) v[i].clear();
cin>>s1;
get_id(s1);
repu(i, , n) {
cin>>s1>>s2;
v[get_id(s2)].push_back(get_id(s1));
}
Num t1 = dp(, ), t2 = dp(, );
if(t1.d == t2.d) printf("%d No\n", t1.d);
else if(t1.d > t2.d) {
printf("%d ", t1.d);
if(t1.f) printf("Yes\n");
else printf("No\n");
}
else {
printf("%d ", t2.d);
if(t2.f) printf("Yes\n");
else printf("No\n");
}
}
return ;
}

 

uva 1220的更多相关文章

  1. POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)

    POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 12 ...

  2. Uva 1220,Hali-Bula 的晚会

    题目链接:https://uva.onlinejudge.org/external/12/1220.pdf 题意: 公司n个人,形成一个数状结构,选出最大独立集,并且看是否是唯一解. 分析: d(i) ...

  3. UVa 1220 - Party at Hali-Bula(树形DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. UVa 1220 (树的最大独立集) Party at Hali-Bula

    题意: 有一棵树,选出尽可能多的节点是的两两节点不相邻,即每个节点和他的子节点只能选一个.求符合方案的最大节点数,并最优方案判断是否唯一. 分析: d(u, 0)表示以u为根的子树中,不选u节点能得到 ...

  5. UVA - 1220 Party at Hali-Bula 树的最大独立集

    题意:  给定n个人,存在上下级关系,每个人只有一个上级,求最大独立集.并判断最大独立集是否唯一 思路:d[i][0]表示以i为根的子树中,不选择第i个节点的最大独立集,f[i][0]表示以i为根的子 ...

  6. UVa 1220 Hali-Bula的晚会(树的最大独立集)

    https://vjudge.net/problem/UVA-1220 题意: 公司里有n个人形成一个树状结构,即除了老板以外每个员工都有唯一的直属上司.要求选尽量多的人,但不能同时选择一个人和他的直 ...

  7. UVa 1220 Party at Hali-Bula (树形DP,最大独立集)

    题意:公司有 n 个人形成一个树形结构,除了老板都有唯一的一个直系上司,要求选尽量多的人,但不能同时选一人上和他的直系上司,问最多能选多少人,并且是不是唯一的方案. 析:这个题几乎就是树的最大的独立集 ...

  8. UVA - 1220 Party at Hali-Bula (树形DP)

    有 n 个员工,n-1个从属关系. 不能同时选择某个员工和他的直接上司,问最多可以选多少人,以及选法是否唯一. 树上的最大独立集问题.只不过多了一个判断唯一性. dp[u][0]表示不选这个点的状态, ...

  9. UVA 1220 Party at Hali-Bula (树形DP)

    求一棵数的最大独立集结点个数并判断方案是否唯一. dp[i][j]表示以i为根的子树的最大独立集,j的取值为选和不选. 决策: 当选择i时,就不能选择它的子结点. 当不选i时,它的子结点可选可不选. ...

随机推荐

  1. CUBRID学习笔记 18 sql语句的预处理(类似存储过程)

    定义预处理  类似sqlserver的存储过程 语法 PREPARE stmt_name FROM preparable_stmt 说明 PREPARE 关键字 stmt_name 预处理语句的名字 ...

  2. Redis事务的分析及改进

    Redis事务的分析及改进 Redis的事务特性 数据ACID特性满足了几条? 为了保持简单,redis事务保证了其中的一致性和隔离性: 不满足原子性和持久性: 原子性 redis事务在执行的中途遇到 ...

  3. iOS : 静态库制作

    一.静态库简介 1. 什么是库? 库 就是程序代码的集合, 是共享程序代码的一种方式 2. 库的分类? 开源库 公开源代码, 能看到具体实现 例如MJExtension, MJRefresh, AFN ...

  4. return、 return false的用法

    1. return返回null,起到中断方法执行的效果,只要不return false事件处理函数将会继续执行,表单将提交2. return false,事件处理函数会取消事件,不再继续向下执行.比如 ...

  5. const 与 readonly 知多少

    const与readonly 很像,都是将变量声明为只读,且在变量初始化后就不可改写.那么,const与readonly 这两个修饰符到底区别在什么地方呢?其实,这个牵扯出C#语言中两种不同的常量类型 ...

  6. 搭建SSH入过的那些坑

    1.添加完相关jar包,写完配置文件,写完测试类,运行提示 WARN:Establishing SSL connection without server's identity verificatio ...

  7. 转 图片资源加密,Lua文件加密

    游戏开发中常遇到资源保护的问题. 目前游戏开发中常加密的文件类型有:图片,Lua文件,音频等文件,而其实加密也是一把双刃剑. 需要安全那就得耗费一定的资源去实现它.目前网上也有用TexturePack ...

  8. FlashPlayer for Android

    1. Manually install on Android devices 教程地址:“https://helpx.adobe.com/flash-player/kb/installing-flas ...

  9. Ajax异步调用使用

    //验证通知号重复 function checkinformcodeagage() { var informcode = $("#txtinformcode").val(); if ...

  10. Python中的join()函数split()函数

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:     join():    连接字符串数组.将字符串.元组.列表中的元素以指定的 ...