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
My Problem Report
树上求最大点独立集+最大点独立集数目。基础树形DP题目,在DFS回溯的过程中进行刷表。需要注意的是直接记录基数相同的集合数目时会爆int,处理技巧是将所有集合数目大于1的情况都处理为2,或者模去一个较大的数。另外,在网上还看到了用bool数组来记录当前最优解是否唯一的方法,可以试一试。
P.S. 这里需要记住一个教训,以后将数组初值付为1时不要用memset
My Source Code
// Created by Chlerry in 2015.
// Copyright (c) 2015 Chlerry. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 210
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array, x) memset(array,x,sizeof(array))
typedef pair<int,int> P;
int n,cnt,f[Size],sum1[Size],sum2[Size],d[Size],way1[Size],way2[Size];
std::vector<int> son[Size];
map<string,int> mp;
string name,boss;
void Initialize()
{
cnt=1;
mp.clear();
mem(sum1,0);
mem(sum2,0);
mem(d,0);
for(int i=1;i<=n;i++)
son[i].clear();
// mem(way1,1);mem(way2,1);//Wrong!
for(int i=0;i<=n;i++)
way1[i]=way2[i]=1;
}
int DFS(int x)
{
for(int i=0;i<son[x].size();i++)
DFS(son[x][i]);
d[x]=max(sum1[x],sum2[x]+1);
int t;
if(sum1[x]==sum2[x]+1)
t=2;//way1[x]+way2[x];
else if(sum1[x]>sum2[x]+1)
t=way1[x];
else
t=way2[x];
// cout<<way1[x]<<','<<way2[x]<<endl;
sum1[f[x]]+=d[x];
way1[f[x]]*=t;
sum2[f[f[x]]]+=d[x];
way2[f[f[x]]]*=t;
return t;
}
int main()
{
freopen("in.txt","r",stdin);
while(cin>>n && n)
{
Initialize();
cin>>name;
mp[name]=1;
for(int i=1;i<n;i++)
{
cin>>name>>boss;
if(!mp[name])
mp[name]=++cnt;
if(!mp[boss])
mp[boss]=++cnt;
f[mp[name]]=mp[boss];
son[mp[boss]].pb(mp[name]);
}
string YN=DFS(1)>1?"No":"Yes";
// for(int i=1;i<=n;i++)
// cout<<d[i]<<' ';
// cout<<endl;
cout<<d[1]<<' '<<YN<<endl;
// cout<<"---------"<<endl;
}
return 0;
}