Party at Hali-Bula

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1365    Accepted Submission(s): 454

Problem 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
 
题意:有个人想请客,但是由于要请的人之间存在上司和下属的关系,为了能避免下属和直接上司一起,寻找一个合理的方案。
         求最大的请客人数,上司和下属满足一棵树。
 
思路:如何处理字符串,转化成一棵树? 由于根节点已经知道,所以通过统计i点有多少个子节点就可以了。
        通过搜索来求取,关键是状态转移怎么写。
   dp[ i ] [ 0 ] 代表不包含第 i 个节点时的 最大人数目。
   dp[ i ] [ 1 ] 代表   包含第 i 个节点时到 最大人数目。
        那么对于叶子节点
        dp[ i ] [ 0 ]=0; dp[ i ] [ 1]=1;
        对于非叶子节点,
        dp[ i ] [ 0 ]= sum{   Max(dp[ j ] [ 0 ], dp[ j ] [ 1 ]  }; 其中dp[ j ] [ 0 ]的 j 是 i 的子节点。
        dp[ i ] [ 1 ]= sum{   dp[ j ] [ 0 ] } + 1 ;
  //画图试一试就知道了。
 
        对于统计是否唯一,另设一个数组ndp[ i ] [ j ]
    对于叶子节点:
          ndp[ i ] [ 0 ]=1; 代表不包含第 i 点的时候,全部子节点的种类是 唯一 的。
          ndp[ i ] [ 0 ]=0; 代表不包含第 i 点的时候,全部子节的的种类是  不唯一的。
 
          ndp[ i ] [ 1 ]=0; 代表  包含第  i 点的时候,全部子节点的种类是  不唯一。
          ndp[ i ] [ 1 ]=1;反之。
 
        非叶子节点:
    1.( dp[ k ] [ 0 ]> dp[ k ] [ 1 ] && ndp[ k ][ 0 ]==0)
        2.( dp[ k ] [ 1 ]> dp[ k ] [ 0 ] && ndp[ k ][ 1 ]==0)
        3.dp[ k ] [ 1 ] = = dp[ k ] [ 0 ];
       1或2或3满足 ,则 ndp[ k ] [ 0 ]= 0; why???要看看dp[ K ] [ 0 ];就知道了。
 
        如果存在子节点 满足 ndp[ j ] [ 0 ]==0 则 ndp[ k ] [ 0 ]=0; //j代表是 K 的子节点。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std; struct node
{
int next[];
int num;
}f[];
char cur[][];
int len;
bool glag,flag;
int dp[][];
int ndp[][]; int Max(int x,int y)
{
return x>y? x:y;
} int serch(char a[])
{
int i;
for(i=;i<=len;i++)
{
if(strcmp(a,cur[i])==)
return i;
}
strcpy(cur[++len],a);
return len;
} void dfs(int k)
{
int i,q,cur=;
if(f[k].num==)
{
dp[k][]=;
dp[k][]=;
ndp[k][]=;
ndp[k][]=;
return;
}
for(i=;i<=f[k].num;i++)
{
q=f[k].next[i];
dfs(q);
cur=Max(dp[q][],dp[q][]);
dp[k][]+=cur;
dp[k][]+=dp[q][]; if(dp[q][]>dp[q][]&&ndp[q][]==)
ndp[k][]=;
if(dp[q][]>dp[q][]&&ndp[q][]==)
ndp[k][]=;
if(dp[q][]==dp[q][]) ndp[k][]=; if(ndp[q][]==) ndp[k][]=;
}
dp[k][]++;
if(ndp[k][]==-) ndp[k][]=;
if(ndp[k][]==-) ndp[k][]=;
} int main()
{
int i,n,ans1,ans2;
char a[],b[];
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<=;i++) f[i].num=;
scanf("%s",cur[]);
len=;
for(i=;i<n;i++)
{
scanf("%s%s",a,b);
ans1=serch(a);
ans2=serch(b); f[ans2].num++;
f[ans2].next[f[ans2].num]=ans1; }
memset(dp,,sizeof(dp));
memset(ndp,-,sizeof(ndp));
glag=false;flag=false;
dfs(); if(dp[][]>dp[][]&&ndp[][]==)
printf("%d Yes\n",dp[][]);
else if(dp[][]>dp[][]&&ndp[][]==)
printf("%d Yes\n",dp[][]);
else printf("%d No\n",Max(dp[][],dp[][]));
}
return ;
}

hdu 2412 Party at Hali-Bula 经典树形DP的更多相关文章

  1. HDU 1561 The more, The Better 经典树形DP

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  3. hdu 5909 Tree Cutting——点分治(树形DP转为序列DP)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治的话,每次要做一次树形DP:但时间应该是 siz*m2 的.可以用 FWT 变成 siz*ml ...

  4. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  5. bzoj 2159 Crash 的文明世界 && hdu 4625 JZPTREE ——第二类斯特林数+树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 学习材料:https://blog.csdn.net/litble/article/d ...

  6. bzoj 2159 Crash 的文明世界 & hdu 4625 JZPTREE —— 第二类斯特林数+树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2159 使用公式:\( n^{k} = \sum\limits_{i=0}^{k} S(k,i ...

  7. HDU 4003 Find Metal Mineral(分组背包+树形DP)

    题目链接 很棒的一个树形DP.学的太渣了. #include <cstdio> #include <string> #include <cstring> #incl ...

  8. 【HDU - 4340】Capturing a country(树形DP)

    BUPT2017 wintertraining(15) #8A 题意 n(<100)个城市组成的树.A攻击i城市需要a[i]代价,B需要b[i].如果一个城市的邻居被A攻击了,那么A攻击它只要A ...

  9. hdu 3660 Alice and Bob's Trip(树形DP)

    Alice and Bob's Trip Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  10. HDU 1561 The more, The Better【树形DP/有依赖的分组背包】

    ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先 ...

随机推荐

  1. Python 魔法方法查询表 -- 总结篇

    据说,Python 的对象天生拥有一些神奇的方法,它们总被双下划线所包围,他们是面向对象的 Python 的一切. 他们是可以给你的类增加魔力的特殊方法,如果你的对象实现(重载)了这些方法中的某一个, ...

  2. 二手前端入门React项目

    个人对ReactJS这门技术比较感兴趣,在基友的帮助下成功创建了一个React标准前端工程,过程中遇到了不少麻烦,今天作为笔记一般记录一下遇到的问题和解决方案. 基础环境 手头一台Mac 使用OSX系 ...

  3. 总结day04 ---- 列表的切片,增删改查,以及,相关方法, 元祖的使用方法

    内容大纲 1 : 列表的索引 : 列表的切片 2 : 列表的增加内容 >1:append(char)  >2:insert(index,char) >3:extend('可迭代对象' ...

  4. vue二级路由跳转后外部引入js失效问题解决方案

    vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...

  5. Linux之virtualbox中的ubuntu虚拟机linux系统共享文件夹

    windows通过virtualbox软件与linux系统机型文件共享 1.第一步 在设置中找到共享文件夹选项,选择添加共享文件夹 2.第二步 选择需要与linux进行共享的文件夹,并选择固定分配 3 ...

  6. 开源单点登录系统CAS入门

    一.什么是CAS CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以 ...

  7. java无符号Byte

    1.无符号byte, 实现了将byte(-128~127) 转换为 (0~255) class UnsignedByte { private short value; private byte raw ...

  8. 分享一个电子发票信息提取工具(Python)

    电子发票太多,想统计下总额异常困难,网上工具不好用,花了2个小时实现一份,测试过中石油.京东开具的电子发票还行,部分发票名称失败有问题不影响统计,有需要的小伙伴自己拿去改吧. import cmd i ...

  9. 用 diff 比较两个 hdfs 文件内容

    diff <(hadoop fs -cat /path/to/file) <(hadoop fs -cat /path/to/file2)

  10. elastic-job动态添加定时任务

    在elastic-job的使用过程中,我们会遇到动态添加定时任务的时候,但是官网上面并没有对这块内容进行说明.按照我的理解以及官网上面elastic-job的框架图,ej的定时任务其实是存储在zook ...