题目链接:http://poj.org/problem?id=2771

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5517   Accepted: 2322

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

 
看到题意我笑了。
题意:人和人之间如果满足4个条件的任意一条就不会成为夫妻,给定一些人,问从中最多能选多少人且保证任意两人不可能成为夫妻。
分析:由于条件之一是性别相同则不能成为夫妻。我们根据性别把人划分为两个集合,每个人是一个结点构成二分图,若两个人可能成为夫妻则连一条边。然后最大独立集 = 顶点数 - 最大匹配数。
#include <stdio.h>
#include <string.h>
#include <math.h> bool maps[][];
bool use[];
int match[]; struct Stu {
int h;
char sex[];
char music[];
char sport[];
}mans[],womans[]; int nman,nwoman; bool judge(Stu a,Stu b)
{
if(strcmp(a.music,b.music)) return false;
if(!strcmp(a.sport,b.sport)) return false;
if(fabs((a.h-b.h)*1.0)>) return false;
return true;
} bool DFS(int u)
{
for(int i=;i<nwoman;i++)
{
if(!use[i]&&maps[u][i])
{
use[i] = true;
if(match[i]==-||DFS(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(maps,false,sizeof(maps));
memset(match,-,sizeof(match));
int num;
nman = ;
nwoman = ;
scanf("%d",&num);
for(int i=;i<num;i++)
{
int height;
char sex[];
char musics[];
char sports[];
scanf("%d%s%s%s",&height,sex,musics,sports);
if(sex[]=='M')
{
mans[nman].h = height;
strcpy(mans[nman].sex,sex);
strcpy(mans[nman].music,musics);
strcpy(mans[nman++].sport,sports);
}
if(sex[]=='F')
{
womans[nwoman].h = height;
strcpy(womans[nwoman].sex,sex);
strcpy(womans[nwoman].music,musics);
strcpy(womans[nwoman++].sport,sports);
}
} for(int i=;i<nman;i++)
{
for(int j=;j<nwoman;j++)
{
if(judge(mans[i],womans[j]))
maps[i][j] = true;
}
} int ans = ;
for(int i=;i<nman;i++)
{
memset(use,false,sizeof(use));
if(DFS(i))
ans++;
}
printf("%d\n",num-ans); }
return ;
}

Poj(2771),最大独立集的更多相关文章

  1. poj 2771 最大独立集

    这道题又无耻的抄袭了别人的代码. 刚开始以为是最大匹配,把条件不相符的人连一起,然后求最大匹配,感觉麻烦,然后看了别人的解题报告,是把相符的人连一起,然后减去,其实就是最大独立集. 最大独立集=|G| ...

  2. POJ 2771 最大独立集 匈牙利算法

    (为什么最大独立集的背景都是严打搞对象的( _ _)ノ|壁) 思路:匈牙利算法 没什么可说的-- // by SiriusRen #include <cstdio> #include &l ...

  3. poj——2771 Guardian of Decency

    poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   ...

  4. POJ 2771 Guardian of Decency 【最大独立集】

    传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  5. POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

    题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...

  6. POJ 2771 二分图(最大独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5244   Accepted: 21 ...

  7. Guardian of Decency POJ - 2771 【二分匹配,最大独立集】

    Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...

  8. poj 2771 Guardian of Decency 解题报告

    题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距   ...

  9. POJ 2771 Guardian of Decency

    http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40  ②同性 ③喜欢 ...

随机推荐

  1. 为benchmarksql的PostgreSQL java驱动进行升级

    为benchmarksql的PostgreSQL java驱动进行升级[root@minion1 benchmarksql-4.1.0]# wget https://jdbc.postgresql.o ...

  2. ActionController::InvalidAuthenticityToken 解决办法(第二种尤其有效)

    第一种:  Ror代码 class FooController < ApplicationController       protect_from_forgery :except => ...

  3. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  4. sqlserver 存储过程 以及统计整个数据库数据

    drop proc test 删除存储过程 go  用于在 SSMS 和 SQLCMD 中将其之前的 T-SQL 语句作为一个批处理提交给 SQL Server 实例.GO 不是 T-SQL 语句,只 ...

  5. Node.js Express 获取request原始数据

    app.use(bodyParser.json());客户端请求接口时如果指名请求头类型 为Content-Type=application/jsonbodyParser 会自动将 body 里的 j ...

  6. 线性表基本维护[ACM]

    #include "iostream" #include "string" using namespace std; typedef struct node{ ...

  7. java post请求

    package com.jfbank.loan.intf.util; import java.io.IOException;import java.util.ArrayList;import java ...

  8. Script to compile invalid objects in DB

    REM: Script to compile invalid objects in DB after refreshing REM: REM:***************************** ...

  9. ionic之AngularJS扩展 移动开发(视图导航一)

    目录: 内联模板 : script 路由机制 : 状态机 导航视图 : ion-nav-view 模板视图 : ion-view 导航栏 : ion-nav-bar 回退按钮 : ion-nav-ba ...

  10. JNI 回调小记

    javah在eclipse中设置参数:location(javah.exe的位置)working dir(${project_loc}/src) -classpath .;./classes -d $ ...