POJ 2771 Guardian of Decency最大独立集

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.:/li>

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

四个条件满足其一则可以一起去,即配对的需要四个条件都满足,而最大独立集等于点的个数-最大匹配数,套一下模板就哦了~注意字符串直接用strcmp比就得了,还有男生女生的序号是一边读一边加的vN uN的大小读完也就知道了 第一次错在把vN uN当做n处理了

#include<cstdio>
#include<cstring>
using namespace std;
/* **************************************************************************
//二分图匹配(匈牙利算法的DFS实现)
//初始化:g[][]两边顶点的划分情况
//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配
//g没有边相连则初始化为0
//uN是匹配左边的顶点数,vN是匹配右边的顶点数
//调用:res=hungary();输出最大匹配数
//优点:适用于稠密图,DFS找增广路,实现简洁易于理解
//时间复杂度:O(VE)
//***************************************************************************/
//顶点编号从0开始的
const int MAXN=550;
int uN,vN;//u,v数目
int boy,girl,t,n;
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
    int v;
    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {//找增广路,反向
              linker[v]=u;
              return true;
          }
      }
    return false;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
//******************************************************************************/

struct stu
{
    int high;
    char sex[3];
    char music[110];
    char sport[100];
    int num;
}arr[550];
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            girl=boy=0;
            uN=vN=0;
            memset(g,0,sizeof(g));
            for(int i=0;i<n;i++)
            {
                scanf("%d%s%s%s",&arr[i].high,arr[i].sex,arr[i].music,arr[i].sport);
                if(arr[i].sex[0]=='F') arr[i].num=vN++;
                else arr[i].num=uN++;
                for(int j=0;j<i;j++)
                {
                    if((arr[i].high-arr[j].high<=40&&arr[i].high-arr[j].high>=-40)&&strcmp(arr[i].sex,arr[j].sex)&&strcmp(arr[i].music,arr[j].music)==0&&strcmp(arr[i].sport,arr[j].sport))
                    {
                       if(arr[i].sex[0]=='M') g[arr[i].num][arr[j].num]=1;
                       else g[arr[j].num][arr[i].num]=1;
                    }
                }

            }
            //printf("%d %d\n",uN,vN);
            printf("%d\n",n-hungary());
        }
    }
    return 0;
}



全部评论

相关推荐

小小梦想家l:图片没加载出来给我整的心都凉了,现在心暖暖的
点赞 评论 收藏
分享
牛客175617325号:有的面试官不开摄像头 可能是因为他是竞业来的
点赞 评论 收藏
分享
曾经那些轻松点击就能进入的企业,如今却变得难以接近。我的一个C9朋友,虽然有人工智能专业的背景和实习经历,但现在除了潍柴,几乎没有其他选择,笛子也只给了个产品经理的岗位。简历投出去的结果大多是挂掉,难道算法岗现在只要硕士了吗?
牛客42556566号:0offer,😋哦0offer,😋哦0offer,😋有了你,🤨生活美好,🥰没烦恼。🥳0offer传奇,😎奇妙至极,😆最棒人生,😎人人赞叹你。😁如果有了,🤗那就不对,🤔今晚没offer,😨否则我会吼叫。😱无论白天,🌞还是夜晚,🌚0offer的感觉,💃🏻让我舞动翩翩。😋在酷暑或寒冬,😨0offer的爱,😍让食欲浓。😋嗯嗯嗯嗯嗯,🤓0offer人生,😋众人齐唱。🤩0offer称霸,🤨欢庆不迟,🥳最棒人生,🤩最美秋招,😋欢呼此时!🥳嗯嗯嗯嗯嗯,🤓0offer,😋哦0offer,😋哦0offer,😋美味如梦,🥰像中大奖的狂喜。😆继续玩耍,🥳别停歇,🤣乐趣不断,😆继续向前!😎
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务