Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Problem H - Guardian of Decency
Time limit: 15 seconds
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.
二分图最大匹配,使用匈牙利算法解决。
根据性别将学生分为两个不交集合。
求出最大匹配数m后,n - m即为所求。
AC Code:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define clr(a, i) memset(a, i, sizeof(a))
#define FOR0(i, n) for(int i = 0; i < n; ++i)
#define FOR1(i, n) for(int i = 1; i <= n; ++i)
#define sf scanf
#define pf printf const int MAXN = ;
struct Node
{
int h;
char gen;
char mus[];
char spo[];
void input() {
scanf("%d %c %s %s", &h, &gen, mus, spo);
}
bool satisfy(Node &y) const{
return gen != y.gen && fabs(h - y.h) <= && !strcmp(mus, y.mus) &&
strcmp(spo, y.spo);
}
}pup[MAXN];
int n;
vector<int> female, male;
bool adj[MAXN][MAXN];
int match[MAXN];
bool vis[MAXN]; void addEdge(int i)
{
adj[i][i] = false;
for (int j = ; j < i; ++j){
adj[i][j] = adj[j][i] = pup[i].satisfy(pup[j]);
}
} bool findCrossPath(int v)
{
for(int i = ; i < n; ++i)
{
if(adj[v][i] == true && vis[i] == false)
{
vis[i] = true;
if(match[i] == - || findCrossPath(match[i]))
{
match[i] = v;
return true;
}
}
}
return false;
} int Hungary()
{
int cnt = ;
memset(match, -, sizeof(match));
for(vector<int>::iterator it = male.begin(); it != male.end(); ++it)
{
memset(vis, false, sizeof(vis));
if(match[*it] == - && findCrossPath(*it))
{
++cnt;
}
}
return cnt;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
female.clear();
male.clear();
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
pup[i].input();
if(pup[i].gen == 'F') female.push_back(i);
else male.push_back(i);
addEdge(i);
}
printf("%d\n", n - Hungary());
}
return ;
}