http://acm.hdu.edu.cn/showproblem.php?pid=2444

题意:给出边,判断这个是否是一个二分图,并求最大匹配。

思路:先染色法求出是否是一个二分图,然后再匈牙利求出最大匹配。注意输出是“No"而不是”NO“!!!

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct node
{
int nxt, v;
}edge[*];
int head[], tot;
int col[];
bool vis[];
int match[]; void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} bool check(int u,int c)
{
col[u] = c;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(col[v] == c) return false;
if(col[v] == && !check(v, -c)) return false;
}
return true;
} bool dfs(int u)
{
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
} int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)) {
memset(match, -, sizeof(match));
memset(head, -, sizeof(head));
memset(col, , sizeof(col));
tot = ;
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
bool f = ;
for(int i = ; i <= n; i++) {
if(col[i] == ) {
if(!check(i, )) {
f = ;
puts("No");
break;
}
}
}
if(!f) continue;
int sum = ;
for(int i = ; i <= n; i++) {
memset(vis, , sizeof(vis));
sum += dfs(i);
}
printf("%d\n", sum / );
}
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 ...

  10. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

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

随机推荐

  1. UISegment属性

    1.segmentedControlStyle 设置segment的显示样式. typedefNS_ENUM(NSInteger, UISegmentedControlStyle) { UISegme ...

  2. Java NIO学习(一)

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...

  3. 王家林 大数据Spark超经典视频链接全集[转]

    压缩过的大数据Spark蘑菇云行动前置课程视频百度云分享链接 链接:http://pan.baidu.com/s/1cFqjQu SCALA专辑 Scala深入浅出经典视频 链接:http://pan ...

  4. Sping中的事务配置

    关于Spring的事务配置,主要的配置文件如下(使用了C3P0连接池): <?xml version="1.0" encoding="UTF-8"?> ...

  5. firefox中 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因

    最近在调试复选框的应用,在ie没有问题,考虑到兼容性,试试了firefox,遇到了问题. 复选框绑定了click事件,点一次选中,再点击取消选中,依次类推.这个功能在ie中没问题,但是在firefox ...

  6. paper 58 :机器视觉学习笔记(1)——OpenCV配置

    开始学习opencv! 1.什么是OpenCV OpenCV的全称是:Open Source Computer Vision Library.OpenCV是一个基于(开源)发行的跨平台计算机视觉库,可 ...

  7. 关于 VS 无法转到定义和无法转到使用的问题

    今天提交完代码以后突然发现  咦  怎么F12 .点击右键的方法都不能转到定义了    转到引用 也提示  没有发现   重启VS  还是不行 .去找王晓  他也不清楚(其实我知道 他应该也不清楚  ...

  8. java 项目打包流程速记

    1.与资源库同步 2.[解决冲突] --可能没有这一步 3.合并标记 4.清除一下项目-- clean 5.打包: run As -->Maven install 6.去服务备份原包,下载服务 ...

  9. RF前端

    加入LTE之后的多模多频需求: 在加入LTE后,不但要求终端在多模的基础上增加LTE工作频段,而且还要增加可以确保用户实现国际漫游的频段.然而 全球分配的LTE频段较多且较离散.   频段 上行工作频 ...

  10. Eclipse字符集设置方式

    默认的字符集是GBK 1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text fi ...