http://acm.hdu.edu.cn/showproblem.php?pid=2444
The Accomodation of Students

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u


Description

There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. 
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room. 
Calculate the maximum number of pairs that can be arranged into these double rooms. 
 

Input

For each data set:  The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs. 
Proceed to the end of file. 
 

Output

If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms. 
 

Sample Input

4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
 

Sample Output

No
3
 
题目大意: 有n个人,和m种关系,代表a和b认识。要求将n个人放入到两个集合中,每个集合中的人互不认识,A集合中的人找B集合的人住房间,两个人一间,且两个人必须认识,问你最多能住多少间房间。
 
判断是否为二分图:在无向图G中,如果存在奇数回路,则不是二分图。否则是二分图。
判断回路奇偶性:把相邻两点染成黑白两色,如果相邻两点出现颜色相同则存在奇数回路。也就是非二分图。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 210 int G[N][N], vis[N], used[N];
int m, n, f, c[N]; bool Find(int u)
{
int i;
for(i = ; i <= n ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} void DFS(int x, int color)
{
int i;
for(i = ; i <= n ; i++)
{
if(G[x][i])
{
if(c[i] == )
{
c[i] = -color;//将x相邻的点i染成与其不同的颜色
DFS(i, -color);
}
else if(c[i] == color)//如果相邻两点颜色相同则不是二分图
{
f = ;
return ;
}
}
}
} int main()
{
int i, a, b;
while(~scanf("%d%d", &n, &m))
{
memset(G, , sizeof(G));
memset(c, , sizeof(c));
f = ;
while(m--)
{
scanf("%d%d", &a, &b);
G[a][b] = G[b][a] = ;
}
c[] = ;
DFS(, );//将1号染成黑色
if(f == )
printf("No\n");
else
{
int ans = ;
memset(used, , sizeof(used));
for(i = ; i<= n ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
printf("%d\n", ans / );
}
}
return ;
}

hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)的更多相关文章

  1. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  2. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

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

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

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

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

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

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

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

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

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

  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 Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. Hadoop实战课程

    Hadoop生态系统配置Hadoop运行环境Hadoop系统架构HDFS分布式文件系统MapReduce分布式计算(MapReduce项目实战)使用脚本语言Pig(Pig项目实战)数据仓库工具Hive ...

  2. theos的makefile写法

    theos的makefile写法与其他linux/unix环境下的makefile写法大同小异,但是对于makefile不熟悉的在导入一些dylib或者framework的时候就会变得很蛋疼. 对于f ...

  3. HDU 1548 A strange lift 奇怪的电梯(BFS,水)

    题意: 有一座电梯,其中楼层从1-n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层.楼主在a层,问是否能到达第b层? 思路: 在起点时只能往上走和往下走两个选择,之后的每层都是这样 ...

  4. HDU 1018 Big Number (阶乘位数)

    题意: 给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路: 用对数log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w ...

  5. 优雅地使用CodeIgniter 3之Session类库(1)(转)

    相信无数人在使用CI2的Session类库时,遇到各种的坑,各种抱怨,各种不解.在CI中国论坛能搜到大量关于Session类库的提问,说明要想用 好session类库还是得下一番功夫.本文将先从CI2 ...

  6. openssl rsa 加解密

    <h4>1.openssl进行rsa加密解密</h4>首先介绍下命令台下openssl工具的简单使用:生成一个密钥:<pre lang="c" esc ...

  7. T-SQL备忘(5):查看执行计划

    先理解几个概念:表扫描.聚集索引扫描.聚集索引查找.索引扫描.书签查找. [查看执行计划] 在理解概念之前先得知道如何查看执行计划—Ctrl+L.如下图: 注:SQL Server的执行计划是从右向左 ...

  8. Struts2配置之Struts.properties

    Struts 2框架有两个核心配置文件,其中struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.除此之 外,Struts 2框架还包含     s ...

  9. vs2008破解方法

    前提条件:测试操作系统WIN7 1,解压缩镜像文件 2,找到文件:Setup\setup.sdb,用记事本打开: 3,找到以下项: [Product Key]XPWKC7X98VKQDGM3QWYVG ...

  10. 锋利的jQuery读书笔记---jQuery中Ajax--序列化元素、Ajax全局事件

    serialize()方法: 作用于一个jQuery对象,它能够将DOM元素内容序列化为字符串,用于Ajax请求. <!DOCTYPE html> <html> <hea ...