[UVA][樹形dp] 1220 - Party at Hali-Bula
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 (1n200) , 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[node][2] 表示要不要約這個人的最大人數。
如果要邀 node, 則 dp[node][1] = sigma(dp[subnode][0]);
// 其下員工都不參加
反之, dp[node][0] = sigma(max(dp[subnode][0], dp[subnode][1]));
// 其下員工可參加或不參加
方法數的計算也很簡單,再此就不多做描述。
#include <stdio.h>
#include <map>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> g[205];
int dp[205][2];//[i][0] i can't be invited, [1] can
int way[205][2];
void dfs(int nd) {
dp[nd][1] = 1;
for(vector<int>::iterator it = g[nd].begin();
it != g[nd].end(); it++) {
dfs(*it);
dp[nd][1] += dp[*it][0];
if(dp[*it][0]) {
way[nd][1] *= way[*it][0];
if(way[nd][1] >= 2)
way[nd][1] = 2;
}
dp[nd][0] += max(dp[*it][0], dp[*it][1]);
if(max(dp[*it][0], dp[*it][1])) {
int tmp = 0;
if(max(dp[*it][0], dp[*it][1]) == dp[*it][0])
tmp += way[*it][0];
if(max(dp[*it][0], dp[*it][1]) == dp[*it][1])
tmp += way[*it][1];
way[nd][0] *= tmp;
if(way[nd][0] >= 2)
way[nd][0] = 2;
}
}
}
int main() {
int n;
int i, j, k;
int x, y;
char s[105];
while(scanf("%d", &n) == 1 && n) {
map<string, int> R;
for(i = 1; i <= n; i++) {
g[i].clear(), dp[i][0] = dp[i][1] = 0;
way[i][0] = way[i][1] = 1;
}
scanf("%s", s);
int &boss = R[s];
int size = 1;
boss++;
for(i = 0; i < n-1; i++) {
scanf("%s", &s);
int &x = R[s];
scanf("%s", &s);
int &y = R[s];
if(x == 0) x = ++size;
if(y == 0) y = ++size;
g[y].push_back(x);
}
dfs(1);
printf("%d ", max(dp[1][0], dp[1][1]));
int tmp = 0;
if(max(dp[1][0], dp[1][1]) == dp[1][0])
tmp += way[1][0];
if(max(dp[1][0], dp[1][1]) == dp[1][1])
tmp += way[1][1];
puts(tmp == 1 ? "Yes" : "No");
}
return 0;
}