Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5428 | Accepted: 1925 |
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
树形dp,给出n,第一行是大老板,之后的n-1行str1,str2,代表下属的名字和他的直接老板的名字,问如果邀请的人中没有直接的下属关系,最多能有多少人,邀请的人员是否确定
定义dp[i][0]代表第i个人为大老板且第i个人不参加时最多邀请的人数dp[i][1]代表第i个人为大老板且第i个人参加时最多邀请的人数。
那么dp[i][0] = ∑ ( dp[j][0],dp[j][1] ) ; dp[i][1] = ∑ dp[j][0] (j为i的直接下属)
对于是否唯一的问题,取决于是否在递归是存在相同的人数。
dup[i][0] 对应dp[i][0] , dup[i][1] = dp[i][1] ;
那么dup[i][0]是否唯一取决于dp[j][0],dp[j][1]中大的那个的方法是否唯一,或者dp[j][0] == dp[j][1]一定不唯一
dup[i][1],取决于dp[j][0]是否唯一
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
map <string,int> Map ;
map <string,int>::iterator it ;
char s1[110] , s2[110] ;
struct node{
int u , v ;
int next ;
}p[300];
int head[300] , cnt ;
int dp[300][2] , dup[300][2] ;
void add(int u,int v)
{
p[cnt].v = v ;
p[cnt].u = u ;
p[cnt].next = head[u] ;
head[u] = cnt++ ;
}
void dfs(int u)
{
if( head[u] == -1 )
{
dp[u][0] = 0 ;
dp[u][1] = 1 ;
dup[u][0] = dup[u][1] = 1 ;
return ;
}
int i , j , v ;
dp[u][0] = 0 ; dp[u][1] = 1;
dup[u][0] = dup[u][1] = 1 ;
for(i = head[u] ; i != -1 ; i = p[i].next)
{
v = p[i].v ;
dfs(v) ;
dp[u][0] += max( dp[v][0],dp[v][1] );
dp[u][1] += dp[v][0] ;
if( ( dp[v][0] > dp[v][1] && !dup[v][0] ) || ( dp[v][0] < dp[v][1] && !dup[v][1] ) || dp[v][0] == dp[v][1] )
dup[u][0] = 0 ;
if( dup[v][0] == 0 )
dup[u][1] = 0 ;
}
}
int main()
{
int n , i , j , a , b , m , num ;
while( scanf("%d", &n) && n )
{
cnt = 0 ;
num = 1 ;
Map.clear();
memset(head,-1,sizeof(head));
for(i = 0 ; i < n ; i++)
{
if( i == 0 )
{
scanf("%s", s1);
Map[s1] = num++ ;
}
else
{
scanf("%s %s", s1, s2);
if( Map[s1] == 0 )
a = Map[s1] = num++ ;
else
a = Map[s1] ;
if( Map[s2] == 0 )
b = Map[s2] = num++ ;
else
b = Map[s2] ;
add(b,a);
}
}
dfs(1) ;
if( dp[1][0] > dp[1][1] )
{
printf("%d ", dp[1][0]);
if( dup[1][0] )
printf("Yes\n");
else
printf("No\n");
}
else if( dp[1][0] < dp[1][1])
{
printf("%d ", dp[1][1]);
if( dup[1][1] )
printf("Yes\n");
else
printf("No\n");
}
else
printf("%d No\n", dp[1][0]);
}
return 0;
}