这是一道树形DP的题目,与之前一道树形DP一样,这个题也对应一个图论的概念——最大独立集,并且除了动态规划的方法外,同样可以用贪心做。这题我仍然只用了动态规划的方法。
首先,介绍一下什么是最大独立集。从图G的顶点集中取出若干顶点组成一个集合,使得对于图中的每条边(u,v),u和v最多只有一个属于该集合,即该集合内的点两两不相邻。在图G的所有独立集中,顶点数最多的称为最大独立集。如果图是树,那么解决起来比较方便,否则没有多项式时间解法。
然后,说一下这道题的状态转移方程。有两个状态,dp[u][0]代表u节点属于最大独立集,dp[u][1]不属于最大独立集。dp[u][1]=∑( max( dp[v][1] + dp[v][0] ) ),dp[u][0]=∑( dp[v][1] )(其中,v为u的子节点)。
最后,说一下题目另外一个比较麻烦的地方,就是要你判断这个最大独立集是否唯一。如果最大独立集不唯一,那么存在至少一个节点存在下述情况:其父节点u不属于独立集且存在一个子节点v有dp[v][1] 等于dp[v][0]。 开始考虑的时候犯了一个错误,认为只要有子节点满足dp[v][1] 等于dp[v][0]就行了,其实如果父节点属于最大独立集,那么子节点只能选dp[v][1],而与dp[v][1] 和dp[v][0]的大小关系无关,即使dp[v][1] 等于dp[v][0]也不会导致多种情况。还有一点就是要判断根节点。
顺便说一句,这道题的输入的关系很有特点,所以可以建一棵有向树,要注意不是所有的题都能建成有向树的形式,因为本来这种题是无向图的问题。
代码(G++):
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#define MAX 203
//#define LOCAL
using namespace std;
struct edge{
int to;
int next;
};
int head[MAX],dp[MAX][2];
edge array[MAX];
bool flag;
void func(int u)
{
int i,v;
dp[u][1]=0; //不属于最大独立集
dp[u][0]=1; //属于最大独立集
for(i=head[u];i!=-1;i=array[i].next)
{
v=array[i].to;
func(v);
dp[u][0]+=dp[v][1];
dp[u][1]+=dp[v][1]>dp[v][0]?dp[v][1]:dp[v][0];
}
if(!flag&&dp[u][0]<=dp[u][1])
{
for(i=head[u];i!=-1;i=array[i].next)
{
v=array[i].to;
if(dp[v][1]==dp[v][0])
{
flag=true;
break;
}
}
}
}
int main(int argc, char *argv[])
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int i,n,c,p,ans;
string s1,s2;
char name1[100],name2[100];
map<string,int> m;
while(scanf("%d",&n)&&n!=0)
{
p=1;
c=0;
flag=false;
memset(head,-1,sizeof(head));
scanf("%s",name1);
s1=name1;
m[s1]=p++;
for(i=1;i<n;i++)
{
scanf("%s %s",name1,name2);
s1=name1;
s2=name2;
if(m.find(s1)==m.end()) m[s1]=p++;
if(m.find(s2)==m.end()) m[s2]=p++;
array[c].to=m[s1];
array[c].next=head[m[s2]];
head[m[s2]]=c;
c++;
}
func(1);
ans=dp[1][0]>dp[1][1]?dp[1][0]:dp[1][1];
cout<<ans<<' ';
if(dp[1][0]==dp[1][1]) flag=true;
if(flag) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
m.clear();
}
system("PAUSE");
return EXIT_SUCCESS;
}
题目( http://acm.hdu.edu.cn/showproblem.php?pid=2412):
Party at Hali-Bula
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)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.
6 Jason Jack Jason Joe Jack Jill Jason John Jack Jim Jill 2 Ming Cho Ming 0
4 Yes 1 No