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. XAF实现运行时填加验证规则并保存到数据库中

    有几种方法可以用来声明一个验证规则.最常用的方法是使用对应的Attribute来定义.详见这里.验证模块还允许您通过在业务类实现 IRuleSource 接口定义自定义的验证规则的来源. IRuleS ...

  2. Nginx简介及配置实用

    Nginx简介 Nginx是一个高性能的HTTP和反向代理服务器: 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...

  3. SQL SERVER赋权限

    --创建登录账户 use master GO EXEC sp_addlogin 'jacky', 'pwd' --EXEC sp_droplogin 'jacky' --删除登陆账户 use Test ...

  4. thinkphp 初始配置

    他喵的,去做了个其他的模板,一段时间不碰tp,居然配置了好久 记录留备用 一.把下好的ThinkPHP放到根目录的文件夹下 ,例如www文件夹下 在www目录下新建文件夹admin和home 新建入口 ...

  5. [转载] EXPLAIN执行计划中要重点关注哪些要素

    原文: https://mp.weixin.qq.com/s?__biz=MjM5NzAzMTY4NQ==&mid=400738936&idx=1&sn=2910b4119b9 ...

  6. HDU5716, HDU5745【dp+bitset】

    DP+bitset  HDU5716 dp[i][j] = dp[i-1][j-1] && (s[i] in set[j]); 第二维压bitset #include <bits ...

  7. 百度之星复赛Astar Round3

    拍照 树状数组(SB了).求出静止状态下,每个点能看到多少个向右开的船c1[i],多少个向左开的船c2[i]. max{c1[i] + c2[j], (满足i <= j)  }即为答案.从后往前 ...

  8. 【Todo】Python字符编码学习

    Python中经常出现字符编码问题,在这里统一整理吧. 参考这篇文章:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 另外这个人 ...

  9. iOS开发 判断扫码是否为有效网址

    - (BOOL)achiveStringWithWeb:(NSString *)infor { NSString *emailRegex = @"[a-zA-z]+://.*"; ...

  10. linux笔记:RPM软件包管理-yum在线管理

    ip地址配置: 用ifconfig命令只能配置ip和子网掩码,这样只能访问内网:如果需要访问公网则还必须要网关和DNS. 使用setup工具配置ip: 网络yum源配置: 常用yum命令:查询 常用y ...