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
的情况
豌豆代理(又称豌豆 IP)是一款一站式国内代理 IP 服务平台,主打高匿名、低延迟、高可用的 IP 资源,支持 HTTP/HTTPS/SOCKS5 协议,适配 Windows、Mac、Android、iOS 多平台。 多类型 IP 资源与高覆盖节点 提供动态住宅 IP、静态独享 IP、数据中心 IP,覆盖全国 200 + 城市,可用率 99%+;支持省市精准选择或全国混拨,适配不同业务合规与稳定性需求。 使用:在客户端 “节点 / 线路” 页,按城市 / 类型筛选,一键连接目标 IP,适合爬虫、电商多账号运营等场景。 秒级 IP 切换与灵活调度 支持手动一键切换、秒级动态切换(切换速度低至 100ms)、定时切换(自定义时长),并自动过滤重复 IP,避免重复使用导致风险。 使用:在 “设置” 中开启 “自动切换” 并设时间间隔,或按 Ctrl+Q 快捷键一键换 IP,适配反爬虫、批量测试等高频切换场景。 全协议支持与多端适配 兼容 HTTP/HTTPS/SOCKS5 主流代理协议,可对接浏览器、爬虫脚本、客户端软件;支持 Windows、Mac、安卓、iOS 多端同步使用,跨设备无缝切换。 使用:在客户端 “协议设置” 选择对应协议,生成代理地址与端口,直接填入目标软件即可生效。 隐私安全与数据加密 自研传输加密技术保护数据传输,搭配高匿名 IP 隐藏真实地址,同时支持自动清除 Cookie / 缓存,降低隐私泄露与追踪风险。 使用:在 “安全设置” 中开启 “数据加密” 与 “自动清理缓存”,公共 WiFi 环境下优先启用,提升隐私防护等级。 智能筛选与稳定网络优化 系统自动筛选低延迟、高可用 IP,过滤失效 / 重复地址;依托自建纯净机房与独享带宽,搭配 BGP 多线接入,保障连接稳定性与速度。 使用:无需手动配置,客户端默认智能匹配合适节点,复杂网络环境可在 “网络
在网络高速发展的时代,众多的软件被开发出来,给用户带来了很大的选择余地,而且人们越来越追求更个性的需求。在这种时代背景下,商家只能以用户为导向,以商品的持续创新作为商家最重要的事项。 在新发展的时代,人们对幼儿资源互助共享平台越来越重视,才能实现幼儿资源互助共享平台的有效发挥,本文将通过幼儿资源互助共享平台的信息,分析在日常生活中对幼儿资源互助共享平台存在哪些问题探讨出进一步提升效率,管理能力的对策。 系统采用了Java技术,将所有模块采用以浏览器交互的模式,选择MySQL作为系统的数据库,来进行系统的设计。基本实现了幼儿资源互助共享平台应有的主要功能模块,本系统有管理员:首页、个人中心、用户管理、卖家管理、咨询师管理、萌宝信息管理、幼儿知识管理、保姆推荐管理、音频资源管理、二手商品管理、商品分类管理、资源分类管理、交流论坛、系统管理,用户;首页、个人中心、萌宝信息管理、保姆推荐管理、音频资源管理,卖家;首页、个人中心、二手商品管理、订单管理,咨询师;首页、个人中心、幼儿知识管理,前台首页;首页、萌宝信息、幼儿知识、保姆推荐、音频资源、二手商品、交流论坛、个人中心、后台管理、购物车等功能。 对系统进行测试后,改善了程序逻辑和代码。同时确保系统中所有的程序都能正常运行,所有的功能都能操作,本系统的开发获取幼儿资源互助共享平台信息能够更加方便快捷,同时也使幼儿资源互助共享平台信息变的更加系统化、有序化。系统界面较友好,易于操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值