POJ 2771 Guardian of Decency【最大独立团】

Guardian of Decency

Time Limit: 3000MS

 

Memory Limit: 65536K

Total Submissions: 5416

 

Accepted: 2278

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

Northwestern Europe 2005

 

题外吐槽:

有的时候发现做图论题蛮有意识.............


题目大意:

学校的老师想要带他的学生去远行,不过他怕这次远行会促成一些学生搞对象(目测初中高中老师0.0,反正大学老师又不会带我们粗去玩而且大学都可以结婚了好伐。)然后他认为,以下条件满足任意一条就不会使得两个人搞对象:

1、两人的身高差大于40cm(我就不说我高中同学的女票身高差差不多达到了40cm)

2、两人是同性(理工大学,同性才是真爱)

3、两个人喜欢的音乐风格不同(这个根本就是赢凑上的条件.......)

4、两个人喜欢的体育项目相同(这个明明也是..........)

让你写程序求一下最多他一次能够带多少人出行。


思路:很明显的最大独立团的问题,两个人如果可以有恋爱条件,那么将这两个人看成节点连接起来,然后求一下二分匹配数,我建的是无向图,所以这里最大独立团==n-最大二分匹配数/2


我使用的是vector+匈牙利算法实现的,还是比较慢的一个状态,大家参考参考,不过对于这个题给出3000ms,匈牙利算法是可以稳稳通过的。


AC代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct people
{
    int h;
    char sex[1005];
    char style[1005];
    char sport[1005];
}a[50005];
vector<int >mp[50000];
int vis[50000];                  //标记数组.
int match[50000];
int abs(int x)
{
    if(x<0)return -x;
    else return x;
}
int find(int x)
{
    for(int i=0;i<mp[x].size();i++)
    {
        int v=mp[x][i];
        if(vis[v]==0)
        {
            vis[v]=1;
            if(match[v]==-1||find(match[v]))
            {
                match[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        memset(match,-1,sizeof(match));
        for(int i=0;i<=n;i++)mp[i].clear();
        for(int i=0;i<n;i++)
        {
            scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].style,a[i].sport);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(abs(a[i].h-a[j].h)<=40&&strcmp(a[i].sex,a[j].sex)!=0&&strcmp(a[i].style,a[j].style)==0&&strcmp(a[i].sport,a[j].sport)!=0)
                {
                    mp[i].push_back(j);
                    mp[j].push_back(i);
                }
            }
        }
        int output=0;
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))output++;
        }
        printf("%d\n",n-output/2);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值