您的位置:首页 > 其它

树形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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  树形DP POJ 3342