您的位置:首页 > 其它

树形DP(1)-Hdu 2412 Party at Hali-Bula

2014-02-18 16:43 453 查看
2014 寒假第一章 - 树形DP

之前做的时候没来得及上传,今天整理一下,一并上传了。首先是这道题 Party at Hali-Bula,和hdu 1520 Anniversary party
还有 1054
strategic game 很相似,不过有所加强,那两道题在之前的博客里已经讲过了,可以先写一下那两道题,想比较一下几个题的异同的,可以点这里

下面来看这道题:

Party at Hali-Bula

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1605    Accepted Submission(s): 544


[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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.

 

[align=left]Sample Input[/align]

6
Jason
Jack Jason
Joe Jack
Jill Jason
John Jack
Jim Jill
2
Ming
Cho Ming
0

 

[align=left]Sample Output[/align]

4 Yes
1 No

 

[align=left]Source[/align]
2006 Asia Regional Tehran
 

题意: 给出n个人的直接上下级关系,形成一颗关系树,要求参加party时,下属不能和他的唯一直接上司一起去,问做多能去多少人?还有问 去最多人的 分配方式是否唯一。

分析:DP,用dp[i][0]表示 不选择i点,i点及其子树能选出的最大人数,dp[i][1]表示选择i点时,i点及其子树能选的最大人数。

状态转移方程:

1,叶子结点k:dp[k][0] = 0; dp[k][1] = 1;

2,非叶子节点i,:dp[i][1] = 1 + sigma(dp[j][0]);       dp[i][0] = sigma(max(dp[j][0],dp[j][1]))       ( j是i的儿子 )。

最多人数即 max(dp[0][1],dp[0][0]);

那么如何判断最优解是否唯一?

新加一个状态 uniq[i][2];表示在i点 取或不取是否唯一; 为1表示唯一,为0表示不唯一。

状态转移:

1.叶子结点:uniq[i][0] = uniq[1] = 1;

2:非叶子节点:

对于 i 的任一儿子 j :<1>.如果有uniq[j][0] == 0,则 uniq[i][1] = 0.

<2>如果 (dp[j][0] > dp[j][1] && uniq[j][0]==0)或(dp[j][0] < dp[j][1] && uniq[j][1]==0)或(dp[j][0] == dp[j][1])则dp[i][0] = 0.

代码:

#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define MAX 205
bool uniq[MAX][2];//用于判断是否是唯一解
int dp[MAX][2];
bool visit[MAX];
char name[MAX][105],name2[MAX][105];
vector<int> v[MAX];
int n;
void read()
{
for (int i = 0; i < n; i++)
{
dp[i][0] = 0;
dp[i][1] = 1;
uniq[i][0] = uniq[i][1] = 1;
visit[i] = 0;
v[i].clear();
}
char tmp[105] = {NULL};
scanf("%s",name[0]);
for (int i = 1; i < n; i++)
{
scanf("%s %s",name[i],name2[i]);
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(strcmp(name2[i],name[j])==0)
{
v[i].push_back(j);
v[j].push_back(i);
break;
}
}
}
}
void solve(int u)
{
visit[u] = 1;
for (int i = 0; i < v[u].size(); i++)
{
int m = v[u][i];
if(visit[m] == 0)
{
solve(m);
//
if(dp[m][0] > dp[m][1] && uniq[m][0] == 0)uniq[u][0]=0;
else if(dp[m][1] > dp[m][0] && uniq[m][1] == 0)uniq[u][0]=0;
else if(dp[m][0] == dp[m][1])uniq[u][0] = 0;

if(uniq[m][0] == 0)uniq[u][1] = 0;
//
dp[u][1] += dp[m][0];
dp[u][0] += max(dp[m][0],dp[m][1]);
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF && n!=0)
{
read();
solve(0);
printf("%d ",max(dp[0][0],dp[0][1]));
if((dp[0][0]>dp[0][1] && uniq[0][0]==1) ||(dp[0][1]>dp[0][0]&&uniq[0][1]==1))
{
puts("Yes");
}else puts("No");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  树形DP