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 studio 怎么运行java
方法/步骤 1.新建一个project,或者如果已经有project的话,那就直接新建一个module.注意选择Java library,然后下一步 2.输入module的一些信息.点击finish ...
- No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC
I'm learning the Spring Framework, and I'm doing the HelloWeb tutorial on tutorialspoint, and I can' ...
- Qt4----子例化QDialog(可扩展对话框的使用)
1.linux下安装Qt4请参考如下博文: Qt4在linux下的安装 2.Qt4工程的创建请参考如下博文: Qt4创建工程的几种方法:linux系统 3.可扩展对话框 通过纯代码的形式,建立工程.点 ...
- sql:oracle, CURSOR
CursorsYou use a cursor to fetch rows returned by a query. You retrieve the rows into the cursor usi ...
- oracle 查询
1. ORACLE查询数据中所存在的表 http://www.cnblogs.com/juddhu/archive/2012/03/22/2411406.html 查询数据库中所有的表: select ...
- C++基础学习笔记----第四课(函数的重载、C和C++的相互调用)
本节主要讲了函数重载的主要概念以及使用方法,还有C和C++的相互调用的准则和具体的工程中的使用技巧. 函数重载 1.基本概念 函数重载就是用同一个函数名来定义不同的函数.使用不同的函数参数来搭配同一个 ...
- 14.10.2 File Space Management 文件空间管理:
14.10.2 File Space Management 文件空间管理: 数据文件 你定义在配置文件形成了InnoDB的系统表空间, 文件是逻辑连接形成表空间,没有条带化使用. 你不能定义你的表在表 ...
- 玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)
一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景 “大数据”作为时下最火热的IT行业的词汇,随之而来的数据仓库.数据分析.数据挖掘等等围绕大数据的商业价值的利用逐渐成为 ...
- linux安装Eclipse c++环境
yum install eclipse yum install eclipse-cdt
- u盘安装ubuntu10.04 server.txt
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...