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入门题,前半部分非常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的更多相关文章

  1. 【poj3342】 Party at Hali-Bula

    http://poj.org/problem?id=3342 (题目链接) 题意 给出一棵树,要求在不存在两个节点相邻的条件下,选出尽可能多的节点,并且判断是否有多种选法. Solution 很水的树 ...

  2. poj3342 Party at Hali-Bula

    树形dp题,状态转移方程应该很好推,但一定要细心. http://poj.org/problem?id=3342 #include <cstdio> #include <cstrin ...

  3. 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代表不唯一. ...

  4. poj 3680 Intervals

    给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...

  5. jQuery 遍历 - parent() 方法

    ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法  parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...

  6. (转)TCP注册端口号大全

    分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...

  7. 【转】CString类型互转 int

    CString类型互转 int 原文网址:http://www.cnitblog.com/Hali/archive/2009/06/25/59632.html CString类型的转换成int  将字 ...

  8. 阿里云ECS被攻击

    今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...

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

随机推荐

  1. all about AIX MPIO

    Multipath  I/O (多路径)   在计算机存储技术里,多路径提供了容错和性能提高,在计算机系统里CPU有多条物理路径通道,块存储设备通过总线,控制器,交换设备以及桥接设备来连接.     ...

  2. w3wp占用CPU过高

    w3wp占用CPU过高 在此之前项目有发生过两次类似的状况,都得以解决,但最近又会发现偶尔CPU会跑满,虽然之前使用过WinDbg解决过两次问题但人的记忆是不可靠的,今天处理同样问题的时候还是遇到了一 ...

  3. Cocos2d-x 3.0 Lua编程 之 响应物理引擎的Contact事件回调不运行的问题

    在较早的版本号如3.0beta使用例如以下代码的话: -- add ground local groudNode = cc.Node:create() groudNode:setPhysicsBody ...

  4. 网络爬虫WebCrawler(1)-Http网页内容抓取

    在windows在下面C++由Http协议抓取网页的内容: 首先介绍了两个重要的包(平时linux在开源包,在windows下一个被称为动态链接库dll):curl包和pthreads_dll,其中c ...

  5. Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 3

    DDL Setup Steps SQL> grant execute on utl_file to ggs; Grant succeeded. Create GLOBALS file [orac ...

  6. Mesos-error

    1,configure: error: cannot find libcurl 解决 yum install  curl-devel 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  7. POJ--2289--Jamie&#39;s Contact Groups【二分图的多个匹配+二分法答案】

    链接:id=2289">http://poj.org/problem?id=2289 意甲冠军:有n个人,m个分组,每一个人能够分配到一些组别.问怎样分能使得人数最多的组别人数最少. ...

  8. DDD领域驱动设计仓储Repository

    DDD领域驱动设计初探(二):仓储Repository(上) 前言:上篇介绍了DDD设计Demo里面的聚合划分以及实体和聚合根的设计,这章继续来说说DDD里面最具争议的话题之一的仓储Repositor ...

  9. POJ2299 Ultra-QuickSort 【树阵】+【hash】

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39529   Accepted: 14250 ...

  10. ResourceBundle

    ResourceBundle本质上也是一个映射,但是它提供了国际化的功能.  假设电脑设置的地区是中国大陆,语言是中文  那么你向ResourceBundle(资源约束名称为base)获取abc变量的 ...