树形DP_____Party at Hali-Bula( POJ 3342 )
2016-08-21 02:08
337 查看
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
Sample Output
题意:
n个点构成的一棵树,要求选取其中一些点并且保证无任意两点是父子关系。问最多可以选多少个点?并且问有没有多个解?
分析:
dp[ i ] [ 0 ] 表示第 i 个点选取后,以 i 为子树对原问题求解的值。
dp[ i ] [ 1 ] 表示第 i 个点选取后,以 i 为子树对原问题求解的值。
则状态转换:
dp [ i ] [ 0 ] += max( dp [ son ] [ 1 ] , dp [ son ] [ 0 ] );
dp [ i ] [ 1 ] += dp [ son ] [ 0 ]
此时能够求得最优解,但是如何求是否有多解呢?
令num[ i ] [ 0 ] 表示 dp [ i ] [ 0 ] 对应最优解是否有多解 如果为 1 表示有多解,否则无多解。
那么对于
dp [ i ] [ 1 ] += dp[ son ] [ 0 ] 如果 num [ son ] [ 0 ] = 1 有多解,那么dp [ i ] [ 1 ] 一定有多解。
对于
dp [ i ] [ 0 ] += max( dp [ son ] [ 1 ] , dp [ son ] [ 0 ] ); 当dp [ son ] [ 1 ] , dp [ son ] [ 0 ] 一样时 dp[ i ] [ 0 ] 一定有多解,否则同上判断。
注意一下:
最后判断多解的时候不仅仅要判断dp [ 1 ] [ 1 ] ,dp [ 1 ] [ 0 ] 是否有多解,还要判断dp [ 1 ] [ 1] 和 dp [ 1 ] [ 0 ]是否一样,一样也是多解。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int inf = 999999999;
int n;
map<string,int>mpt;
vector<int>edge[220];
int num[220][2];
int fa[220];
int dp[220][2];
void dfs(int now)
{
dp[now][1] = 1;
dp[now][0] = 0;
for(int i = 0 ; i < edge[now].size() ; i ++)
dfs(edge[now][i]);
for(int i = 0 ; i < edge[now].size() ;i ++)
{
if(num[edge[now][i]][0] == 1) num[now][1] = 1;
dp[now][1] += dp[edge[now][i]][0];
if(dp[edge[now][i]][0] == dp[edge[now][i]][1])
{
dp[now][0] += dp[edge[now][i]][0];
num[now][0] = 1;
}
else if(dp[edge[now][i]][0] > dp[edge[now][i]][1])
{
dp[now][0] += dp[edge[now][i]][0];
if(num[edge[now][i]][0] == 1) num[now][0] = 1;
}
else
{
dp[now][0] += dp[edge[now][i]][1];
if(num[edge[now][i]][1] == 1) num[now][1] = 1;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n == 0)break;
for(int i = 1 ; i <= n ; i ++)
edge[i].clear();
memset(fa,0,sizeof(fa));
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
mpt.clear();
int tot = 0;
char now[110],next[110];
for(int i = 1 ; i <= n ; i ++)
{
scanf("%s",now);
if(mpt.find(now) == mpt.end())
mpt[now] = ++tot;
while(getchar()!='\n')
{
scanf("%s",next);
if(mpt.find(next) == mpt.end())
mpt[next] = ++tot;
edge[mpt[next]].push_back(mpt[now]);
fa[mpt[next]] = 1;
}
}
dfs(1);
int ans,cnt;
ans = dp[1][0],cnt = num[1][0];
if(ans < dp[1][1])
{
ans = dp[1][1];
cnt = num[1][1];
}
if(cnt == 1 || dp[1][1] == dp[1][0])
printf("%d No\n",ans);
else
printf("%d Yes\n",ans);
}
return 0;
}
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
题意:
n个点构成的一棵树,要求选取其中一些点并且保证无任意两点是父子关系。问最多可以选多少个点?并且问有没有多个解?
分析:
dp[ i ] [ 0 ] 表示第 i 个点选取后,以 i 为子树对原问题求解的值。
dp[ i ] [ 1 ] 表示第 i 个点选取后,以 i 为子树对原问题求解的值。
则状态转换:
dp [ i ] [ 0 ] += max( dp [ son ] [ 1 ] , dp [ son ] [ 0 ] );
dp [ i ] [ 1 ] += dp [ son ] [ 0 ]
此时能够求得最优解,但是如何求是否有多解呢?
令num[ i ] [ 0 ] 表示 dp [ i ] [ 0 ] 对应最优解是否有多解 如果为 1 表示有多解,否则无多解。
那么对于
dp [ i ] [ 1 ] += dp[ son ] [ 0 ] 如果 num [ son ] [ 0 ] = 1 有多解,那么dp [ i ] [ 1 ] 一定有多解。
对于
dp [ i ] [ 0 ] += max( dp [ son ] [ 1 ] , dp [ son ] [ 0 ] ); 当dp [ son ] [ 1 ] , dp [ son ] [ 0 ] 一样时 dp[ i ] [ 0 ] 一定有多解,否则同上判断。
注意一下:
最后判断多解的时候不仅仅要判断dp [ 1 ] [ 1 ] ,dp [ 1 ] [ 0 ] 是否有多解,还要判断dp [ 1 ] [ 1] 和 dp [ 1 ] [ 0 ]是否一样,一样也是多解。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int inf = 999999999;
int n;
map<string,int>mpt;
vector<int>edge[220];
int num[220][2];
int fa[220];
int dp[220][2];
void dfs(int now)
{
dp[now][1] = 1;
dp[now][0] = 0;
for(int i = 0 ; i < edge[now].size() ; i ++)
dfs(edge[now][i]);
for(int i = 0 ; i < edge[now].size() ;i ++)
{
if(num[edge[now][i]][0] == 1) num[now][1] = 1;
dp[now][1] += dp[edge[now][i]][0];
if(dp[edge[now][i]][0] == dp[edge[now][i]][1])
{
dp[now][0] += dp[edge[now][i]][0];
num[now][0] = 1;
}
else if(dp[edge[now][i]][0] > dp[edge[now][i]][1])
{
dp[now][0] += dp[edge[now][i]][0];
if(num[edge[now][i]][0] == 1) num[now][0] = 1;
}
else
{
dp[now][0] += dp[edge[now][i]][1];
if(num[edge[now][i]][1] == 1) num[now][1] = 1;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n == 0)break;
for(int i = 1 ; i <= n ; i ++)
edge[i].clear();
memset(fa,0,sizeof(fa));
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
mpt.clear();
int tot = 0;
char now[110],next[110];
for(int i = 1 ; i <= n ; i ++)
{
scanf("%s",now);
if(mpt.find(now) == mpt.end())
mpt[now] = ++tot;
while(getchar()!='\n')
{
scanf("%s",next);
if(mpt.find(next) == mpt.end())
mpt[next] = ++tot;
edge[mpt[next]].push_back(mpt[now]);
fa[mpt[next]] = 1;
}
}
dfs(1);
int ans,cnt;
ans = dp[1][0],cnt = num[1][0];
if(ans < dp[1][1])
{
ans = dp[1][1];
cnt = num[1][1];
}
if(cnt == 1 || dp[1][1] == dp[1][0])
printf("%d No\n",ans);
else
printf("%d Yes\n",ans);
}
return 0;
}
相关文章推荐
- POJ 3342 - Party at Hali-Bula 树形DP+最优解唯一性判断
- poj 3342 Party at Hali-Bula(树形dp)
- POJ - 3342 Party at Hali-Bula 树形DP
- poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)
- [poj 3342]Party at Hali-Bula 树形dp
- Party at Hali-Bula - POJ 3342 树形dp
- poj 3342 Party at Hali-Bula(树形dp)
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- POJ - 3342 - Party at Hali-Bula (树形dp + 判断唯一解)
- POJ 3342 Party at Hali-Bula 树形DP 最大独立集
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- poj 3342 Party at Hali-Bula 树形dp
- POJ 3342 Party at Hali-Bula(树形DP)
- poj 3342 Party at Hali-Bula 树状dp
- pku 3342 Party at Hali Bula 树形dp 解题报告
- HDU 2412 / POJ 3342 Party at Hali-Bula(树形DP+判断多解)
- POJ 3342 Party at Hali-Bula / HDU 2412 / UVAlive 3794 / UVA 1220
- hdu 2412 Party at Hali-Bula(树形DP)
- 紫书动规 例题9-13 UVA - 1220 Party at Hali-Bula 树形dp
- UVA - 1220 Party at Hali-Bula 树形DP入门