码迷,mamicode.com
首页 > 其他好文 > 详细

poj2771 二分图的最大独立集

时间:2015-03-10 17:15:41      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

http://poj.org/problem?id=2771

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

/**
poj 2771 最大独立集
题目大意:一个老师要带一部分人,要求所带的人中不能有可能出现会搞对象的(不满足题目中的条件之一则有可能),
          问最多能带多少学生。
解题思路:所有可能搞对象的人匹配。求二分图的最大独立集就行。最大独立集=n-匹配数
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
const int maxn=505;

int n,high[maxn];
char mus[maxn][102],sex[maxn][2],spo[maxn][103];
int w[maxn][maxn];
int linker[maxn];
bool used[maxn];
bool dfs(int u)
{
    int v;
    for(v=0;v<n;v++)
    {
        if(w[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<n;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u))res++;
    }
    return res;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%s%s%s",&high[i],sex[i],mus[i],spo[i]);
        }
        memset(w,0,sizeof(w));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(abs(high[i]-high[j])<=40&&strcmp(sex[i],sex[j])!=0&&strcmp(mus[i],mus[j])==0&&strcmp(spo[i],spo[j])!=0)
                {
                    w[i][j]=w[j][i]=1;
                }
            }
        }
        printf("%d\n",n-hungary()/2);
    }
    return 0;
}



poj2771 二分图的最大独立集

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/44177277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!