poj2771(二分图,最大独立集)

Guardian of Decency
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 5398 Accepted: 2269

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

题意:老师要找一群人漂流,可是老师怕有人谈恋爱,所以找的人之中任何两个人都不能有谈恋爱的趋势,所以给出4个没有谈恋爱趋势的标准,现在让你求老师最多带多少人去漂流。

思路:根据题目意思描述,我们要把和其他人有谈恋爱趋势最多的人找出来,所以相当于一个求最大独立集的过程,首先把有谈恋爱趋势的人连边,连完边直接用二分图跑个最大独立集就ok。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
struct data
{
    int h;
    char sex;
    char music[110],sport[110];
} x[510];

const int N=510;///两边的最大数量
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int n,m;///m,n分别表示两边的各自数量,n是左边,m是右边
bool dfs(int x)
{
    for(int i=1; i<=m; i++) ///m是右边,所以这里上界是m
        if(!use[i]&&tu[x][i])
        {
            use[i]=1;
            if(from[i]==-1||dfs(from[i]))
            {
                from[i]=x;
                return 1;
            }
        }
    return 0;
}
int hungary()
{
    int tot=0;
    memset(from,-1,sizeof(from));
    for(int i=1; i<=n; i++) ///n是左边,所以这里上界是n
    {
        memset(use,0,sizeof(use));
        if(dfs(i))
            tot++;
    }
    return tot;
}

bool ok(int i,int j)
{
    if(abs(x[i].h-x[j].h)>40)
        return 1;
    if(x[i].sex==x[j].sex)
        return 1;
    if(strcmp(x[i].music,x[j].music)!=0)
        return 1;
    if(strcmp(x[i].sport,x[j].sport)==0)
        return 1;
    return 0;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(tu,0,sizeof(tu));
        scanf("%d",&n);
        m=n;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %c%s%s",&x[i].h,&x[i].sex,x[i].music,x[i].sport);
            for(int j=1; j<i; j++)
                if(!ok(i,j))
                    tu[i][j]=tu[j][i]=1;
        }
        printf("%d\n",n-hungary()/2);
    }

    return 0;
}


转载于:https://www.cnblogs.com/martinue/p/5547199.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值