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. Python数据类型之列表

    一.基本数据类型 整数:int 字符串:str(注:\t等于一个tab键) 布尔值: bool 列表:list (元素的集合) 列表用[] 元祖:tuple 元祖用() 字典:dict注:所有的数据类 ...

  2. 序列的方法(str,list,tuple)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在快速教程中,我们了解了最基本的序列(sequence).回忆一下,序列包含有定值 ...

  3. Threejs 使用的3D格式

    3D格式你可以通过任意软件导出(.3ds,dae等),但是threejs 无法使用, 1,http://www.blender.org/ 下载这款开源的3d软件 2,https://github.co ...

  4. Spring依赖注入

    依赖注入: 使用构造器注入 使用属性setter方法注入 使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员 ...

  5. ubuntu12.04 gdb安装使用

    参考文档:http://blog.csdn.net/haoel/article/details/2879 http://www.programlife.net/gdb-manual.html [新手笔 ...

  6. 关于Spring定时任务(定时器)用法

    Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...

  7. Android notifications通知栏的使用

    app发送通知消息到通知栏中的关键代码和点击事件: package com.example.notifications; import android.os.Bundle; import androi ...

  8. 用Volley让GridView加载网络图片

    一.布局文件 总共两个布局文件,一个是GridView,还有一个是GridView的item,是NetworkImageView和TextView activity_main.xml <Rela ...

  9. [C++]访问控制与继承(public,protect,private) 有时间再整理!!!

    http://www.cnblogs.com/chio/archive/2007/06/11/779408.html http://www.cnblogs.com/SelaSelah/archive/ ...

  10. Spring 定时任务的配置

    1.applicationContext.xml 中 加入task 的声明与xsd ? 1 xmlns:task="http://www.springframework.org/schema ...