二分图匹配 --最大独立集 poj 2771

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4382   Accepted: 1833

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
题意:有个老师非常保守,他要选出一部分学生做一个短途旅行,又怕某些男女同学之间谈恋爱,因此制定了几条规则,通过规则来确定某两个人之间谈恋爱的可能性,要求选出尽可能多的人。
解题思路:典型的二分图匹配最大独立子集,通过男女生之间的特征计算最大匹配度数,总人数减去最大匹配数即为所求答案.
二分图匹配模板代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
bool used[550];
vector<int> a[550];
int linker[550];
int n;
struct node
{
       int height;
       char sex;
       string music;
       string sport;
};
node s[550];
int dfs(int u)
{
       int i;
       for(i=0;i<a[u].size();i++)
       if(used[a[u][i]]==0)
       {
              used[a[u][i]]=1;
              if(linker[a[u][i]]==-1||dfs(linker[a[u][i]]))
                 {
                        linker[a[u][i]]=u;
                        return 1;
                 }
       }
       return 0;
}
int hungray()
{
       int i,ret=0;
       memset(linker,-1,sizeof(linker));
       for(i=0;i<n;i++)
       {
              memset(used,0,sizeof(used));
              if(dfs(i))ret++;
       }
       return ret;
}
int main()
{
       int i,j,k,m,p,q,T;
       scanf("%d",&T);
       while(T--)
       {
              scanf("%d",&n);
              for(i=0;i<n;i++)cin>>s[i].height>>s[i].sex>>s[i].music>>s[i].sport;
              for(i=0;i<n;i++)a[i].clear();
              for(i=0;i<n;i++)
                 for(j=i+1;j<n;j++)
                 if(s[i].sex!=s[j].sex&&abs(s[i].height-s[j].height)<=40&&s[i].music==s[j].music&&s[i].sport!=s[j].sport)
                 {
                        a[i].push_back(j);
                 }
                 printf("%d\n",n-hungray());
       }
       return 0;
}


posted @ 2013-05-26 16:27  线性无关  阅读(194)  评论(0)    收藏  举报