UVALive - 3415 Guardian of Decency (the largest independent point set in a bipartite graph)

    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.

The meaning of the question: An extremely feudal teacher wants to take his classmates out to play, but he is afraid of falling in love with his classmates on the way, so he only takes any two students to meet one of the following requirements.
1" Height difference> 40
2> Gender Same
3 "Love different music

4> Like sports of the same type

Idea: We can connect an edge between two people who may fall in love. It is enough to ask for a maximum independent point set, which can be divided into two columns of men and women, and edges will only appear between men and women, pay attention to map construction Must be divided into two columns, do not mix !

The maximum independent point set + minimum point coverage = n, the reason is very clear, remove all the points covered by the minimum point, and there is no edge in the graph, but it is the maximum independent point set.

Code:

#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])
		{
			show [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);
}

intmain()
{
	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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902960&siteId=291194637