POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)

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

Http

POJ:https://vjudge.net/problem/POJ-3342

HDU:https://vjudge.net/problem/HDU-2412

UVAlive:https://vjudge.net/problem/UVALive-3794

UVA:https://vjudge.net/problem/UVA-1220

Source

树形动态规划

题目大意

在一棵树上选取若干个点,使得没有任意两个点相邻且点权值和最大,并问最大的方案数是否唯一。

解决思路

这题和POJ2342其实是差不多的,只是增加了一个要判断是否唯一。那么关于本题的基本框架请到这里来看,本文着重讲一下如何判断唯一性。

我们知道对于一个点i和其儿子节点j来说,F[i][0]=sum(max(F[j][1],F[j][0])) ,F[i][1]=sum(F[j][0])+Value[i](上题已经说过啦),那么我们只要再加一个bool数组来存放方案是否唯一就可以啦。

我们定义Unique[i]为判断是否唯一的数组,0表示不唯一,1表示唯一;Unique[i][0]表示不选i时的唯一性,Unique[i][1]表示选i时的唯一性。

首先来看Unique[i][1]。我们知道F[i][1]=sum(F[j][0]),也就是说i只要有任意一颗子树不唯一,i就不唯一,所以有if (Unique[j][0]==0) Unique[i][1]=0;

再来看Unique[i][0]。因为F[i][0]=sum(max(F[j][0],F[j][1])),所以Unique[i][0]应该与F[j][0],F[j][1]中大的的Unique有关。

最后要注意的是,F[j][0]是可能为0的,也就是说没有选节点(这在叶子结点的上一层节点会出现),这个时候对于Unique[j][0]的讨论是无意义的(没有点怎么会有不同的方案呢),所以要跳过(楼主在这里WA了好多次)。更多细节请参看代码

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std; const int maxN=300;
const int inf=2147483647; int n;
int F[maxN][5];
vector<int> E[maxN];
bool Unique[maxN][5];
map<string,int> Map;//因为输入的是字符串,所以我们用一个Map将其转换成数字编号 void dfs(int u); int main()
{
while (cin>>n)
{
if (n==0)
break;
memset(F,0,sizeof(F));
memset(Unique,0,sizeof(Unique));
for (int i=1;i<=n;i++)
E[i].clear();
Map.clear();
string u,v;
int cnt=0; cin>>u;
cnt++;
Map[u]=cnt;
int Boss=cnt; for (int i=1;i<n;i++)//输入字符串并分配编号
{
cin>>u>>v;
if (Map[u]==0)
Map[u]=++cnt;
if (Map[v]==0)
Map[v]=++cnt;
E[Map[v]].push_back(Map[u]);
//In[Map[u]]++;
} dfs(Boss); bool ok;
cout<<max(F[Boss][1],F[Boss][0])<<' ';
if (F[Boss][1]==F[Boss][0])
ok=0;
else
if (F[Boss][1]>F[Boss][0])
ok=Unique[Boss][1];
else
ok=Unique[Boss][0];
if (ok)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
/*for (int i=1;i<=n;i++)
cout<<F[i][0]<<' '<<F[i][1]<<endl;
cout<<endl;
for (int i=1;i<=n;i++)
cout<<Unique[i][0]<<' '<<Unique[i][1]<<endl;*/
}
return 0;
} void dfs(int u)
{
Unique[u][1]=Unique[u][0]=1;//唯一性先置为唯一
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i];
dfs(v); if ((Unique[v][0]==0)&&(F[v][0]!=0))//注意这里一定要有F[v][0]!=0这一句,因为若不加的话那些没有选择任何节点的方案也会统计进去,造成出错
Unique[u][1]=0;
F[u][1]+=F[v][0]; //F[u][0]+=max(F[v][0],F[v][1]);
if (F[v][0]>F[v][1])
{
F[u][0]+=F[v][0];
if (Unique[v][0]==0)
Unique[u][0]=0;
}
else
if (F[v][0]<F[v][1])
{
F[u][0]+=F[v][1];
if (Unique[v][1]==0)
Unique[u][0]=0;
}
else if (F[v]!=0)//这里的F[v]!=0也必须有!否则会出现和上面一样的情况
{
F[u][0]+=F[v][0];
Unique[u][0]=0;
}
}
F[u][1]++;
return;
}

POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)的更多相关文章

  1. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  2. POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)

    POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...

  3. POJ 2152 fire / SCU 2977 fire(树型动态规划)

    POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...

  4. POJ 3398 Perfect Service(树型动态规划,最小支配集)

    POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...

  5. POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心)-动态规划做法

    POJ 3659 Cell Phone Network / HUST 1036 Cell Phone Network(最小支配集,树型动态规划,贪心) Description Farmer John ...

  6. hdu 2412 Party at Hali-Bula【树形dp】

    HDU 2412 和poj 2342(hdu 1520)差不多,多了一个判断最优解是(Yes)否(No)唯一.关键问题也在这个判断最优解是否唯一上. 先定义dp[u][2],表示选(dp[][1])或 ...

  7. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  8. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)

    POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...

  9. 【POJ 3140】 Contestants Division(树型dp)

    id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS   Memory Limit: 65536K Tot ...

随机推荐

  1. 支付宝支付-常用支付API详解(查询、退款、提现等)

    所有的接口支持沙盒环境的测试 1.前言 前面几篇文件详细介绍了 支付宝提现.扫码支付.条码支付.Wap支付.App支付 支付宝支付-提现到个人支付宝 支付宝支付-扫码支付 支付宝支付-刷卡支付(条码支 ...

  2. 全景智慧城市常诚——没接触过VR全景的你就是目前VR最大的新闻

    据调查,自2015年开始,VR(虚拟现实)技术在传媒行业中的应用呈现井喷式增长,各大国际主流媒体纷纷在新闻报道中使用VR技术.国内运用VR报道新闻最早在2015年12月,财新网利用VR技术对深圳山体垮 ...

  3. 【webpack】webpack-dev-server生猛上手——让我们来搭一个webpack的微服务器吧!

      [前言]:因为最近在搞百度地图API的时候到了webpack的externals,才发现我之前都只是用webpack做一些搭建完项目后的"收尾工作"--即打包,而没有把它纳入到 ...

  4. 2015阿里巴巴安全峰会PPT

    有幸参加了阿里巴巴安全峰会,不得不佩服阿里巴巴神盾局,真牛B!然后亲眼目睹了第二天的各大厂商牛(zhuang)B人才上台演讲,有被捧的,有被喷的,呵呵.总的来说,大家的分享精神还是阔以的. 下面是会议 ...

  5. Grunt压缩HTML和CSS

    我的小伙伴们!我明明 在压缩图片之前发过一篇,关于Grunt压缩cCSS是和HTML的!但是不知道为什么,今天再一看.迷之消失了! 没办法.只好今天在写一次,从头开始!首先.我来介绍一下为什么要用构建 ...

  6. datatables 学习笔记1 基础篇

    本文共3部分:基本使用|遇到的问题|属性表 1.DataTables的默认配置 $(document).ready(function() { $('#example').dataTable(); } ...

  7. rest api get 查询接口 多个参数

    查询时,使用get,传递参数至服务器. angular js中,$http可以直接传递object,在get中,params:data 在服务端, query(x=x,y=y)可写成 query(** ...

  8. Vin码识别(车架号识别)技术,摆脱手动录入提高工作效率

    本文主题:Vin码识别(车架号识别)技术,摆脱手动录入提高工作效率 本文关键词:Vin码识别,汽车Vin码识别,车架号识别,汽车车架号识别,车代码识别,车代号识别 本文主旨:一.Vin码(车架号)在什 ...

  9. 愚公oracle数据库同步工具

    最近,利用一些时间对oracle数据库实时同步工具做了一些调研分析,主要关注了linkedin的databus和阿里的yugong两个中间件,其中databus需要在每个待同步的表上增加额外的列和触发 ...

  10. PHP基础入门(四)---PHP数组实用基础知识

    PHP数组 数组是特殊的变量,它可以同时保存一个以上的值. ***关键词:数组基础.数组遍历.超全局数组.数组功能.数组函数. 下面来和大家分享一下有关PHP的数组基础知识,希望对你PHP的学习有所帮 ...