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.
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.
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.
6 Jason Jack Jason Joe Jack Jill Jason John Jack Jim Jill 2 Ming Cho Ming 0
4 Yes
1 No
其实这道题之前做过一种类似的,就是不判断人数最多时方案是否唯一,当时是队友贪的,并不知道原来这是树形dp啊。
每个节点选择或不选择直接递归求就可以。
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define MAXN 205 vector<int> v[MAXN]; int n; string s1, s2; map<string, int> ma; int tot; struct Num{ int d, f; Num() : d(1), f(1) {} }num; int get_id(string s) { if(ma[s]) return ma[s]; else return ma[s] = ++tot; } Num dp(int u, int flag) { Num ans; if(flag) ans.d = 1; else ans.d = 0; if(v[u].empty()) return ans; int k = v[u].size(); int d = 0, f = 1; if(flag) { repu(i, 0, k) { Num t = dp(v[u][i], 0); d += t.d; if(t.f == 0) f = 0; } ans.d = d + 1; ans.f = f; } else { repu(i, 0, k) { Num t1, t2; t1 = dp(v[u][i], 0); t2 = dp(v[u][i], 1); if(t1.d == t2.d) f = 0, d += t1.d; else if(t1.d > t2.d) { if(t1.f == 0) f = 0; d += t1.d; } else { if(t2.f == 0) f = 0; d += t2.d; } } ans.d = d; ans.f = f; } return ans; } int main() { while(~scanf("%d", &n) && n) { ma.clear(); tot = 0; repu(i, 0, n + 2) v[i].clear(); cin>>s1; get_id(s1); repu(i, 1, n) { cin>>s1>>s2; v[get_id(s2)].push_back(get_id(s1)); } Num t1 = dp(1, 1), t2 = dp(1, 0); if(t1.d == t2.d) printf("%d No\n", t1.d); else if(t1.d > t2.d) { printf("%d ", t1.d); if(t1.f) printf("Yes\n"); else printf("No\n"); } else { printf("%d ", t2.d); if(t2.f) printf("Yes\n"); else printf("No\n"); } } return 0; }