Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5418 | Accepted: 1920 |
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
树形dp入门题,前半部分非常easy,后半部分非常难搞,详见点击打开链接
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int dp[222][2]; struct node { int next; int to; }edge[222]; int head[222]; char str[111], tr[111]; int tot, n; void addedge(int from, int to) { edge[tot].to = to; edge[tot].next = head[from]; head[from] = tot++; } void dfs(int u) { dp[u][1] = 1; for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; dfs(v); dp[u][1] += dp[v][0]; dp[u][0] += max(dp[v][1], dp[v][0]); } } int main() { while (~scanf("%d", &n), n) { map<string, int>qu; qu.clear(); memset (head, -1, sizeof(head) ); memset (dp, 0, sizeof(dp)); tot = 0; int res = 0; scanf("%s", str); qu[str] = ++res; for (int i = 1; i <= n - 1; i++) { scanf("%s%s", str, tr); if (qu[str] == 0) { qu[str] = ++res; } if (qu[tr] == 0) { qu[tr] = ++res; } addedge(qu[tr], qu[str]); } dfs(1); printf("%d ", max(dp[1][0], dp[1][1])); bool flag = false; if (n == 1) { printf("Yes "); continue; } if (n == 2) { printf("No "); continue; } for (int i = 1; i <= n; i++) { if (dp[i][1] == dp[i][0]) { for (int j = head[i]; ~j; j = edge[j].next) { if (dp[edge[j].to][1] == dp[edge[j].to][0]) { flag = true; break; } } if (flag) { break; } } } if (flag) { printf("No "); continue; } printf("Yes "); } return 0; }