The Accomodation of Students---hdu2444(二分图,最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444
题意:有n个学生,m个关系,但是如果a认识b,b认识c,但是a不一定认识c;
求能不能把这n个人分成两个房间,每个房间的人互相都不认识,这就是让我们判断是不是二分图了,可以用涂色法来进行判断;
如果是二分图则求出来最大匹配是多少;
可以把两个房间的人分成两个数组也可以不分让结果除以2就行了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 210 int maps[N][N], vis[N], used[N], n, ans, p, q;
int color[N];///利用涂色法判断是否为二分图; bool Find(int u)
{
for(int i=; i<=n; i++)
{
int x = i;
if(!vis[x] && maps[u][x])
{
vis[x] = ;
if(!used[x] || Find(used[x]))
{
used[x] = u;
return true;
}
}
}
return false;
} int main()
{
int m;
int x, y, flag;
while(scanf("%d %d", &n, &m)!=EOF)
{
flag = ;
p = q = ;
memset(color, , sizeof(color));
memset(maps, , sizeof(maps));
memset(used, , sizeof(used));
for(int i=; i<=m; i++)
{
scanf("%d%d", &x, &y);
maps[x][y] = maps[y][x] = ;
if(color[x]== && color[y]==)
{
color[x] = ;
color[y] = -;
}
else if(color[x]!= && color[y]==)
color[y] = -color[x];
else if(color[x]== && color[y]!=)
color[x] = -color[y];
else if(color[x]==color[y] && color[x]!=)
flag=;
}
if(flag==)
{
printf("No\n");
continue;
}
int ans = ;
for(int i=; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans/);///因为最终会存入1-2和2-1,所以要除以二;
}
return ;
}
The Accomodation of Students---hdu2444(二分图,最大匹配)的更多相关文章
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
链接: https://vjudge.net/problem/HDU-2444#author=634579757 题意: There are a group of students. Some of ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- HDU 2444:The Accomodation of Students(二分图判定+匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:给出边,判断这个是否是一个二分图,并求最大匹配. 思路:先染色法求出是否是一个二分图,然后再匈牙利求 ...
随机推荐
- js 补零方法,如果不足位数
var pad = function() { var tbl = []; return function(num, n) { var len = n-num.toString().length; if ...
- 根据多表条件更新表.............. 一条sql语句.............
CREATE TABLE a( NAME INT, age int ); CREATE TABLE b( height INT, weight int ); ,); ,); SELECT * FROM ...
- unicode and utf-8
今晚听同事分享提到这个,简单总结下. Unicode字符集 Unicode的出现是因为ASCII等其他编码码不够用了,比如ASCII是英语为母语的人发明的,只要一个字节8位就能够表示26个英文字母了, ...
- c++ 参赛设置
void report(LogWriter& lw); 代表引用原对象 void report(LogWriter lw); 代表重新拷贝构造一个对象
- ThinkPHP Mongo驱动update方法支持upsert参数
Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...
- win8 学习笔记二 输出日志
Win8 Store App的日志输出不像Desktop App 那么简单,见 这篇文档(http://www.cnblogs.com/xiaokang088/archive/2011/12/27/2 ...
- ALM在win7/IE8下无法浏览
操作系统WIN7 64位. 安装完ALM后,用IE8打开查看,没有登录界面,提示需要安装东西. 按照提示安装,没有响应,然后到网上查了一下资料: ALM/QC11.0在win8/IE11下无法浏览 页 ...
- Android中文API
Android中文API http://www.android-doc.com/index.html
- 基于Cocos2d-x学习OpenGL ES 2.0系列——纹理贴图(6)
在上一篇文章中,我们介绍了如何绘制一个立方体,里面涉及的知识点有VBO(Vertex Buffer Object).IBO(Index Buffer Object)和MVP(Modile-View-P ...
- 在create-react-app的脚手架里面使用scss
之前用vue-cli脚手架的时候,只需要引进sass需要的依赖包便可以引入scss,但是在create-react-app的时候,发现除了需要引入sass依赖,还需要进行配置: 不管用什么方法进行sa ...