POJ 2771 Guardian of Decency 最大独立集

本文介绍了一个关于最大独立集的问题背景及其解决方案。该问题源于一个保守老师的独特需求,即如何在考虑学生身高、性别、音乐偏好及体育爱好的条件下,找出能够避免形成情侣组合的学生最大数量。文章详细阐述了解决这一问题的算法实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Guardian of Decency
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 4675 Accepted: 1957

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


老师带学生旅游,但是不能带贪恋的学生,问老师最多带多少人。不谈恋爱标准:
1:身高差距大于40(个人认为真心相爱的话身高只是一个符号)
2:性别是一样的
3:音乐喜好不一样
4:运动爱好是一样的
除了第二条,搞不懂这个老师是怎么认为两个人贪恋爱的。。O__O"…
将发生贪恋爱的人配对,则答案为此二分图的最大独立集,因为x,y的集合都是从1~n,左右对称,所以求出最大匹配值之后,还要除以2.
最大独立集=总点个数-最大匹配数。
//1500K	1172MS
#include<stdio.h>
#include<string.h>
#include<math.h>
#define M 507
int link[M],g[M][M];
bool vis[M];
int n;
struct ST
{
    int height;
    char sex[2],music[107],sport[107];
}pupils[M];
bool judge(int x,int y)
{
    if(fabs(pupils[x].height-pupils[y].height)>40||strcmp(pupils[x].sex,pupils[y].sex)==0||strcmp(pupils[x].music,pupils[y].music)!=0||strcmp(pupils[x].sport,pupils[y].sport)==0)
        return false;
    return true;
}
bool find(int i)
{
    for(int j=1;j<=n;j++)
        if(!vis[j]&&g[i][j])
        {
            vis[j]=true;
            if(!link[j]||find(link[j]))
            {
                link[j]=i;
                return true;
            }
        }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(link,0,sizeof(link));
        memset(g,0,sizeof(g));
        int count=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d%s%s%s",&pupils[i].height,pupils[i].sex,pupils[i].music,pupils[i].sport);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(judge(i,j))g[i][j]=1;
        for(int i=1;i<=n;i++)
        {
            memset(vis,false,sizeof(vis));
            if(find(i))count++;
        }
        printf("%d\n",n-count/2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值