poj 2771 最大独立集
这道题又无耻的抄袭了别人的代码。
刚开始以为是最大匹配,把条件不相符的人连一起,然后求最大匹配,感觉麻烦,然后看了别人的解题报告,是把相符的人连一起,然后减去,其实就是最大独立集。
最大独立集=|G|-最大匹配。
首先先把性别分开,因为同性不能成为couple,然后把符合条件的异性连一起,然后就是最大匹配了。
#include<stdio.h>
#include<string.h>
#define maxn 505
#define maxl 106 struct Person
{
int h;
char music[maxl];
char sport[maxl];
} male[maxn], female[maxn]; int uN, vN, n;
bool g[maxn][maxn];
int visit[maxn],match[maxn];
int abs(int x)
{
if(x<)
return -x;
return x;
}
int DFS(int u)
{
for(int i=;i<vN;i++) if(!visit[i]&&g[u][i])
{
visit[i]=;
if(match[i]==- || DFS(match[i]))
{
match[i]=u;
return ;
}
}
return ;
} int MaxMatch()
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<uN;i++)
{
memset(visit,,sizeof(visit));
if(DFS(i)) ans++;
}
return ans;
} void input()
{
scanf("%d", &n);
uN = vN = ;
for (int i = ; i < n; i++)
{
int a;
char sex[];
scanf("%d%s", &a, sex);
if (sex[] == 'M')
{
male[uN].h = a;
scanf("%s%s", male[uN].music, male[uN].sport);
uN++;
}
else
{
female[vN].h = a;
scanf("%s%s", female[vN].music, female[vN].sport);
vN++;
}
}
} bool couple(int a, int b)
{
if (abs(male[a].h - female[b].h) > )
return false;
if (strcmp(male[a].music, female[b].music))
return false;
if (strcmp(male[a].sport, female[b].sport) == )
return false;
return true;
} void make()
{
memset(g, , sizeof(g));
for (int i = ; i < uN; i++)
for (int j = ; j < vN; j++)
if (couple(i, j))
g[i][j] = true;
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
input();
make();
printf("%d\n", n - MaxMatch());
}
return ;
}
poj 2771 最大独立集的更多相关文章
- POJ 2771 最大独立集 匈牙利算法
(为什么最大独立集的背景都是严打搞对象的( _ _)ノ|壁) 思路:匈牙利算法 没什么可说的-- // by SiriusRen #include <cstdio> #include &l ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- Poj(2771),最大独立集
题目链接:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K To ...
- POJ 2771 Guardian of Decency 【最大独立集】
传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Tot ...
- POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)
题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...
- POJ 2771 二分图(最大独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5244 Accepted: 21 ...
- Guardian of Decency POJ - 2771 【二分匹配,最大独立集】
Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...
- poj 2771 Guardian of Decency 解题报告
题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距 ...
- POJ 2771 Guardian of Decency
http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40 ②同性 ③喜欢 ...
随机推荐
- Android EditText限制输入一些固定字符的属性
android:digits="abcdefghijklmnopqrstuvwxyz1234567890" 仅仅能输入这些
- 卓尼斯ZT-180评測
卓尼斯ZT-180评測 ——正在出差途中,用10”上网本发帖,没有拍照,且写得冲忙,不妥之处见谅. 一.採购 1.因外出旅游,不想带那台14"笔记本,所以想买一台平板电脑.当时,选择的 ...
- 第001篇——C#学习计划开启
大年三十了,选在今天开启Blog,就是为了克服拖延症! Windows桌面程序,多年的执念,到现在一直不会写,再拖拉谁知道又要拖几年? 特此立下目标: 基本掌握C# winform 半年内可以做出一些 ...
- vbox下安装 linux 64 bit出现“kernel requires an x86_64 cpu
今天在vbox下安装linux 64bit出现"kernel requires an x86_64 cpu, but only detected "的错误,网上有很多文章介 ...
- 【视图】实时库存【SSKC】
select A.pluno,A.pluname,A.qty,CASE WHEN b.QTY IS NULL THEN 0 ELSE B.QTY END AS XSQTY ,case when ...
- 如何让tableView展示数据
设置数据源对象 self.tableView.dataSource = self; 数据源对象要遵守协议 @interface ViewController () <UITableViewDat ...
- C# 启动EXE文件及带启动参数EXE
(一).先制作一个带启动参数的EXE文件. 步骤: 1.定义全局私有变量:private string[] s = new string[1]; //这里为了简单起见,只做一个参数 2. 在窗体的 ...
- TCP/IP详解之:IGMP和DNS
第13章 IGMP:Internet组管理协议 IGMP用于支持主机和路由器进行多播: IGMP是IP层的一部分,IGMP报文通过IP数据报进行传输: IGMP报文长度为固定8 Byte: 报文中,I ...
- [置顶] 学习鸟哥的Linux私房菜笔记(6)——过滤器、输入输出及管道
一.过滤器 Linux中的应用工具分为三种: 交互工具 过滤器 编辑器 能够接受数据,过滤再输出的工具,称之为过滤器 对过滤器和进程,存在着输入源与输出对象 二.输入.输出.重定向 输入:过滤器的数据 ...
- Android Activity之 setContentView()总结
从一开始hello world的第一个安卓应用开始,Activity 自动生成,布局自动生成,直接修改布局,在Activity中,findviewById()找到view,然后处理相应的业务逻辑即可, ...