这是一个基础的二分图,题意比较好理解,给出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(二分图判定+最大匹配)的更多相关文章

  1. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  2. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  3. 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 ...

  4. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  5. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

  6. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. 《Windows驱动开发技术详解》之派遣函数

    驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的.用户模式下所有对驱动程序的I/O请求,全部由操作系统转化为一个叫做IRP的数据结构,不同的IRP数据会被“派遣”到不同 ...

  2. Windows server 2008搭建php运行环境

    下载php组件包 首先到http://windows.php.net/download/下载你需要的php版本,这里我下载的是php5.3. 下面解压php组件 包到磁盘上. 安装Microsoft ...

  3. H5的新应用-获取用户当前的地理坐标

    ------------------------------ <script type="text/javascript">                       ...

  4. spring Stack Overflow

    1. ApplicationContext 不关闭,资源泄露问题: Spring ApplicationContext - Resource leak: 'context' is never clos ...

  5. 用js 将long类型转换成日期格式

    //扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+": this.getMont ...

  6. nefu 196 让气球飞吧

    description 国际大学生程序设计竞赛已经发展成为最具影响力的大学生计算机竞赛,ACM-ICPC以团队的形式代表各学校参赛,每队由3名队员组成,一个队每做出来一个题该队就会获得该题对应颜色的气 ...

  7. nginx配置错误

    重启nginx:sudo /usr/local/nginx/sbin/nginx -s reload 出现错误提示:nginx: [emerg] unknown directive "if& ...

  8. grub修复

    sudo mount /dev/sda1 /boot/efi sudo modprobe efivarfs sudo grub-install /dev/sda sudo update-grub su ...

  9. ActiveMQ-CPP编译

    1.activemq-cpp下载地址: http://activemq.apache.org/cms/download.html 2.相关依赖库 http://mirrors.hust.edu.cn/ ...

  10. 转 Oracle12c/11个 Client安装出现"[INS-30131]"错误“请确保当前用户具有访问临时位置所需的权限”解决办法之完整版

    错误分析:安装时exe会自动解压到C:\Users\Administrator\AppData\Local\Temp再进行安装,当文件夹权限不足时就会拒绝安装程序的访问: 第一步:  在win+R输入 ...