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 ...
随机推荐
- C++学习笔记1--基础知识
#include <iomanip> setpresition(int n); 设置输出精度浮点数是n. [goto声明] goto也被称为无条件分支语句购买勇于改变运行顺序的声明.got ...
- XML数组和对象,反之亦然
惊人的互相转换,还是因为麻烦.程序很反感麻烦猿 1 阵转xml <?php /* 一维数组转xml 思路: 循环数组每一个单元,添加到xml文档节点中去 */ /* $arr = array( ...
- 设计模式 State模式 机器的情况下用自己的主动性
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/26350617 状态模型给我眼前一亮的感觉啊,值得学习~ 看看定义:改变一个对象的 ...
- HTTP响应代码
HTTP响应代码 1xx - 消息通知 这些状态代码表示临时响应.client在收到常规响应.应准备接收一个或多个 1xx 应. · 100 - Continue 初始的请求已经接受,客户应当继续发送 ...
- 无法Debug SQL: Unable to start T-SQL Debugging. Could not attach to SQL Server process on
今天SSMS debug SQL当脚本,突然错误: Unable to start T-SQL Debugging. Could not attach to SQL Server process on ...
- C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)
C++有时模板很可能无法处理某些类型的. 例如: #include <iostream> using namespace std; class man{ private: string n ...
- NSIS文字及字符串函数与头文件介绍
原文 NSIS文字及字符串函数与头文件介绍 文字函数,顾名思义就是处理字符串的函数.使用这些字符串函数前,必须先包含头文件WordFunc.nsh.该头文件目前包含如下一些函数:WordFind.Wo ...
- Android Studio常见报错及处理办法
在Android Studio上点了update,系统自动升级,自动重启Android Studio后,以前的项目Gradle正常编译: Unable to start the daemon proc ...
- SQLServer-----SQLServer 2008 R2卸载
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- mariadb 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost /]# systemctl stop mariadb.service[root@localhost /]# mysqld_safe --user=mysql --ski ...