题目链接

题意: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. 蛋疼的Apple IOS Push通知协议

    简单介绍 Apple Push通知机制事实上非常easy,就是Apple的APNsserver做为中间人,把消息推送到相应的设备上. 一张来自Apple文档的图: 当然,示意图看起来简单,可是另一些实 ...

  2. pomelo研究笔记-RPCclient

    1. mailbox数据收发模块 一个RPC客户端可能同一时候须要调用多个远端(server)提供的服务.在pomelo里每一个server抽象为一个mailbox.先来看看mailbox的实现: v ...

  3. PHP - 接口 - 单一接口

    /* * 接口的使用 */ //定义接口 interface IPerosn{ public function eat(); public function water(); } //定义继承自接口的 ...

  4. 立贴读 《CLR》

    弱弱的说,我要开始读<CLR>这本书了,怕自己不能坚持下来,特立贴监督自己,本来是大牛们涉及的区域,现在好朋友的鼓励下,勇敢的踏入,如有错误,还请各位指正.

  5. [置顶] c#验证码识别、图片二值化、分割、分类、识别

    c# 验证码的识别主要分为预处理.分割.识别三个步骤 首先我从网站上下载验证码 处理结果如下: 1.图片预处理,即二值化图片 *就是将图像上的像素点的灰度值设置为0或255. 原理如下: 代码如下: ...

  6. Writing a ServiceMain Function(使用RegisterServiceCtrlHandler函数)

    The following global definitions are used in this sample. C++   #define SVCNAME TEXT("SvcName&q ...

  7. 基于visual Studio2013解决面试题之1102合并字符串

     题目

  8. 类似QtiPlot的veusz,sigmaplot,pymol

    qtiplot在win下没那么好编译 依赖很多外部包的 scidavis 和 labplot是从他fork出来的 比较接近Origin 可以用这两个 FreeBSD 的 ports 里有直接 cd / ...

  9. View实现涂鸦、撤销以及重做功能

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  10. 基于visual Studio2013解决面试题之0401非递归遍历二叉树

     题目