POJ3342——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
树形dp入门题,前半部分非常easy,后半部分非常难搞,详见点击打开链接
#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;
}
POJ3342——Party at Hali-Bula的更多相关文章
- 【poj3342】 Party at Hali-Bula
http://poj.org/problem?id=3342 (题目链接) 题意 给出一棵树,要求在不存在两个节点相邻的条件下,选出尽可能多的节点,并且判断是否有多种选法. Solution 很水的树 ...
- poj3342 Party at Hali-Bula
树形dp题,状态转移方程应该很好推,但一定要细心. http://poj.org/problem?id=3342 #include <cstdio> #include <cstrin ...
- POJ3342 Party at Hali-Bula(树形DP)
dp[u][0]表示不选u时在以u为根的子树中最大人数,dp[u][1]则是选了u后的最大人数: f[u][0]表示不选u时的唯一性,f[u][1]是选了u后的唯一性,值为1代表唯一,0代表不唯一. ...
- poj 3680 Intervals
给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...
- jQuery 遍历 - parent() 方法
ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法 parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...
- (转)TCP注册端口号大全
分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...
- 【转】CString类型互转 int
CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int 将字 ...
- 阿里云ECS被攻击
今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...
- 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 ...
随机推荐
- Java就业前景怎么样?学了后好找工作吗?
不知道大家对Java就业前景了解多少.随着信息化的发展.IT培训受倒了越来越多人的追捧.在开发领域,JAVA培训成为了很多人的首选!JAVA应用广泛.JAVA培训就业前景良好. 眼下.尽管JAVA人才 ...
- AsyncSocket长连接棒包装问题解决
project正在使用长连接快来server沟通.因此,指定我们的协议前两个字节为数据长度来区分数据包 app这边数据有两种传输形式: 1.app主动请求所须要的数据: 2.app异步接收来自服务端的 ...
- 类似的微博推断客户关系sql声明
类别似新浪微博的关注和共同关心 不知道别人是怎么设计的. 反正我是例如以下设计的 ID USER FRIEND 1 A B 2 B A 3 ...
- 房费制 它 结账BUG
声明:以下内容仅仅是对在桌子上的卡与卡表的后面,适合学生的表! 最近,我们已经开始做VB.NET系统重构版,在这里跟大家聊聊我在机房收费系统中发现的漏洞. 在机房收费系统中有这样一个窗口--结 ...
- MFC 盾webBrowser打开弹出的页面
void CansDlg::NewWindow3Explorer1(LPDISPATCH* ppDisp, BOOL* Cancel, unsigned long dwFlags, LPCTSTR b ...
- 复制(5)——事务复制中的发布者(Publisher)
发布者是所有被复制(replicated)的数据的集合.每个发布者可以有多个发布(publication),每个发布项包含多个项目(articles),但是这些发布必须处于一个单一的数据库中,而每个项 ...
- JSTL实现int数据的类型的长度
JSTL否int类型转换string该功能.为了解决增益int数据的类型的长度,闹失踪. 得到String的数据的长度是jstl的fn能够${fn:length(string)},但int做数据? 经 ...
- jquery ui tab跳转
1.tabs_iframe.jsp <%-- Document : tabs Created on : 2015-2-28, 14:44:02 Author : liyulin lyl01099 ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Ajax基础知识(一)
随便在百度谷歌上输入Ajax都会出现一大堆的搜索结果,这已经不再是什么新奇的技术了.但若从一开始就学习了ASP.Net,使用功能齐全的Visual Studio集成开发工具,或许未必能对访问一个动态网 ...