POJ3342——Party at Hali-Bula

D - Party at Hali-Bula(树上DP 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 ... 阅读详情
Party at Hali-Bula
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5418 Accepted: 1920

Description

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

Source

Tehran 2006

树形dp入门题,前半部分很简单,后半部分很难搞,详见 点击打开链接

#include <map>  
#include <set>  
#include <list>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <cstdio>  
#include <cmath>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
  
using namespace std;

int dp[222][2];
struct node
{
	int next;
	int to;
}edge[222];
int head[222];
char str[111], tr[111];
 
int tot, n;

void addedge(int from, int to)
{
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void dfs(int u)
{
	dp[u][1] = 1;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		dfs(v);
		dp[u][1] += dp[v][0];
		dp[u][0] += max(dp[v][1], dp[v][0]);
	}	
}

int main()
{
	while (~scanf("%d", &n), n)
	{
		map<string, int>qu;
		qu.clear();
		memset (head, -1, sizeof(head) );
		memset (dp, 0, sizeof(dp));
		tot = 0;
		int res = 0;
		scanf("%s", str);
		qu[str] = ++res;
		for (int i = 1; i <= n - 1; i++)
		{
			scanf("%s%s", str, tr);
			if (qu[str] == 0)
			{
				qu[str] = ++res;
			}
			if (qu[tr] == 0)
			{
				qu[tr] = ++res;
			}
			addedge(qu[tr], qu[str]);
		}
		dfs(1);
		printf("%d ", max(dp[1][0], dp[1][1]));
		bool flag = false;
		if (n == 1)
		{
			printf("Yes\n");
			continue;
		}
		if (n == 2)
		{
			printf("No\n");
			continue;
		}
		for (int i = 1; i <= n; i++)
		{
			if (dp[i][1] == dp[i][0])
			{
				for (int j = head[i]; ~j; j = edge[j].next)
				{
					if (dp[edge[j].to][1] == dp[edge[j].to][0])
					{
						flag = true;
						break;
					}
				}
				if (flag)
				{
					break;
				}
			}
		}
		if (flag)
		{
			printf("No\n");
			continue;
		}
		printf("Yes\n");
	}
	return 0;
}


POJ3342Party at Hali-Bula题解动态规划DP Party at Hali-BulaTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 2676 Accepted: 994DescriptionDear 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 im 阅读详情

相关推荐

Party at Hali-Bula POJ - 3342(树形dp

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 ...

starlet_kiss的博客 267

POJ - 3342Party at Hali-Bula(树形dp,最大独立集,是否有唯一解)

题干: 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 wh...

xuanweiace的博客 500

POJ 3342- Party at Hali-Bula (树形dp+判断是否唯一)

题目: 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 wh...

weixin_44757834的博客 290

[poj 3342]Party at Hali-Bula 树形dp

题目链接:http://poj.org/problem?id=3342 Party at Hali-BulaTime Limit: 2000MS Memory Limit: 65536K Total Submissions: 5997 Accepted: 2140 DescriptionDear Contestant,I’m going to have a party at my vill

ATOD 3167

Poj3342 Party At Hali-Bula 树型dp

题目传送门 http://poj.org/problem?id=3342 因为名字是唯一的,所以可以采用map来对名字进行编号,再通过vector建立树的关系。 AC代码 #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;cstring&gt; #include &lt;algorithm&gt; #includ...

Juicewyh的博客 3688

POJ 3342 Party at Hali-Bula

树形dp 入门,先输入大boss名字, 接下来n-1组,输入

uuuuuu 556

[POJ3342]Party at Hali-Bula

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

363

POJ 3342 Party at Hali-Bula(树形DP

题目链接 相比POJ 2342那题,增加了一问:最佳选择是否唯一的. 首先可根据雇员与老板的关系建立一颗树,老板做根节点,雇员是该根节点的儿子。  如上面这棵简易的树,对于节点A有两种决策:  选与不选。  若选择A,则B 、C都不能选,问题转换为求所有以 “B、C的儿子为根节点的树” 子问题最优解之和。  若不选A, 则问题转换为求“以B、C为根节点"的子树的最优解之和。

u011276914的专栏 768

poj3342 Party at Hali-Bula

树形dp

YHaX的博客 343

Party at Hali-Bula POJ3342

<br />AC的第一道水树形DP,纪念下!详解以后再说,理由同上,嘿嘿!那个判断是否唯一借鉴的别人的思想!<br />#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 205 #define Max(a,b) a>b?a:b int G[N][N]; //存子节点个数,及坐标 int dp[N][2]; //存值 int n,last,flag; char name[N][N]; int Get_id(c

surfacedust的专栏 632

POJ3342 Party at Hali-Bula 树形DP

这题是问聚会时老板与员公不能同时出现的方案ov

MY专栏 444

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

POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划) Description Dear Contestant, I'm going to have a party at my vill...

dipinzhu4111的博客 165

poj3342——Party at Hali-Bula

树状dp。必要条件:子树之间不可以相互干扰。思路来源于ppt.#include #include #include using namespace std; int head[210]; char name1[110],name2[110]; char name[210][110]; int cnt,len; class node { public: int v,yes,no; bool yessole,nosole; bool tt; bool vis; int

r_ax的专栏 568

POJ - 3342 Party at Hali-Bula 树形dp入门

原题链接:http://poj.org/problem?id=3342 目录题意分析code 题意 和没有上司的舞会题意基本一致,多了一个判断答案是否唯一 分析 设两个状态设两个状态设两个状态 dp[x][1/0]表示取或不取当前节点获得的最大值dp[x][1/0] 表示取或不取当前节点获得的最大值dp[x][1/0]表示取或不取当前节点获得的最大值 f[x][1/0]表示取或不取当前节点答案是否唯一f[x][1/0]表示取或不取当前节点答案是否唯一f[x][1/0]表示取或不取当前节点答案是否唯一 状态转

kaka03200的博客 222

poj 3342 Party at Hali-Bula

简单的树形dp, 先建好图, 然后在dfs的过程中进行dp      dp[i][j]   标记j表示人的序号   i有2种状态 1, 0,分别表示此人是否参加聚会。  具体的dp过程   当i为1是, 他的孩子状态必须全部为0, 如果为1是则无硬性要求,只要求人最多。  这题唯一麻烦的便是唯一性的判断,要是如果j取其孩子时,如果孩子不唯一,则j肯定不唯一,这是必然的道理。  具体的判断过程

purevegetable 865

POJ - 3342 Party at Hali-Bula(树形dp)

题目链接:点击查看 题目大意:n个人参加聚会,每个人都不想和老板一起参加,问最多可以有多少个人参加,并且判断方案唯一性 题目分析:这个类型的题目这已经是第三个了,状态转移方程都一模一样,不过这个题有点不同的地方是需要判断唯一性,我看到网上有两种方法,一种是通过状态转移的过程中如果发现dp[v][1]==dp[v][0]就设置一个flag为false,即在这个地方就出现了分歧,因为想起来和写起来...

Falcon的博客 481

【树形动态规划】poj3342 Party at Hali-Bula

代码好长,调了2个小时。。。。 #include #include #include #include #include using namespace std; vector v[20005]; int n,maximum,boss,record[20005][2]; char str[20005][105],str_boss[20005][105]; bool notUnique[2000

BananaTree 989
上一篇: hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.
下一篇: Codeforces Round #260 (Div. 2)——C. Boredom
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值