hdu 2412 Party at Hali-Bula
Party at Hali-Bula
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1351 Accepted Submission(s): 447
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
Source
2006 Asia Regional Tehran
Recommend
lcy
1 //15MS 348K 1413 B C++ 2 /* 3 4 题目大意: 5 6 n个人形成一个关系树,每个节点代表一个人, 7 节点的根表示这个人的唯一的直接上司,只有根没有上司。 8 要求选取一部分人出来,使得每2个人之间不能有直接的 9 上下级的关系, 10 求最多能选多少个人出来,并且求出获得最大人数的 11 选人方案是否唯一。 12 13 树形DP: 14 dp[i][0]表示以i为根的树中不选i的最优数目 15 dp[i][1]表示以i为根的树中选i的最有数目 16 dup[i][j]为0时表示dp[i][j]解不唯一 17 dup[i][j]为1时表示dp[i][j]有唯一解 18 19 分析状态转移: 20 21 dp[u][0]+=max(dp[v][0],dp[v][1]); 22 dp[u][1]+=dp[v][0]; 23 24 对于叶子结点, dup[k][0] = dup[k][1] = 1. 25 对于非叶子结点, 26 对于i的任一儿子j,若(dp[j][0] > dp[j][1] 且 27 dup[j][0] == 0) 或 (dp[j][0] < dp[j][1] 且 28 dup[j][1] == 0) 或 (dp[j][0] == dp[j][1]), 29 则dup[i][0] = 0 30 对于i的任一儿子j有 dup[j][0] == 0, 则 31 dup[i][1] = 0 32 33 */ 34 #include<iostream> 35 #include<map> 36 #include<string> 37 #include<vector> 38 using namespace std; 39 vector<int>V[205]; 40 int dp[205][2]; //记录人数 41 int dup[205][2]; //记录是否可行 42 int n; 43 void dfs(int u) 44 { 45 dp[u][0]=0; 46 dp[u][1]=1; 47 dup[u][0]=1; 48 dup[u][1]=1; 49 int m=V[u].size(); 50 for(int i=0;i<m;i++){ 51 int v=V[u][i]; 52 dfs(v); //由下往上递推子状态 53 dp[u][0]+=max(dp[v][0],dp[v][1]); //父节点和全部子节点有关 54 dp[u][1]+=dp[v][0]; 55 if(dp[v][0]>dp[v][1] && dup[v][0]==0) dup[u][0]=0; 56 else if(dp[v][0]<dp[v][1] && dup[v][1]==0) dup[u][0]=0; 57 else if(dp[v][0]==dp[v][1]) dup[u][0]=0; 58 if(dup[v][0]==0) dup[u][1]=0; 59 } 60 } 61 int main(void) 62 { 63 string a,b; 64 while(cin>>n,n) 65 { 66 memset(dp,0,sizeof(dp)); 67 memset(dup,0,sizeof(dup)); 68 for(int i=0;i<=n;i++) V[i].clear(); 69 map<string,int>M; //使用map记录人名 70 int m=1; 71 cin>>a; 72 M[a]=m++; 73 for(int i=1;i<n;i++){ 74 cin>>a>>b; 75 if(M[a]==0) M[a]=m++; 76 if(M[b]==0) M[b]=m++; 77 V[M[b]].push_back(M[a]); 78 } 79 dfs(1); 80 printf("%d ",dp[1][0]>dp[1][1]?dp[1][0]:dp[1][1]); 81 if(dp[1][0]>dp[1][1] && dup[1][0]) puts("Yes"); 82 else if(dp[1][0]<dp[1][1] && dup[1][1]) puts("Yes"); 83 else puts("No"); 84 } 85 return 0; 86 }