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;
代碼如下 :