[poj 3342]Party at Hali-Bula 树形dp

POJ3342 Party at Hali-Bula 树形DP 这题是问聚会时老板与员公不能同时出现的方案ov 阅读详情

题目链接http://poj.org/problem?id=3342
Party at Hali-Bula

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5997 Accepted: 2140

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

题意:BB将要邀请BCM的员工参加一个party来庆祝他的退休。还是每个人不能同其直接boss一起参加。要求使参加人数最多,并要求判断方案是否唯一。

思路
计算最大的很简单dp[i][0]=求和max(dp[soni][0],dp[soni][1]); dp[i][1]=求和dp[soni][0];

判断开始只傻逼的判dp[1][0]=?dp[1][1];
应用flag[i][0]判断,如果选的子树方案不唯一,该方案不唯一;初始当dp[soni][0]==dp[soni][1]时flag【i】[0]=1;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
using namespace std;
int n;
int tot;
vector<int> lin[205];
map<string,int> q;
string s,s1;
int dp[201][2];
int flag[201][2];
void dfs(int x)
{
    int ret=0;
    int tmp=0;
    int f1=0;
    int f2=0;
    for(int i=0;i<lin[x].size();i++)
    {
        int v=lin[x][i];
        dfs(v); 
        ret+=max(dp[v][0],dp[v][1]);

        if(dp[v][0]==dp[v][1]) f1=1;
        else if(dp[v][0]>dp[v][1]&&flag[v][0]==1) f1=1;
        else if(dp[v][1]>dp[v][0]&&flag[v][1]==1) f1=1;

        tmp+=dp[v][0];
        if(flag[v][0]) f2=1;
    }


    dp[x][0]=ret;
    dp[x][1]=tmp+1;
    if(f2) flag[x][1]=1; 
    if(f1) flag[x][0]=1;
}
int main()
{
    while(scanf("%d",&n))
    {
        if(!n) return 0;
        tot=0;
        cin>>s;
        q.clear();
        q[s]=++tot;
        for(int i=1;i<=n;i++)
        {
            lin[i].clear();
        }
        for(int i=1;i<n;i++)
        {
            cin>>s>>s1;
            if(!q[s])
            {
                q[s]=++tot;
            }
            if(!q[s1])
            {
                q[s1]=++tot;
            }
            lin[q[s1]].push_back(q[s]);
        }
        memset(flag,0,sizeof(flag));
        memset(dp,0,sizeof(dp));
        dfs(1);


        if(dp[1][0]==dp[1][1])
        {
            printf("%d No\n",dp[1][0]);
        }
        else if(dp[1][0]>dp[1][1]&&flag[1][0])
        {
            printf("%d No\n",dp[1][0]);
        }
        else if(dp[1][1]>dp[1][0]&&flag[1][1])
        {
            printf("%d No\n",dp[1][1]);
        }
        else printf("%d Yes\n",max(dp[1][0],dp[1][1]));
    }
}
POJ 3342 Party at Hali-Bula树形DP 题目链接 相比POJ 2342那题,增加了一问:最佳选择是否唯一的. 首先可根据雇员与老板的关系建立一颗树,老板做根节点,雇员是该根节点的儿子。  如上面这棵简易的树,对于节点A有两种决策:  选与不选。  若选择A,则B 、C都不能选,问题转换为求所有以 “B、C的儿子为根节点的树” 子问题最优解之和。  若不选A, 则问题转换为求“以B、C为根节点"的子树的最优解之和。 阅读详情

相关推荐

poj3342Party at Hali-Bula树形dp

1 /* 2 树形dp! 3 判重思路: 4 当dp[v][0]==dp[v][1]时,很自然,flag[u][0]必然是有两种方案的。flag[u][1]则不然, 5 因为它只和dp[v][0]有关系。而若flag[v][0]不唯一时,则必然flag[u][1]也不唯一 6 也就是u的子节点有dp[v][1]==dp[v][0](...

aipiannian6725的博客 152

POJ - 3342 Party at Hali-Bula树形dp

题目链接:点击这里 题目大意: 题目分析: 出现不同的解的情况是,先进行遍历所有节点,如果出现 dp[i][0]dp[i][0]dp[i][0] 和 dp[i][1]dp[i][1]dp[i][1] 相等的情况,对于它的子节点,如果出现来与不来人数都一样的情况,就说明无论该子节点的父节点是否受该子节点的父节点的父节点的影响,总能出现由于该子节点的来与不来而出现的情况 ...

羊驼的博客 191

POJ 3342 Party at Hali-Bula(树形DP)

题目链接:点击打开链接 题意:给定一个树,选择若干节点,使得选择的结点中任一结点不会和它的子结点同时被选择,求能选节点的最大数量,并且统计选择方案是否唯一。之前做过这个题,只不过没加入唯一性判断。 唯一性判断的方法也基于动态规划思想,开个二维数组,flag[v][2],用于标记选择或者不选择当前节点时,最大数量方法的唯一性,唯一性由后继节点决定。 对于flag[fa][1],由于dp[fa]

不忘初心 422

POJ - 3342 Party at Hali-Bula 树形dp入门

原题链接:http://poj.org/problem?id=3342 目录题意分析code 题意 和没有上司的舞会题意基本一致,多了一个判断答案是否唯一 分析 设两个状态设两个状态设两个状态 dp[x][1/0]表示取或不取当前节点获得的最大值dp[x][1/0] 表示取或不取当前节点获得的最大值dp[x][1/0]表示取或不取当前节点获得的最大值 f[x][1/0]表示取或不取当前节点答案是否唯一f[x][1/0]表示取或不取当前节点答案是否唯一f[x][1/0]表示取或不取当前节点答案是否唯一 状态转

kaka03200的博客 222

POJ - 3342 Party at Hali-Bula(树形dp)

题目链接:点击查看 题目大意:n个人参加聚会,每个人都不想和老板一起参加,问最多可以有多少个人参加,并且判断方案唯一性 题目分析:这个类型的题目这已经是第三个了,状态转移方程都一模一样,不过这个题有点不同的地方是需要判断唯一性,我看到网上有两种方法,一种是通过状态转移的过程中如果发现dp[v][1]==dp[v][0]就设置一个flag为false,即在这个地方就出现了分歧,因为想起来和写起来...

Falcon的博客 481

POJ 3342 Party at Hali-Bula 树形DP 最大独立集

题意:一个公司想举办聚会,但是为了气氛,如果邀请一个员工的话,是不会再邀请他的直接上司。这个公司的人员结构是树形的,每个人至多有一名直接上司。给出这个公司的人员结构,求出能参加聚会的人数的最大值,同时判断方案是否唯一。 思路:同样的树的最大独立集,不同的是还需要判断方案是否唯一。其实,如果我们求出了方案数,就可以知道方案是否是唯一的。 题目中是给出的每个人的名字,直接用map离散一下就可以得到数

leodestiny 682

POJ - 3342 Party at Hali-Bula树形DP+STL)

POJ - 3342 Party at Hali-Bula树形DP+STL) 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 h...

weixin_43179892的博客 234

POJ-3342 Party at Hali-Bula(树形dp+判断结果是否唯一)

来,一起去POJ参加你的专属AC party Party at Hali-Bula Time Limit:2000MS Memory Limit:65536K Total Submissions:7184 Accepted:2531 Description Dear Contestant, I'm going to have a party ...

From now on...的Blogs 221

poj 3342 Party at Hali-Bula(树形dp)

Party at Hali-Bula Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6320   Accepted: 2254 Description Dear Contestant, I'm going to have a party at my vill

saber_acher的博客 316

poj 3342 Party at Hali-Bula 树形dp

poj 3342 Party at Hali-Bula http://poj.org/problem?id=3342 题意:给定一些点,这些点最多只有一个父节点,且只有一个点没有父节点,要选择一个集合使点数最多,且不能同时选择子节点与其对应的父节点   树形dp状态转移很好写,只是中间的选择是否唯一纠结了一下,见注释   另外忘记了些mp.clear()。。。。wa了好多次。。。。以后

402

POJ 3342- Party at Hali-Bula (树形dp+判断是否唯一)

题目: 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 wh...

weixin_44757834的博客 290

POJ3342 Party at Hali-Bula(树形DP)

题目大意: 有n个人要一起聚会,规定直接上司不能和他的直接下属一起出席。问最多能去几人是唯一解吗? 题目思路:树形DP+链试前向星:#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> int to1[210],begin1[210],next1[210],d[210][2],u[210][2],e; ch

lily_cnyali的博客 420

poj-3342 Party at Hali-Bula (树形dp入门)

初次接触树形dp。。。。。

_ispecter_的博客 126

poj3342 Party at Hali-Bula树形dp)(判断唯一性)

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

Ellery的book 211

POJ - 3342 Party at Hali-Bula 树形DP

题目大意:某公司有个聚会,要邀请员工来参加。要求员工和他的直系上司不能同时到这个聚会,问最多能邀请到多少人,有多种邀请方法时输出No解题思路:用dp[i][1]表示邀请第i个人,dp[i][0]表示没有邀请第i个人 初始dp[i][1] = 1, dp[i][0] = 0 状态转移方程为 dp[i][1] = sum(dp[son][0]) son为i的下属 dp[i][0] = sum(m

静静地码 633

Party at Hali-Bula - POJ 3342 树形dp

Party at Hali-Bula Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5200   Accepted: 1850 Description Dear Contestant, I'm going to have a party at my vill

提比的强行AC 588

树形DPPOJ3342-Party at Hali-Bula

参考:http://blog.csdn.net/sdjzping/article/details/13131611 第一次接触树形DP的题目,这种题解题思路还挺固定的,深度优先搜索+动态规划。这点比较好理解,dp[i][j]保存节点i在状态j下的最多参加宴会人数,状态j其实就两种状态,也就是只有2种取值,0代表不参加,1代表参加。 首先赋初值,dp[i][0]=0,就结点i一人来说,不参加,人

俊哥有个Blog 484
上一篇: 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 搜索
下一篇: [poj 1201]Intervals 差分约束
ALPS233
博客等级 码龄11年 25粉丝 191原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值