poj——2771 Guardian of Decency

                    poj——2771    Guardian of Decency

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5916   Accepted: 2458

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

题目大意:

题意:现在有一个老师想带领他的学生出去郊游,但是他非常担心在郊游的过程中有些学生会发生恋爱关系,而他认为发生恋爱关系的可能性比较小的判断标准有以下四个,如果满足四个条件中的任何一个,即被他认为可能发生恋爱关系的可能性比较小:

            1>两人身高的差距超过40cm;
            2>两人性别相同;
            3>两人所喜欢的音乐风格不同;
            4>两人最喜爱的运动相同;

现在给出n个学生,并给出每个学生的信息(信息为:身高 性别 所喜欢的音乐风格 喜爱的运动),要求求解最大可以带出去郊游的学生数.

思路:

由于两个女生一定不会发生恋爱关系(此处我们不考虑性取向有问题的情况(ORZ)),所以我们可以以该同学的性别为根据来建立二分图。

然后我们在判断两边的同学是否满足上述情况,若满足则连边。然后对于我们建出的这个图在求最大匹配。这样我们就转化成了裸地匈牙利算法。最多可以带的学生数=总学生数-最大匹配数。

注意:char不能用==直接比,这样比出来的只是第一个字符,而非全部,我们这里要用strcmp比较两个字符的大小,这两个字符的字典序如果相同则返回0,如果第一个字符的字典序大返回1,反之,返回-1。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 510
using namespace std;
char ch;
bool vis[N];
int t,n,x,ans,gnum,bnum,pre[N],map[N][N];
struct nn
{
    int h;
    char fm[10000];
    char fs[10000];
}girl[N],boy[N];
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘) f=-1; ch=getchar();}
    while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
}
void add_edge()
{
    for(int i=1;i<=bnum;i++)
     for(int j=1;j<=gnum;j++)
      if(!strcmp(boy[i].fm,girl[j].fm)&&strcmp(boy[i].fs,girl[j].fs)&&abs(boy[i].h-girl[j].h)<=40)
        map[i][j]=1;
}
int find(int x)
{
    for(int i=1;i<=gnum;i++)
     if(!vis[i]&&map[x][i])
     {
         vis[i]=true;
         if(pre[i]==0||find(pre[i]))
         {
             pre[i]=x;
             return 1;
         }
     }
    return 0;
}
int main()
{
    t=read();
    while(t--)
    {
        ans=0;bnum=0,gnum=0;
        memset(boy,0,sizeof(boy));
        memset(girl,0,sizeof(girl));
        memset(pre,0,sizeof(pre));
        memset(map,0,sizeof(map));
        n=read();
        for(int i=1;i<=n;i++)
        {
            x=read(); scanf("%c",&ch);
            if(ch==‘F‘)
            {
                girl[++gnum].h=x;
                cin>>girl[gnum].fm;
                cin>>girl[gnum].fs;
            }
            else
            {
                boy[++bnum].h=x;
                cin>>boy[bnum].fm;
                cin>>boy[bnum].fs;
            }
        }
        add_edge();
        for(int i=1;i<=bnum;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
时间: 2024-11-02 16:16:56

poj——2771 Guardian of Decency的相关文章

poj 2771 Guardian of Decency【最大点独立集】

K - Guardian of Decency Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2771 Appoint description:  System Crawler  (2014-08-17) Description Frank N. Stein is a very conservative high-school teac

poj 2771 Guardian of Decency 解题报告

题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距  > 40cm 2.相同性别 3.喜欢的音乐种类  不同 4.有共同喜爱的 运动 只要满足其中这4个条件中的一个(当然越多越好啦),就可以将他们编为一组啦(一组两个人),求能被编为一组的最多组数. 这题实质上求的是二分图的最大独立集.  最大独立集 = 顶点数 - 最大匹配数 可以这样转化:两个人至少

poj 2771 Guardian of Decency

Guardian of Decency http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K       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 som

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

Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 2555 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

POJ - 2771 Guardian of Decency 二分图 最大匹配数

题目大意:有n个人要参加一项活动,活动要求参加的人里面尽量不要有couples,主办方提出了四个降低couples的方法: 1.两个人的身高差大于40 2.性别相同 3.喜欢的音乐风格不同 4.喜欢的运动相同 只要满足其中的一项就认定两人不是couples 现在给出n个人的四项数据,问最多能邀请到多少人 解题思路:这题和Poj 1466 Girls and Boys这题很相似,只不过这题给的条件不是直接给出的,而是要我们自己去找的 只要两个人四个条件都不满足,就可以认为他们是couples了(相

poj 2771 Guardian of Decency(最大独立数)

题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最大匹配.题目要求的是最大独立数. 最大独立数=顶点数-最大匹配数 #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 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

UVA 12083 - Guardian of Decency(二分图最大匹配)

UVA 12083 - Guardian of Decency 题目链接 题意:给定一些男女,满足身高差不大于40,喜欢同一种音乐,不喜欢同一种体育项目,并且性别不同,就可能发生关系,现在老师要带一些男女出去玩,要求不能有一对发生关系,问最多能带多少人 思路:分男女,把会发生关系的连边,然后做最大匹配,最后n-最大匹配就是最多能带的人 代码: #include <cstdio> #include <cstring> #include <string> #include

POJ 2771 最大点独立集

这是经典的最大点独立集 还是可以转化成最大匹配数,为什么呢,因为求出最大匹配数之和,匹配的边的两个端点互斥,只能去一个,所以最后结果就用总点数-最大匹配数即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int h[600],sex[600]; char music[600][200],ball[600][