您的位置:首页 > 其它

uva1220Party at Hali-Bula(树形dp)

2016-03-25 01:04 483 查看
题目:

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≤ \len≤ \le200) , 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

分析:

树的最大独立集问题,并判定唯一性

状态:b[u][0]表示以u为根的子树中,不选u点能得到的最大人数;d[u][0]为方案唯一性,0为唯一;

相应的有b[u][1],d[u][1]表示选u的情况.

b[u][1]:选u则子节点不选,b[u][1] = sum{ b[v][0] | v是u的子节点} 当且仅当d[v][0]全为0时d[u][1]才是0;

b[u][0]:因为u没有选,所以每个子结点v可以选也可以不选,即 b[u][0] = sum{ max(b[v][0], b[v][1]) }

如果某个地方b[v][0] == b[v][1] 则说明d[u][0]不唯一。另外,如果取到最大的那个值为1,则也不唯一;

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
char a[205][2][105];
int b[205][2],d[205][2];
vector<int> tree[205];
void dp(int n);

//b[u][0] 表示以u为树根的子树,不取u的最优解。
//b[u][1] 表示以u为树根的子树,取u的最优解。

//d[u][0] 表示以u为树根的子树,不取u时结果是否唯一。值为1时表示不唯一
//d[u][1] 表示以u为树根的子树,取u时结果是否唯一。值为1时表示不唯一
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
tree[i].clear();
scanf("%s",a[0][0]);
for(int i=1;i<n;i++)
scanf("%s%s",a[i][0],a[i][1]);
for(int i=1;i<n;i++)                        //此处注意对每个人找他的boss,如果对每个boss找他的下属则程序会错误
{
for(int j=0;j<n;j++)
if(strcmp(a[i][1],a[j][0])==0)
{
tree[j].push_back(i);
break;
}
}
memset(d,0,sizeof(d));
memset(b,0,sizeof(b));

dp(0);
if(b[0][0]>b[0][1])
{
printf("%d ",b[0][0]);
if(d[0][0])
printf("No\n");
else
printf("Yes\n");
}
else if(b[0][0]<b[0][1])
{
printf("%d ",b[0][1]);
if(d[0][1])
printf("No\n");
else
printf("Yes\n");
}
else
printf("%d No\n",b[0][0]);
}
}
void dp(int u)
{
int num=tree[u].size();
b[u][1]=1,b[u][0]=0;
for(int i=0;i<num;i++)
{
int v=tree[u][i];
dp(v);
if(b[v][0]>b[v][1])
{
b[u][0]+=b[v][0];
if(d[v][0])
d[u][0]=1;
}
else if(b[v][0]<b[v][1])
{
b[u][0]+=b[v][1];
if(d[v][1])
d[u][0]=1;
}
else                                        //当b[v][0]==b[v][1]时表示答案b[u][0]不唯一
{
b[u][0]+=b[v][0];
d[u][0]=1;
}
b[u][1]+=b[v][0];
if(d[v][0])
d[u][1]=1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: