题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数。

思路:把男女分为两部分,接下来就是二分图的匹配问题。把能成为一对的之间连边,然后求出最大匹配。题目要求的是最大独立数。

最大独立数=顶点数-最大匹配数

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define MAXN 1024 struct person{
int h;
char music[];
char sport[];
}male[MAXN],female[MAXN]; int n,m,k,x,y,pre[MAXN];
//二分图中X集和Y集的节点数各为n、m,边数为k;匹配边集为pre,其中节点i所在的匹配边为(pre[i],i)
bool v[MAXN],a[MAXN][MAXN];
//设二分图相邻矩阵为a,Y集合中节点的访问标志为v,若Y集合中的节点j已访问,则v[j]=true bool dfs(int i){//判断以X集合中的节点i为起点的增广路径是否存在
int j;
for(j=; j<m; j++){
if(!v[j]&&a[i][j]){//搜索所有与i相邻的未访问点
v[j]=;//访问节点j
if(pre[j]==-||dfs(pre[j])){
//若j的前驱是未盖点或者存在由j的前驱出发的增广路径,则设定(i,j)为匹配边,返回成功标志
pre[j]=i;
return true;
}
}
}
return false;//返回失败标志
} int main(){
int t,num,h;
char sex[];
int i,j,ans;
scanf("%d",&t);
while(t--){
memset(a,,sizeof(a));//二分图的相邻矩阵初始化
memset(pre,-,sizeof(pre));//匹配边集初始化为空
n=m=;
scanf("%d",&num);
while(num--){
scanf("%d%s",&h,sex);
if(sex[]=='M'){
male[n].h=h;
scanf("%s%s",male[n].music,male[n].sport);
++n;
}
else{
female[m].h=h;
scanf("%s%s",female[m].music,female[m].sport);
++m;
}
}
for(i=;i<n;++i){
for(j=;j<m;++j){
if(abs(male[i].h-female[j].h)<=&&strcmp(male[i].music,female[j].music)==&&strcmp(male[i].sport,female[j].sport)!=)
a[i][j]=;
}
}
ans=;//匹配边数初始化为0
for(i=; i<n; i++){//枚举X集的每个节点
memset(v,,sizeof(v));//设Y集合中的所有节点的未访问标志
if(dfs(i)) ans++;//若节点i被匹配边覆盖,则匹配边数+1
}
printf("%d\n",n+m-ans);
}
return ;
}

poj 2771 Guardian of Decency(最大独立数)的更多相关文章

  1. poj——2771 Guardian of Decency

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

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

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

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

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

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

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

  5. POJ 2771 Guardian of Decency

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

  6. poj 2771 Guardian of Decency 解题报告

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

  7. UVA 12083 POJ 2771 Guardian of Decency

    /* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...

  8. POJ 2771 Guardian of Decency(求最大点独立集)

    该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...

  9. POJ——T2271 Guardian of Decency

    http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   A ...

随机推荐

  1. 选取第K大数的快速选择算法和注意事项

    快速选择算法,是一种能在大致O(N)的时间内选取数组中第k大或者k小的算法.其基本思路与快速排序算法类似,也是分治的思想. 其实这个算法是个基础算法,但是不常用,所以今天编的时候错了POJ2388,才 ...

  2. python多线程(一)

    原文:http://www.pythonclub.org/python-basic/threading 一.python多线程thread和threading实现 python是支持多线程的,并且是n ...

  3. android 长按弹出菜单,复制,粘贴,全选

    <!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/ap ...

  4. xgboost调参

    The overall parameters have been divided into 3 categories by XGBoost authors: General Parameters: G ...

  5. Python机器学习--降维

    主成分分析(PCA) 测试 # -*- coding: utf-8 -*- """ Created on Thu Aug 31 14:21:51 2017 @author ...

  6. fabricJs使用系列(一)

    Get the canvas object while using fabric js I'm using Fabric.js and I've created a fabric canvas obj ...

  7. Mqtt协议IOS端移植3

    ServerMqFramework.h #import "MqttFramework.h" @interface ServerMqFramework : MqttFramework ...

  8. LInux查看CPU状态

    1.htop 2.top 内容解释: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占用的虚拟内存 RES:进程占用的物理内存 SH ...

  9. Intel graphics processing units

    http://en.wikipedia.org/wiki/Comparison_of_Intel_graphics_processing_units Comparison of Intel graph ...

  10. 菜鸟系列之C/C++经典试题(三)

    设计包括min函数的栈 题目:定义栈的数据结构,要求加入一个min函数,可以得到栈的最小元素.要求函数min.push以及pop的时间复杂度都是O(1). 分析:这是2006年google的一道面试题 ...