HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以判断,其次让我们求分在两部分的最大对数,这就是二分图的最大匹配问题,这里数据只有200,所以匈牙利算法求蹭广路径的办法可以解决这个问题,也相对比较容易编写。
另外一开始我链式前向星的数组开小了,G++居然返回超时,后来换了C++才RE,想到数组越界的问题,不得不说这些编译器真傲娇啊~
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
struct EDGE
{
int to,nxt;
}edge[];
int head[],n,m,tot;
void add_edge(int x,int y)
{
edge[tot].to = y;
edge[tot].nxt = head[x];
head[x] = tot++;
}
int clo[];
bool bfs(int id)
{
queue<int>que;
clo[id] = ;
while(!que.empty()) que.pop();
que.push(id);
while(!que.empty())
{
int now = que.front();
que.pop();
for(int i = head[now];i != -;i = edge[i].nxt)
{
int go = edge[i].to;
if(clo[go] == -)
{
clo[go] = (clo[now] ^ );
que.push(go);
}
else if(clo[go] == clo[now]) return false;
}
}
return true;
}
bool is_erfen()
{
memset(clo,-,sizeof(clo));
bool mark = true;
for(int i = ;i <= n;i++)
{
if(clo[i] != -)continue;
mark = bfs(i);
if(mark == false) return false;
}
return mark;
}
int vis[],link[];
bool Find(int x)
{
for(int i = head[x];i != -;i = edge[i].nxt)
{
int now = edge[i].to;
if(vis[now]) continue;
vis[now] = ;
if(link[now] == - || Find(link[now]))
{
link[now] = x;
return true;
}
}
return false;
}
int match()
{
memset(link,-,sizeof(link));
int ans = ;
for(int i = ;i <= n;i++)
{
memset(vis,,sizeof(vis));
if(Find(i))
ans++;
}
return ans / ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
tot = ;
memset(head,-,sizeof(head));
for(int i = ;i < m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
bool flag = is_erfen();
if(!flag)
{
printf("No\n");
}
else printf("%d\n",match());
}
return ;
}
HDU 2444 The Accomodation of Students(二分图判定+最大匹配)的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)
There are a group of students. Some of them may know each other, while others don't. For example, A ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)
题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- 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(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- 利用朴素贝叶斯算法进行分类-Java代码实现
http://www.crocro.cn/post/286.html 利用朴素贝叶斯算法进行分类-Java代码实现 鳄鱼 3个月前 (12-14) 分类:机器学习 阅读(44) 评论(0) ...
- linux下shell命令trap
某些时候,在执行shell脚本(.sh)时,我们并不希望被打断.这时我们要用到trap命令. 例如: 在shell脚本中,忽略“终止”信号 trap ' ' TERM
- Entity Framework技巧系列之十四 - Tip 56
提示56. 使用反射提供程序编写一个OData Service 在TechEd我收到一大堆有关将数据作为OData暴露的问题. 到目前为止你大概知道可以使用数据服务与Entity Framework将 ...
- Entity Framework技巧系列之六 - Tip 20 – 25
提示20. 怎样处理固定长度的主键 这是正在进行中的Entity Framework提示系列的第20篇. 固定长度字段填充: 如果你的数据库中有一个固定长度的列,例如像NCHAR(10)类型的列,当你 ...
- 硬盘安装win8系统方法汇总
从硬盘安装 (推荐)硬盘安装方法一 使用Nt6 hdd installer进行安装,此方法适合XP,vista, Windows 7等系统. 下载Nt6 hdd installer(Win8硬盘安装工 ...
- fszipx.exe
来源:http://www.funduc.com/fszipx.htm 是个免费软件,用于把.zip转化为.exe自解压文件. COPY /B "C:\Tools\FsZipX\FsZipX ...
- Boost的VS开发环境搭建
1. 下载并解压Boost C++ Libs 下载地址: SourceForge:http://sourceforge.net/projects/boost/files/boost/1.48.0/ B ...
- android--屏幕旋转方法总结
在介绍之前,我们需要先了解默认情况下android屏幕旋转的机制: 默认情况下,当用户手机的重力感应器打开后,旋转屏幕方向,会导致当前activity发生onDestroy-> onCreate ...
- 在MyEclipse中运行tomcat报错 严重: Error starting static Resources
严重: Error starting static Resourcesjava.lang.IllegalArgumentException: Document base E:\apache-tomca ...
- string.IsNullOrWhiteSpace
string.IsNullOrWhiteSpace(str) 这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char. ...