UVALive - 3415Guardian of Decency(二分图最大独立点集)

    Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an
excursion, but he is afraid that some of them might become couples. While you can never exclude this
possibility, he has made some rules that he thinks indicates a low probability two persons will become
a couple:
• Their height differs by more than 40 cm.
• They are of the same sex.
• Their preferred music style is different.
• Their favourite sport is the same (they are likely to be fans of different teams and that would
result in fighting).
    So, for any two persons that he brings on the excursion, they must satisfy at least one of the
requirements above. Help him find the maximum number of persons he can take, given their vital
information.
Input
The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line
of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one
line for each pupil consisting of four space-separated data items:
• an integer h giving the height in cm;
• a character ‘F’ for female or ‘M’ for male;
• a string describing the preferred music style;
• a string with the name of the favourite sport.
No string in the input will contain more than 100 characters, nor will any string contain any
whitespace.
Output
For each test case in the input there should be one line with an integer giving the maximum number

of eligible pupils.

题意:一个极度封建的老师要带同学们出去玩,但是他怕在途中同学之间发生恋情,所以只带任意两个学生满足下列要求之一的去.
1》身高差 > 40
2>性别相同
3》爱好不同的音乐

4>爱好同类型的运动

思路:我们可以把两个可能发生恋情的人之间连一条边,酱求一个最大独立点集就阔以了,正好可以分为男女两列,边只会出现在男女之间,注意建图一定要分两列,千万不要混在一起

最大独立点集+最小点覆盖 = n,道理很清楚,去掉最小点覆盖的所有点,图中一条边都没有惹,可不就最大独立点集了嘛.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int h;
	char x;
	string music;
	string kind;
} sm[520],sf[520];

int n,p,q;
int g[520],vis[520];
int mp[520][520];

int find(int x)
{
	for(int i = 1;i<= q;i++)
	{
		if(mp[x][i]&&!vis[i])
		{
			vis[i] = 1;
			
			if(!g[i]||find(g[i]))
			{
				g[i] = x;
				return 1;
			}
		}
	}
	
	return 0;
}

void init()
{
	p = q = 0;
	mem(mp,0);
	mem(g,0);
}

int main()
{
	int t;
	cin>>t;
	
	while(t--)
	{
		init();
		scanf("%d",&n);
		for(int i = 1;i<= n;i++)
		{
			node st;
			cin>>st.h>>st.x>>st.music>>st.kind;
			if(st.x == 'M')
				sm[++p] = st;
			else
				sf[++q] = st;
		}
		
		for(int i = 1;i<= p;i++)
		{
			for(int j = 1;j<= q;j++)
			{
				if(abs(sm[i].h-sf[j].h)<= 40&&sm[i].music.compare(sf[j].music) == 0&&sm[i].kind.compare(sf[j].kind) != 0)
					mp[i][j] = 1;
			}
		}
		
		int sum = 0;
		for(int i = 1;i<= p;i++)
		{
			mem(vis,0);
			if(find(i))
				sum++;
		}
		
		printf("%d\n",n-sum);
	}
	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值