HDU2412 && POJ3342:Party at Hali-Bula(树形DP)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5701 | Accepted: 2034 |
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
题意:一个公司想举办聚会,但是为了气氛,如果邀请一个员工的话,是不会再邀请他的直接上司。这个公司的人员结构是树形的,每个人至多有一名直接上司。给出这个公司的人员结构,求出能参加聚会的人数的最大值,同时判断方案是否唯一。思路: 求树的最大独立集,不同的是还需要判断方案是否唯一。
dp[u][0]为不选节点u,以节点u为根的子树得到的最大独立集,f[u][1]为对应的方案数。
dp[u][1]为选节点u,以节点u为根的子树得到的最大独立集,f[u][0]为对应的方案数。
假如不懂的话看:http://m.blog.csdn.net/blog/u012139398/43765031
代码:
View Code#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 1000000000
const int N = 205;
int head[N],tot,n;
int dp[N][2],f[N][2];
map<string,int> mp;
struct Edge
{
int to,next;
} edge[N*2];
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
mp.clear();
}
void dfs(int u,int fa)
{
dp[u][1] = 1,dp[u][0] = 0;
f[u][1] = f[u][0] = 1;
for(int i = head[u];i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
dfs(v,u);
dp[u][1] += dp[v][0];
f[u][1] *= f[v][0];
if(dp[v][0]>dp[v][1])//父节点不选,子节点可选可不选挑个大的,方案数修改
{
dp[u][0] += dp[v][0];
f[u][0] *= f[v][0];
}
else if(dp[v][0]<dp[v][1])
{
dp[u][0] += dp[v][1];
f[u][0] *= f[v][1];
}
else
{
dp[u][0] += dp[v][1];
f[u][0] *= (f[v][1]+f[v][0]);
}
}
}
int main()
{
string s,s1,s2;
int sz;
while(~scanf("%d",&n)&&n)
{
sz = 1;
init();
cin >> s;
mp[s] = sz++;//根为1
for(int i=1; i<n; i++)
{
cin >> s1;
if(!mp.count(s1))
mp[s1] = sz++;
cin >> s2;
if(!mp.count(s2))
mp[s2] = sz++;
int u = mp[s1], v = mp[s2];
addedge(u,v);
addedge(v,u);
}
dfs(1,0);
int ans = max(dp[1][1],dp[1][0]);
printf("%d ",ans);
if(dp[1][0]==dp[1][1] || (ans==dp[1][0]&&f[1][0]!=1) || (ans==dp[1][1]&&f[1][1]!=1))
puts("No");
else
puts("Yes");
}
return 0;
}