Party at Hali-Bula
| Source : Tehran2006 |
| Time limit : 3 sec | | Memory limit : 64 M |
Submitted : 368, Accepted : 150
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
Sample Output
4 Yes
1 No
题意:比较经典的派对问题。。。邀请一些成员,使得两两之间没有直接从属关系,问最多能邀请多少人,并问在
邀请人数最大的情况下,是否有多种方案。。。
思路:数量的不必多说了吧。。。方案问题就是从方程转移的方向确定。。。dp[x][1]只能从dp[y][0]相加得到,如果dp[y][0]有多种方案,那么dp[x][1]也有多种方案,dp[x][0] 是从dp[y][0],dp[y][1]中的较大者转移过来,如果dp[y][0]==dp[y][1]那么dp[x][0]就有多种方案,如果dp[y][0] > dp[y][1]且dp[y][0]有多种方案,那么dp[x][0]也有多种方案,如果dp[y][0] < dp[y][1] 且 dp[y][1]有多种方案,那么dp[x][0]也有多种方案。。。那么就出来了
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
#define max(a,b) (a) < (b) ? (b) : (a)
#define LL long long
const int maxn = 210;
int dp[maxn][2];
bool same[maxn][2];
map<string,int> name;
string s;
char buffer[110];
int n , m , sz;
struct Node
{
int y;
Node *next;
}*first[maxn] , edge[maxn*2];
void add(int x,int y)
{
edge[++m].y = y;
edge[m].next = first[x];
first[x] = &edge[m];
}
void tp(int x,int fa)
{
dp[x][1] = 1;
dp[x][0] = 0;
same[x][0] = same[x][1] = false;
Node *p = first[x];
while (p)
{
int y = p->y;
if (y!=fa)
{
tp(y,x);
dp[x][1] += dp[y][0];
dp[x][0] += max(dp[y][0],dp[y][1]);
if (same[y][0]) same[x][1] = true;
if (dp[y][0]==dp[y][1]) same[x][0] = true;
else if (dp[y][1] > dp[y][0])
same[x][0] = same[x][0] || same[y][1];
else
same[x][0] = same[x][0] || same[y][0];
}
p = p->next;
}
}
void init()
{
name.clear();
memset(first,0,sizeof(first));
memset(edge,0,sizeof(edge));
m = sz = 0;
}
void input()
{
scanf("%s",buffer);
s = buffer;
name[s] = ++sz;
for (int i = 1 ; i < n ; ++i)
{
scanf("%s",buffer);
s = buffer;
int u , v;
if (name.count(s))
u = name[s];
else
u = name[s] = ++sz;
scanf("%s",buffer);
s = buffer;
if (name.count(s))
v = name[s];
else
v = name[s] = ++sz;
add(u,v);
add(v,u);
}
}
void solve()
{
tp(1,-1);
printf("%d ",max(dp[1][0],dp[1][1]));
if (dp[1][0]==dp[1][1])
printf("No\n");
else if (dp[1][0] > dp[1][1])
{
if (same[1][0]) printf("No\n");
else printf("Yes\n");
}
else
{
if (same[1][1]) printf("No\n");
else printf("Yes\n");
}
}
int main()
{
while (scanf("%d",&n),n)
{
init();
input();
solve();
}
}
题后语:这道题目是怎么提出来的?为什么上司与下属之间不能同时存在,难道上司和下属之间永远都不能好好相处吗?上司和下属之间是多么的水火不容。在我自己印象中对领导总是那么无法直视,在他们面前总是会被他们的气场压住。。。什么时候上司与下属之间能和谐一点啊。。。不是那种看起来和谐,而是真的和谐。。。。