UVa 1220 Party at Hali-Bula

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$ \le$n$ \le$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
 
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;

// next[i]代表第i个点的子结点
vector<int> next[220];

// my_map[x]代表名字为x的结点编号
map<string, int> my_map;

// 节点个数
int n;

// record[i][0/1]代表以i节点为根选/不选i的最大独立集结点数
//int record[220][2];
int record[220];
// f[i][0/1]代表以i节点为根选/不选i的最大独立集是否唯一
//int f[220][2];

// alone[i]=1代表以i节点为根的最大独立集唯一,为0为不唯一
int alone[220];

//int get_max(int x, int flag);
int get_max(int x);

int main()
{
	while(cin >> n && n != 0)
	{

		int ch;
		while((ch=getchar()) != '\n') 
			;
		memset(record, -1, sizeof(record));
//		memset(f, 0, sizeof(f));
		my_map = map<string, int>();
/*		for(int i = 1; i <= n; i++)
			next[i] = vector<int>();
*/		// 读入数据
		int count = 1;
		for(int i = 1; i <= n; i++)
		{
			string str;
			getline(cin, str);
		//	cout << "str:" << str << endl;
			if(i == 1)
			{
//				cin >> str;
//				my_map[str] = i;
				my_map[str] = count;
				next[count] = vector<int>();
				count++;
			}	
			else
			{
	
				stringstream scin(str);
				string s1, s2;
				scin >> s1 >> s2;
//				cin >> s1 >> s2;
				if(my_map.find(s1) == my_map.end())
				{
					my_map[s1] = count;
					next[count] = vector<int>();
					count++;
				}
				if(my_map.find(s2) == my_map.end())
				{
					my_map[s2] = count;
					next[count] = vector<int>();
					count++;
				}
				next[my_map[s2]].push_back(my_map[s1]);	
			}	
		}

		// 计算结果
		
		printf("%d ", get_max(1));	
		if(alone[1] == 1)
			printf("Yes\n");
		else
			printf("No\n");			
		
/*		int r1 = get_max(1, 0);
		int r2 = get_max(1, 1);
		int flag, r;
		if(r1 > r2)
		{
			r = r1;
			flag = f[1][0];
			
		}		
		else if(r1 < r2)
		{
			r = r2;
			flag = f[1][1];
		}
		else
		{
			r = r1;
			flag = 0;
		}
		printf("%d ", r);
		if(flag == 1)
			printf("Yes\n");
		else
			printf("No\n");
*/	}		
	return 0;
}


// 计算以x为根的子树的最大独立集结点个数
// flag为0代表不选x, flag为1代表选x
//int get_max(int x, int flag)
int get_max(int x)
{
/*
	if(record[x][flag] != -1)
		return record[x][flag];

	if(next[x].size() == 0)
	{
		record[x][flag] = flag;
		f[x][flag] = 1;
//		alone[x] = 1;
		return record[x][flag];
	}

	// 如果选i, 就查看不选子节点的情况
	if(flag == 1)
	{
		int n_flag = 1;
		int sum = 1;
		for(int i = 0; i < next[x].size(); i++)
		{
			sum += get_max(next[x][i], 0);
			n_flag = n_flag && f[next[x][i]][0];	
		}
		f[x][flag] = n_flag;
		record[x][flag] = sum;
		return record[x][flag];	
	}
	// 如果是不选i, 则子节点可选可不选
	else		
	{
		int n_flag = 1;
		int sum = 0;
		for(int i = 0; i < next[x].size(); i++)
		{
			int s1 = get_max(next[x][i], 0);
			int s2 = get_max(next[x][i], 1);
			if(s1 == s2)
			{
				n_flag = 0;
				sum += s1;
			}	
			else if(s1 > s2)
			{
				n_flag = n_flag && f[next[x][i]][0];
				sum += s1;
			}
			else
			{
				n_flag = n_flag && f[next[x][i]][1];
                              	sum += s2;
			}
		}		
		f[x][flag] = n_flag;
                record[x][flag] = sum;
                return record[x][flag];	
	}
*/

	if(record[x] != -1)
                return record[x];

        if(next[x].size() == 0)
        {
                record[x] = 1;
                alone[x] = 1;
//              alone[x] = 1;
                return record[x];
        }
	// 分别记录儿子的结果数之和,以及孙子的节点数之和+1
	int sum = 0, sub_sum = 1;
	int alone_son = 1, alone_son2 = 1;	
	for(int i = 0; i < next[x].size(); i++)
	{
		int s = next[x][i];
		
		sum += get_max(s);
		alone_son = alone_son && alone[s];

		for(int j = 0; j < next[s].size(); j++)
		{
			sub_sum += get_max(next[s][j]);			
			alone_son2 = alone_son2 && alone[next[s][j]];
		}
	}	
	
	record[x] = max(sum, sub_sum);
	if(sum == sub_sum)
		alone[x] = 0;
	else if(sum > sub_sum)
		alone[x] = alone_son;
	else
		alone[x] = alone_son2;
	return record[x];
		
}


 
树的最大独立集问题。
在汝佳的书上讲d(i)表示以i为根的最大独立集的结点个数,考虑选i或者不选i两种情况,可得
d(i) = max(1 + sum(d(j)) (j为i的孙子) ,  sum(d(j)) (j为i的儿子))。
状态使用1维就够了,不知道为什么后面的这道例题就改用两维状态了,网上的题解也都是两维状态,实际没有必要。
判断是否唯一,就只需要判断这两个情况是否相同,相同就不唯一,
若儿子的那一方大,就需要每个儿子的解都不同。
若孙子的那一方大,就需要每个孙子的解都不同。
 
这题一开始WA了好几次,不知道为什么。后来发现是输入的时候不一定按照先父亲后儿子的顺序,input有可能会出现:
Jam
Tom Jesse
Jesse Jam
的情况
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值