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 ...
随机推荐
- .NET 并行(多核)编程系列之七 共享数据问题和解决概述
原文:.NET 并行(多核)编程系列之七 共享数据问题和解决概述 .NET 并行(多核)编程系列之七 共享数据问题和解决概述 前言:之前的文章介绍了了并行编程的一些基础的知识,从本篇开始,将会讲述并行 ...
- smark和openfire即时通信代码
从:http://blog.csdn.net/casuallc/article/details/34794501 server:openfire client计划:smark写 首先安装openfir ...
- 读书时间《JavaScript高级程序设计》一:基础篇
第一次看了<JavaScript高级程序设计>第二版,那时见到手上的书,第一感觉真是好厚的一本书啊.现在再次回顾一下,看的是<JavaScript高级程序设计>第三版,并记录一 ...
- ios save image to album
- (void)savePhotoToAlbum { ZoomScrollView *zoomScrollView = (ZoomScrollView*)[self.scrollView viewWi ...
- python基础课程_学习笔记13:标准库:有些收藏夹——sys
标准库:有些收藏夹 sys sys这个模块可以让你访问和python解释器联系紧密的变量和函数. sys模块中一些重要的函数和变量 函数/变量 描写叙述 argv 命令行參数,包含脚本名称 exit( ...
- 013实现使用两个堆栈队列(keep it up)
实现使用两个堆栈队列 FIFO队列是一种数据结构(FIFO),后堆叠前进出的数据结构的(FILO). 两个栈实现的最简单的方法就是排队:队列中的第一个推栈, 队列将数据顺序的第一个堆栈推入第二堆叠 ...
- IOS开发——Protocol使用协议
protocol ['prəutəkɔl] (样例:http://blog.sina.com.cn/s/blog_6aafe9c90100yozz.html ) 一.说明 两个类进行通讯,用协议就比 ...
- Linux内核分析(一)---linux体系简介|内核源码简介|内核配置编译安装
原文:Linux内核分析(一)---linux体系简介|内核源码简介|内核配置编译安装 Linux内核分析(一) 从本篇博文开始我将对linux内核进行学习和分析,整个过程必将十分艰辛,但我会坚持到底 ...
- cocos2d-x教程2:在windows下怎样批量转换pvr,ccz为png或jpg
这是一个非经常见的功能,可是找了全网,竟然找不到,于是借鉴别人的批处理文件,改了下,就能够把整个文件夹的所有一次批量转换. 将这个bat文件暂定为,myConvert.bat,运行时就把这个bat文件 ...
- NPOI以及在ASP.NET MVC中的使用
NPOI以及在ASP.NET MVC中的使用 1.前言 相信大家在工作中经常要遇到一些导入导出Execl操作.学习贵在分享,分享使人快乐,园子里的前辈已经有很多好的文章,鄙人也是能力有限,在这里把这些 ...