题目链接

题意:n个学生,m对关系,每一对互相认识的能住一个房间。问否把这些学生分成两组,要求每组的学生都互不认识。求最多须要多少个房间。

能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用染色法

把初始点染成黑色,然后与之相连的染成白色,反复,使路径黑白相间,

假设当前点的颜色和与他相连点的颜色同样时,则说明这个图不是二分图

求最多须要多少个房间?也就是求最大匹配数。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define init(a) memset(a,0,sizeof(a))
#define PI acos(-1,0)
using namespace std;
const int maxn = 310;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
const int N = 50010;
int ma[maxn][maxn];
int line[maxn],color[maxn];
bool vis[maxn];
int k,n,m;
bool flag;
int DFS(int u)
{
for(int v = 1;v<=n;v++)
{
if(ma[u][v]==1 && !vis[v])
{
vis[v] = 1;
if(line[v]==-1 || DFS(line[v]))
{
line[v] = u;
return 1;
}
}
}
return 0;
}
void dfs(int u,int st)
{
if(flag == false)
return ;
for(int v = 1;v <= n;v++)
{
if(ma[u][v])
{
if(!color[v])
{
color[v]= -st;//黑染白 / 白染黑
dfs(v,color[v]);
}
else if(color[v]==st)
{
flag = false;
return;
} }
} }
bool judge()
{
flag = true;
color[1] = 1 ;//1代表黑色,-1代表白色
dfs(1,1) ;
return flag;
}
int K_M()
{
int ans = 0;
memset(line,-1,sizeof(line));
for(int i = 1;i<=n;i++)
{
init(vis);
ans += DFS(i);
}
return ans;
}
int main()
{
int a,b;
while(scanf("%d%d",&n,&m)!=EOF)
{
init(ma);
memset(color,0,sizeof(color));
for(int i = 1;i<=m;i++)
{
scanf("%d%d",&a,&b);
ma[a][b] = 1;
}
if(!judge())
{
puts("No");
continue;
}
int ans = K_M();
printf("%d\n",ans);
}
return 0;
}

HDU 2444 The Accomodation of Students(推断是否是二分图)的更多相关文章

  1. hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配

    此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...

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

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

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

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

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

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

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

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

  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 - [二分图判断][匈牙利算法模板]

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

随机推荐

  1. [poj 1265]Area[Pick定理][三角剖分]

    题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...

  2. Swift - 异步加载各网站的favicon图标,并在单元格中显示

    下面是一个简单的应用,表格视图的各个单元格自动异步加载各个网站的favicon图标,并显示出来. 主要是复习下如何自定义单元格,单元格中图片的异步加载,以及didSet的用法. 效果图如下: 操作步骤 ...

  3. Spring Cloud Config

    Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...

  4. 列举一些常见的Python HTTP服务器

    要使 Python 写的程序能在 Web 上被访问,还需要搭建一个支持 Python 的 HTTP 服务器.下面列举一些常见的 Python HTTP 服务器,以及它们目前的大致发展情况,以便用户的对 ...

  5. HTML文档类型DTD与浏览器怪异模式

    虽然在兼容IE6时候经常会注意到两个模式的区别,但是系统的理解起来,还没有认真总结过.看了一些网上的资料.结合自己的理解汇总了一下,放在这里备忘并分享给大家. 浏览器从服务端获取网页后会根据文档的DO ...

  6. linux命令:find

    先上例子: find ./*_src -type f | xargs grep -ils "date" 在指定的那些文件夹下面,递归寻找包含“date” 字符串的普通文件. fin ...

  7. 来自中油瑞飞的SQL笔试题20131202

    1.有三张表,用户表,用户角色表,角色表, 使用sql显示如下内容: 用户ID,用户名,超级管理员,录入员,会计 也就是角色用逗号分隔. 解: 1.填充数据到表User select * from [ ...

  8. Expert for SQL Server 诊断系列

    Expert for SQL Server 诊断系列 Expert 诊断优化系列------------------锁是个大角色   前面几篇已经陆续从服务器的几个大块讲述了SQL SERVER数据库 ...

  9. ASP.NET Core 中文文档

    ASP.NET Core 中文文档 翻译计划 五月中旬 .NET Core RC2 如期发布,我们遂决定翻译 ASP.NET Core 文档.我们在 何镇汐先生. 悲梦先生. 张仁建先生和 雷欧纳德先 ...

  10. 基于visual Studio2013解决面试题之0708字符串全排列

     题目