HDU 2444 The Accomodation of Students(推断是否是二分图)
题意: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(推断是否是二分图)的更多相关文章
- hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU——2444 The Accomodation of Students
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
随机推荐
- [Android] 更改关联的源码路径
右击选中工程 → Java Build Path → Libraries → Android 4.1.2 → 点开android.jar → 选中Source attachment → Edit,即可 ...
- WCF技术剖析之二十一: WCF基本的异常处理模式[上篇]
原文:WCF技术剖析之二十一: WCF基本的异常处理模式[上篇] 由于WCF采用.NET托管语言(C#和NET)作为其主要的编程语言,注定以了基于WCF的编程方式不可能很复杂.同时,WCF设计的一个目 ...
- [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传
原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...
- 基于visual Studio2013解决面试题之1105字符串压缩
题目
- 再淡spring jdbc 连接池断开重连设置
先看一段错误日志: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConne ...
- 1数组的join方法
function log(e) { console.log(e) } 有时候写console.log太长了,所以会自己写个这样的函数省去写console的步骤. 数组的join方法可以把一个数组按照j ...
- 一个问题:关于类型转换Type Cast(汇编讲解 as 语法)
问题如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
- STM32的FSMC总线复用调试笔记
调试FSMC总线复用模式时主要遇到以下几点: 1.寄存器的配置,首先注意使能地址数据复用,其次要存储器类型选择FSMC_MemoryType_NOR,否则出现不了NADV信号. FSMC_NORSRA ...
- [置顶] Objective-C ,/,ios,/iphone开发基础:协议(protocol)
protocol协议时为了补充Objective-C 只能单继承的缺陷而增加的一个新功能.Objective-C重所有的方法都是虚方法,所以在oc重也就没有关键字 virtual一说,有了协议可以补充 ...
- 原始的js代码和jquery对比
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript ...