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. oracle PL/SQL基础编程

    PL/SQL(Procedural Language/SQL)是oracle中引入的一种过程化编程语言 PLS-00103:出现符号"declare"在需要下列之一时 符号&quo ...

  2. TestNg测试框架使用笔记

    Gradle支持TestNG test { useTestNG(){ //指定testng配置文件 suites(file('src/test/resources/testng.xml')) } } ...

  3. Github上不错的Android开源代码(一)

    总有一些朋友很热心的整理一些好的资料,在收集之后,可以用作阅读.学习和实践.小伙伴们,总有一天,你也能写出 Niubility 的 Android App :-) 为了防止以上链接失效,以及部分内容丢 ...

  4. [poj2785]4 Values whose Sum is 0(hash或二分)

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

  5. XML 解析器

    所有现代浏览器都内建了供读取和操作 XML 的 XML 解析器.解析器把 XML 转换为 XML DOM 对象 - 可通过 JavaScript 操作的对象. 解析 XML 文档为DOM对象 方法一: ...

  6. (二)miller指导查看主控板寄存器操作

    Welcome to Command Shell!Username:admin Password:***** ROS>en ROS# ROS# ROS# ROS# ROS#^ada ROS(ad ...

  7. jquery定位

    1.$("div").offset().left ; div到文档的左距离(offset() 方法返回或设置匹配元素相对于文档的偏移) $("div").off ...

  8. LTIB常用命令3

    http://blog.csdn.net/junht/article/details/7656540 LTIB 中的包 4.3.1  查看哪些包是使能的,并且可以安装 在配置ltib之前,您可以在一个 ...

  9. jquery中DOM

    节点包裹 wrap() (1)$().wrap(html) 将选择的节点用指定的元素包装 $('p').wrap('<div></div>'); (2)多层包裹 $('p'). ...

  10. IE6 + png24 透明图片(滤镜)

    IE6 + png24 透明图片(滤镜) .IE6png{_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true ...