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. 【VB6笔记-02】从Command中获取链接参数

    Public Sub GetParameters() Dim Para As String Para = Command$() gstrUserID = GetCommandPara(Para, ) ...

  2. linux配置IP地址

    1. ifconfig命令临时配置IP地址 ifconfig命令:查看与配置网络状态命令 如:  ifconfig eht0 192.168.0.200 netmask 255.255.255.0 # ...

  3. SQL中char、varchar、nvarchar的区别(zhuan)

    char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.   nvarcha ...

  4. 强大!基于拖放布局的 Twitter Bootstrap 网站生成器

    强大!基于拖放布局的 Twitter Bootstrap 网站生成器 网址如下 http://www.layoutit.com/build http://demo.sc.chinaz.com/File ...

  5. UVA 11468【AC自动机+DP】

    dp[i][j]表示走了i步走到j结点的概率.初始值dp[0][0] = 1.当走到的结点不是单词尾结点时,才能走过去. !end[i]&&last[i] == root时,该结点才可 ...

  6. intersection-of-two-arrays-ii

    https://leetcode.com/problems/intersection-of-two-arrays-ii/ class Solution { public: vector<int& ...

  7. redhat 6.4 yum 本地配置简记

    准备工作 ----------------------------------------------------------------------------- 1. 加载光驱  将iso镜像文件 ...

  8. 在linux(CentOS-6.7_x86_64)上安装mysql成功记录

    查看linux服务器的yum源设置: [root@hadoop03 yum.repos.d]# cd /etc/yum.repos.d [root@hadoop03 yum.repos.d]# ll ...

  9. maven install 报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project*****

    [ERROR]Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-co ...

  10. 什么是cname a记录

    https://support.dnsimple.com/articles/cname-record/ CNAME就是别名记录,就是负责跳转,比如你给某个地址设置了一个cname,那当访问那个cnam ...