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 ②同性 ③喜欢 ...
随机推荐
- 卓尼斯ZT-180评測
卓尼斯ZT-180评測 ——正在出差途中,用10”上网本发帖,没有拍照,且写得冲忙,不妥之处见谅. 一.採购 1.因外出旅游,不想带那台14"笔记本,所以想买一台平板电脑.当时,选择的 ...
- JqueryUI-2
本文在于巩固基础 jQuery UI Widgets jQuery UI Widgets控件-Accordion <!DOCTYPE html> <html> <head ...
- Android开发环境的搭建之(五)DDMS的测试
启动DDMS(Dalvik Debug Monitor Service) 测试Voice通话功能 左图Device选择MyPhone[Emulator- 5554],右图选择Emulator Cont ...
- jQuery ZeroClipboard中Flash定位不准确的解决方案
转自波斯马,原文地址<jQuery ZeroClipboard中Flash定位不准确的解决方案> jQuery ZeroClipboard支持在多种浏览器中复制内容到剪贴板,IE.Fire ...
- Oracle触发器trigger3利用时间限制用户输入
--触发器的应用限制用户写入 --具体功能:在写入一个表之前,限制必须要在周一到周5和工作时间8:00~18:00 create or replace trigger tri3 before inse ...
- hibernate-annotation CascadeType.PERSIST不起作用的解决方法
有如下两个实体类 , Student和Grade 为多对一关系. Student.java @Entity public class Student { private Integer id; pri ...
- java接口的理解
接口的最主要的作用是达到统一访问,就是在创建对象的时候用接口创建,[接口名] [对象名]=new [实现接口的类],这样你像用哪个类的对象就可以new哪个对象了,不需要改原来的代码,就和你的USB接口 ...
- undo_retention:确定最优的撤销保留时间
使用下面的公式来计算undo_retention参数的值: undo_retention=undo size/(db_block_size * undo_block_per_sec) 可以通过提交下面 ...
- ubuntu连接无线网
我的ubuntu是因为没有安装无线网卡驱动,首先查看网卡型号 但是我在Broadcom官网上没有找到BCM43142的驱动. 通过谷歌后发现通过安装bcwl-kernel-source来解决这个问题, ...
- test for randomness