博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2771 Guardian of Decency(二分匹配,最大独立集)
阅读量:7008 次
发布时间:2019-06-28

本文共 2617 字,大约阅读时间需要 8 分钟。

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 4071   Accepted: 1702

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

2435 M classicism programming0 M baroque skiing43 M baroque chess30 F baroque soccer827 M romance programming194 F baroque programming67 M baroque ping-pong51 M classicism programming80 M classicism Paintball35 M baroque ping-pong39 F romance ping-pong110 M romance Paintball

Sample Output

37

Source

 
 
 
 
二分匹配。
主要是建图方法。
题目要求是选出两两之间满足四个条件之一的人。
我们建图应该反过来建。
给不满足四个条件的人一条边。就转换成求最大独立集的问题。

二分图最大独立集=顶点数-二分图最大匹配

独立集:图中任意两个顶点都不相连的顶点集合。

 

#include
#include
#include
#include
#include
using namespace std;/* **************************************************************************//二分图匹配(匈牙利算法的DFS实现)//初始化:g[][]两边顶点的划分情况//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配//g没有边相连则初始化为0//uN是匹配左边的顶点数,vN是匹配右边的顶点数//调用:res=hungary();输出最大匹配数//优点:适用于稠密图,DFS找增广路,实现简洁易于理解//时间复杂度:O(VE)//***************************************************************************///顶点编号从0开始的const int MAXN=510;int uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{ int v; for(v=0;v

 

转载地址:http://khvtl.baihongyu.com/

你可能感兴趣的文章
微信小程序把玩(二十八)image组件
查看>>
OpenCV+OpenGL 双目立体视觉三维重建
查看>>
R12.2 克隆完成之后weblogic AdminSErver无法启动
查看>>
Unicode字符编码标准
查看>>
云计算就像是产业链的重新组合
查看>>
第三代北斗芯片发布 2020年北斗计划向全球提供服务
查看>>
阿里巴巴集团CTO王坚:云计算让理想平等
查看>>
《中国人工智能学会通讯》——11.30 深度迁移学习
查看>>
Dell EMC扩充数据保护产品线 Data Domain增强云分层功能
查看>>
美柚社区精选:贴心宝妈的八大育儿经验
查看>>
走进医疗明星企业之北京天坛普华医院
查看>>
一点资讯电影贴片广告以假乱真
查看>>
曙光出炉“数据中国加速计划”
查看>>
中国制造2025新机遇 机器视觉行业爆发
查看>>
中国工商银行阿根廷分行用数据运营展现本地特色
查看>>
使用闪存存储的优势与注意事项
查看>>
《并行计算的编程模型》一2.2 GASNet概述
查看>>
彷徨中的成长-记一个文科生的IT成长过程
查看>>
OpenStack正重新定义数据解决方案商业模式
查看>>
大数据会撒谎?如何戳破大数据的谎言
查看>>