HDU2412 树状DP

来源:互联网 发布:信息搜集软件 编辑:程序博客网 时间:2024/05/17 02:45

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 retirementfrom BCM. I wish I could invite all my co-workers, but imagine how an employeecan enjoy a party when he finds his boss among the guests! So, I decide not toinvite both an employee and his/her boss. The organizational hierarchy at BCMis such that nobody has more than one boss, and there is one and only oneemployee with no boss at all (the Big Boss)! Can I ask you to please write aprogram to determine the maximum number of guests so that no employee isinvited when his/her boss is invited too? I've attached the list of employeesand the organizational hierarchy of BCM.

Best,
--Brian Bennett

P.S. I would be very grateful if your program can indicate whether the list ofpeople is uniquely determined if I choose to invite the maximum number ofguests with that condition.

 

 

Input

The input consists of multiple test cases. Each test caseis started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCMemployees. The next line contains the name of the Big Boss only. Each of thefollowing n-1 lines contains the name of an employee together with the name ofhis/her boss. All names are strings of at least one and at most 100 letters andare separated by blanks. The last line of each test case contains a single 0.

 

 

Output

For each test case, write a single line containing anumber indicating the maximum number of guests that can be invited according tothe required condition, and a word Yes or No, depending on whether the list ofguests 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]))  (ji的儿子)

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

 

解的唯一性的判断:

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

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

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

 

代码如下: