Uva--1220(树形动规)
2014-08-28 17:11:10
1220 - Party at Hali-Bula
Time limit: 3.000 seconds
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 <tex2html_verbatim_mark>(1n200) <tex2html_verbatim_mark>, the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n - 1 <tex2html_verbatim_mark>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[u][0] 表示以u为树根的子树,不取u的最优解,dp[u][1] 表示以u为树根的子树,取u的最优解。难点在于如何判断方案的唯一性。
考虑dp[u][1]时,如果dp[sons of u][0]的方案都唯一,则dp[u][1]也唯一;考虑dp[u][0]时,取max(dp[sons of u][0],dp[sons of u][1]),而且要考虑 max 的哪个 dp 是否唯一,若不唯一,则dp[u][0]也不唯一。
1 /************************************************************************* 2 > File Name: 1220.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 28 Aug 2014 03:15:58 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 typedef long long ll; 17 const int INF = 1 << 30; 18 19 char name[205][105]; 20 char boss[205][105]; 21 vector<int> ver[205]; 22 int dp[205][2]; 23 int uniq[205][2]; 24 int n; 25 26 //dp[u][0] 表示以u为树根的子树,不取u的最优解。 27 //dp[u][1] 表示以u为树根的子树,取u的最优解。 28 29 void Dp(int u){ 30 int len = ver[u].size(); 31 dp[u][1] = 1; 32 dp[u][0] = 0; 33 int v; 34 for(unsigned int i = 0; i < len; ++i){ 35 v = ver[u][i]; 36 Dp(v); 37 if(dp[v][0] > dp[v][1]){ 38 dp[u][0] += dp[v][0]; 39 if(uniq[v][0]) uniq[u][0] = 1; 40 } 41 else if(dp[v][0] < dp[v][1]){ 42 dp[u][0] += dp[v][1]; 43 if(uniq[v][1]) uniq[u][0] = 1; 44 } 45 else{ 46 dp[u][0] += dp[v][0]; 47 uniq[u][0] = 1; 48 } 49 dp[u][1] += dp[v][0]; 50 if(uniq[v][0]) uniq[u][1] = 1; 51 } 52 } 53 54 int main(){ 55 int i,j; 56 char tmp[105]; 57 while(scanf("%d",&n) == 1 && n){ 58 scanf("%s",name[0]); 59 for(i = 0; i < n; ++i) ver[i].clear(); 60 for(i = 1; i < n; ++i){ 61 scanf("%s%s",name[i],boss[i]); 62 } 63 for(i = 1; i < n; ++i){ 64 for(j = 0; j < n; ++j){ 65 if(strcmp(boss[i],name[j]) == 0) 66 break; 67 } 68 ver[j].push_back(i); 69 } 70 memset(uniq,0,sizeof(uniq)); 71 memset(dp,0,sizeof(dp)); 72 Dp(0); 73 if(dp[0][0] > dp[0][1]){ 74 printf("%d ",dp[0][0]); 75 if(uniq[0][0]) printf("No\n"); 76 else printf("Yes\n"); 77 } 78 else if(dp[0][0] < dp[0][1]){ 79 printf("%d ",dp[0][1]); 80 if(uniq[0][1]) printf("No\n"); 81 else printf("Yes\n"); 82 } 83 else printf("%d No\n",dp[0][0]); 84 } 85 return 0; 86 }