POJ 2771 二分图(最大独立集)

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5244   Accepted: 2192

Description

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.

Sample Input

2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball

Sample Output

3
7

Source

 
题目意思:
有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。
 
思路:
最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 505 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
bool visited[N];
vector<int>ve[N];
int from[N]; struct node{
int h;
char sex[];
char music[];
char sport[];
}a[N]; int march(int u){
int i, j, k, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
return ;
}
}
}
return ;
} main()
{
int t, i, j, k;
cin>>t;
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].music,a[i].sport);
}
for(i=;i<=n;i++) ve[i].clear();
for(i=;i<n;i++){
if(a[i].sex[]=='F'){
for(j=;j<n;j++){
if(a[j].sex[]=='M'){
if(abs(a[i].h-a[j].h)<=&&strcmp(a[i].music,a[j].music)==&&strcmp(a[i].sport,a[j].sport)){
ve[i].push_back(j);
}
}
}
}
} int num=;
memset(from,-,sizeof(from));
for(i=;i<n;i++){
memset(visited,false,sizeof(visited));
if(march(i)) num++;
}
printf("%d\n",n-num);
}
}
上一篇:Javascript两个感叹号的用法(!!)


下一篇:mysql自定义循环函数