题目链接:https://vjudge.net/problem/UVALive-3415

题解:

题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花。即求出最大独立集。

1.因为性别有男女之分,所以此题的模型是个天然的二分图。

2.如果两个人之间可能擦出火花(即4条限定都不满足),则在他和她之间连一条边。

3.用匈牙利算法求出最小覆盖点数(即最大匹配数),然后最大独立集的元素个数就是总结点数减去最小覆盖点数。

4.为何:最大独立集 = 总体 - 最小覆盖点集  ?

答:最大独立集也可以这么理解:在二分图中, 删去尽可能少的点, 使得剩下的点互相独立,即没有边的存在。而我们又知道,最小覆盖点集覆盖掉了所有的边,当我们把这些点都删去了,就不存在边了。而又因为是“最小”覆盖点集,即删除了最少的点,而得到了最大独立集。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = 1e3+; int n;
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN]; struct Node
{
int h;
string sex, music, sport;
}student[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} bool judge(Node x, Node y)
{
return (abs(x.h-y.h)<= && x.sex!=y.sex
&& x.music==y.music && x.sport!=y.sport);
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
cin>>student[i].h>>student[i].sex>>student[i].music>>student[i].sport; memset(M, , sizeof(M));
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
if(i!=j && judge(student[i], student[j]))
M[i][j] = ; int cnt = hungary()/;
printf("%d\n", n-cnt);
}
}

UVALive3415 Guardian of Decency —— 最大独立集的更多相关文章

  1. UVAlive3415 Guardian of Decency(最大独立集)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34831 [思路] 二分图的最大独立集. 即在二分图中选取最多的点, ...

  2. UVALive-3415 Guardian of Decency (最大独立集)

    题目大意:一个老师要带一些学生去春游,但是要带的学生中任意两个人都满足下面四个条件中的至少一个:1.性别相同:2.身高差大与40公分:3.最喜欢的音乐类型不同:4.最喜欢的体育运动相同.问老师最多能带 ...

  3. Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游

    /** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...

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

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

  5. Guardian of Decency(二分图)

    Guardian of Decency Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

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

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

  7. uva 12083 Guardian of Decency (二分图匹配)

    uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...

  8. poj——2771 Guardian of Decency

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

  9. UVALive 3415 Guardian of Decency(二分图的最大独立集)

    题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...

随机推荐

  1. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  2. loadrunner协议开发

    可以参考loadrunner自带的VuGen Guild文档,里面详细描述了所有协议的录制和开发内容

  3. Python中input()和raw_input()函数的区别

    问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...

  4. POJ2455 Secret Milking Machine【二分,最大流】

    题目大意:N个点P条边,令存在T条从1到N的路径,求路径上的边权的最大值最小为多少 思路:做了好多二分+最大流的题了,思路很好出 二分出最大边权后建图,跑dinic 问题是....这题是卡常数的好题! ...

  5. hdu 2795线段树

    #include<stdio.h> #define N 200005 int h,w,n; struct node { int x,y,max; }a]; int mmax(int e,i ...

  6. COdevs 1251 括号

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 计算乘法时,我们可以添加括号,来改变相乘的顺序,比如计算X1, X2, X3, X4 ...

  7. MYSQL常见运算符和函数【重要】

    字符函数 (1)CONCAT():字符连接 SELECT CONCAT(‘IMOOC’,’-‘,’MySQL’);//IMOOC-MySQL SELECT CONCAT (first_name,las ...

  8. msp430入门编程50

    msp430中项目编程套路 msp430入门编程 msp430入门学习

  9. django学习之- modelForm

    ModelForm(耦合很强) 可以实现 1:数据库操作 2:数据验证 使用地方:1:小型项目,2:自定制jdango admin 功能: 1:可以生成html标签:class Meta... 2:m ...

  10. python学习之-- redis模块基本介绍

    数据缓存系统: 1:mongodb:是直接持久化,直接存储于硬盘的缓存系统 2:redis: 半持久化,存储于内存和硬盘 3:memcache:数据只能存储在内存里的缓存系统关于memcache 学习 ...