HDU2412 树状DP

Party at Hali-Bula

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 138    Accepted Submission(s): 51

Problem 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.

dp[i][0] 为在以 i 为根的子树中 , 不选择点 i 最多能够选的数目 ,dp[i][1] 为选择 i 点的最多数目 .

状态转移方程 :

i 为叶子节点时 :

dp[i][0]=0;

dp[i][1]=1;

i 为非叶子节点时 :

dp[i][0]=sum(max(dp[j][0],dp[j][1]))  (j i 的儿子 )

dp[i][1]=sum(dp[j][0])  (j i 的儿子 )

 

解的唯一性的判断 :

u[i][x] 0 时表示 dp[i][x] 的解唯一 , 1 则表示不唯一 .

x 0 , 若存在 j i 的儿子 , 使得 dp[j][0]>dp[j][1] u[j][0]=1, dp[j][0]<dp[j][1] u[j][1]=1 dp[j][0]=dp[j][1], u[i][0]=1;

x 1 , 若存在 j i 的儿子 , 使得 u[j][0]=1, u[i][0]=1;

 

代码如下 :

#include<stdio.h> #include<string.h> #define MAXN 205 struct Node { char name[105]; Node *next; }node[MAXN]; struct List { int node; List *next; }head[MAXN]; int dp[MAXN][2],u[MAXN][2]; void DP(int node) { List *p; int sumdp=0; if (head[node].next==NULL) { dp[node][0]=0; dp[node][1]=1; u[node][0]=0; u[node][1]=0; } else { p=head[node].next; while (p!=NULL) { DP(p->node); if (dp[p->node][0]>dp[p->node][1]) { sumdp+=dp[p->node][0]; if (u[p->node][0]==1) u[node][0]=1; } else if (dp[p->node][0]<dp[p->node][1]) { sumdp+=dp[p->node][1]; if (u[p->node][1]==1) u[node][0]=1; } else { sumdp+=dp[p->node][0]; u[node][0]=1; } dp[node][1]+=dp[p->node][0]; if (u[p->node][0]==1) u[node][1]=1; p=p->next; } dp[node][0]=sumdp; dp[node][1]+=1; } } int main() { List *s; int i,j,n; char s1[MAXN][105],s2[MAXN][105]; while (scanf("%d",&n)&&n) { memset(dp,0,sizeof(dp)); memset(u,0,sizeof(u)); memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); for (i=0;i<=n;++i) head[i].next=NULL; getchar(); scanf("%s",node[0].name); getchar(); for (i=1;i<n;++i) { scanf("%s %s",s1[i],s2[i]); getchar(); strcpy(node[i].name,s1[i]); } for (i=1;i<n;++i) { for (j=0;j<n;++j) { if (strcmp(s2[i],node[j].name)==0) { s=new List; s->node=i; s->next=head[j].next; head[j].next=s; break; } } } DP(0); if (dp[0][0]>dp[0][1]) { printf("%d ",dp[0][0]); if (u[0][0]) printf("No/n"); else printf("Yes/n"); } else if (dp[0][0]<dp[0][1]) { printf("%d ",dp[0][1]); if (u[0][1]) printf("No/n"); else printf("Yes/n"); } else printf("%d No/n",dp[0][0]); } return 0; }

你可能感兴趣的:(HDU2412 树状DP)