现在的位置: 首页 > 综合 > 正文

Hdu 2412 Party at Hali-Bula

2018年01月14日 ⁄ 综合 ⁄ 共 2920字 ⁄ 字号 评论关闭

Party at Hali-Bula

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
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
树形DP  题目其实挺简单,就是输入处理起来比较麻烦

d[i][0]表示以i为根的结点不取i结点的最大值
d[i][1]表示以i为根的结点取i结点的最大值
flag[i][0]表示以i为根的结点不取i结点取最大值时是否有多解
flag[i][1]表示以i为根的结点取i结点取最大值时是否有多解

#include<iostream>
#include<cstring>
#include<string>
#include<set>
#include<vector>

using namespace std;

#define Max(a,b) (a>b?a:b)

string num[210],name1[210],name2[210];
set<string> vis;
vector<int> son[210];
int d[210][2],flag[210][2];

void DFS(int x)
{
	d[x][0]=0;
	d[x][1]=1;
	flag[x][0]=0;
	flag[x][1]=0;
	for(int i=0;i<son[x].size();i++)
	{
		int v=son[x][i];
		DFS(v);
		d[x][0]+=Max(d[v][1],d[v][0]);
		d[x][1]+=d[v][0];
		if(flag[v][0]==1)
		{
			flag[x][1]=1;
			if(d[v][0]>d[v][1])
				flag[x][0]=1;
		}
		if(flag[v][1]==1 && d[v][1]>d[v][0])
			flag[x][0]=1;
		if(d[v][1]==d[v][0])
			flag[x][0]=1;
	}
}


int main()
{
	int i,j,n,u,v;
	while(scanf("%d",&n),n)
	{
		vis.clear();

		cin>>name1[0];	vis.insert(name1[0]);
		num[0]=name1[0];
		for(i=1;i<n;i++)
		{
			cin>>name1[i]>>name2[i];
			if(!vis.count(name1[i]))
			{
				num[vis.size()]=name1[i];
				vis.insert(name1[i]);
			}
			if(!vis.count(name2[i]))
			{
				num[vis.size()]=name2[i];
				vis.insert(name2[i]);
			}
			son[i].clear();
		}
		son[0].clear();
		for(i=1;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				if(name1[i]==num[j])
					v=j;
				if(name2[i]==num[j])
					u=j;
			}
			son[u].push_back(v);
		}
		DFS(0);
		if(d[0][0]>d[0][1])
		{
			printf("%d ",d[0][0]);
			if(!flag[0][0])
				printf("Yes\n");
			else
				printf("No\n");
		}
		if(d[0][0]<d[0][1])
		{
			printf("%d ",d[0][1]);
			if(!flag[0][1])
				printf("Yes\n");
			else
				printf("No\n");
		}
		if(d[0][0]==d[0][1])
			printf("%d No\n",d[0][0]);

/*		for(i=0;i<n;i++)
			printf("%d %d %d %d %d\n",i,d[i][0],d[i][1],flag[i][0],flag[i][1]);
*/
	}

	return 0;
}

 

抱歉!评论已关闭.