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

原创 2016年05月30日 16:49:11

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;
}


版权声明:本文为博主原创文章,若转载请注明转载地址http://blog.csdn.net/martinue。

相关文章推荐

POJ 1466 Girls and Boys(二分图最大独立集)

POJ 1466 Girls and Boys(二分图最大独立集) http://poj.org/problem?id=1466 题意:        在一群男女同学之间存在”浪漫关系”,且该关系只存...

NYOJ-522 Interval【树状数组】

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=522 题目大意: 给你M个区间,区间内所有整数个数+1,然后给出N个询问,问k这个数字...

LCA的四种做法(Tarjan,ST,倍增,树链剖分)

LCA的四种做法

poj2771二分图点权最大独立集,用Dinic解决

题意:有一个保守的老师,带领学生出去玩,但是他要保证学生不能成为夫妻,求在保证这个前提的情况下,他最多能带多少人。 分析:典型的二分图点权最大独立集的问题,以男女为界,分为两组,实现二分图。 注:...

poj 2771 二分图最大独立集

是一道二分图最大独立集的裸题,,,就是看着麻烦了点,,其实很简单。题目: Guardian of Decency Time Limit: 3000MS   Memory...
  • wmn_wmn
  • wmn_wmn
  • 2011年10月05日 17:06
  • 1082

POJ 2771 二分图的最大独立集

简单的最大独立集,二分匹配求解,因为这里除了男生就是女生,所以此图为二分图。   最大独立集保证任意一条边的两个顶点不会同时被选中,因此可以在同时满足题目4个要求的两人之间添加一条边,注意这...

二分图最大独立集题目汇总 HDU3829,POJ1446、2771

HDU 3829 #include #include #include #include using namespace std; struct people{ char ch1,ch2...

二分图的最大独立集-poj-2771

Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. He want...

【二分图+最大独立集】北大 poj 2771 Guardian of Decency

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...

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

POJ 2771 Guardian of Decency(二分图最大独立集) http://poj.org/problem?id=2771 题意:        老师带学生出去旅游,但是担心学生会发生...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2771(二分图,最大独立集)
举报原因:
原因补充:

(最多只允许输入30个字)