HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9477 Accepted Submission(s): 4165
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444
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:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
int link[N][N],match[N],check[N],color[N];
int n,m,ans=; inline void init(){
ans=;memset(color,-,sizeof(color));
memset(link,,sizeof(link));memset(match,-,sizeof(match));
} inline int dfs(int x){
for(int i=;i<=n;i++){
if(link[x][i] && !check[i]){
check[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
}
inline bool ok(int x){ //二分图染色
for(int i=;i<=n;i++){
if(link[x][i]){
if(color[i]==-){
color[i]=-color[x];
if(!ok(i)) return false;
}else if(color[i]==color[x]) return false ;
}
}
return true;
}
/*dfs实现 ,连通图直接调用ok(1,0)
inline bool ok(int x,int c){
color[x]=c;
for(int i=1;i<=n;i++){
if(link[x][i]){
if(color[i]==-1){
if(!ok(i,1-color[x])) return false;
}else if(color[i]==color[x]) return false ;
}
}
return true;
}
*/
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
link[a][b]=;
link[b][a]=;
}
bool flag=false ;
for(int i=;i<=n;i++){
if(color[i]==-){
color[i]=;
if(!ok(i)){
flag=true;break;
}
}
}
if(flag){
puts("No");continue;
}
for(int i=;i<=n;i++){
memset(check,,sizeof(check));
if(dfs(i)) ans++;
}
printf("%d\n",ans/);
}
return ;
}
HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)的更多相关文章
- HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- HDU2444 The Accomodation of Students
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU2444 The Accomodation of Students(二分图最大匹配)
有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
/*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...
- hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)
//推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...
- HDU2444 The Accomodation of Students【匈牙利算法】
题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...
- HDU——T 2444 The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
随机推荐
- 手把手教你封装 Vue 组件,并使用 npm 发布
Vue 开发插件 开发之前先看看官网的 开发规范 我们开发的之后期望的结果是支持 import.require 或者直接使用 script 标签的形式引入,就像这样: // 这里注意一下包的名字前缀是 ...
- Tunnel上传遇到字符[NUL]问题
模拟生产环境下数据格式,再现异常情景: Notepad++怎样输入字符[NUL]? 安装 Hex-Editor 插件: HexEditor插件用于在notepad++中查看16进制文件,只需要将此 ...
- zabbix 2.2.2 安装部署
zabbix 2.2.2版本与1.8.3版本安装过程略有不同,下面为实施步骤: 服务端:172.16.1.61 客户端:172.16.1.8 搭建zbbix软件 安装LAMP环境及依赖包 [root@ ...
- 一次大量TIME_WAIT和Recv-Q 堵塞问题排查思路
记录一下周末出现问题~ 仅自己摘记不做任何参考. 第一天故障: 现象: 公司销售群和售后群炸了,说老后台(1.0版本)崩溃了,因为还有部门的业务没来得及迁移到新后台,我当时正在打农药哈哈~ 后 ...
- DWORD WORD到INT的转换
最近在做一个有关TCP/TP通信的消息解析,涉及到了这方面的转换,记录一下. 首先,如果是在网络传输.消息解析的情况下,要注意一下网络传送使用的是大端还是小端模式,这影响到我们的高低位的传输顺序. W ...
- python学习摘要(3)--字符串处理函数
python没有字符类型, "字符串" '字符串' '''字符串''' """字符串""" 三引号可以支持字符串跨行 字 ...
- Java核心技术点之接口
1. 为什么使用接口 Java中的接口是一组对需求的描述.接口通过声明接口方法来对外宣布:“要想具有XX功能,就得按我说的做(即实现接口方法).” 而接口的实现类通过实现相应接口的方法来宣布:“我已经 ...
- LintCode-159.寻找旋转排序数组中的最小值
寻找旋转排序数组中的最小值 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 你可以假设数组中不存在重复的 ...
- OSG数学基础:坐标系统
坐标系是一个精确定位对象位置的框架,所有的图形变换都是基于一定的坐标系进行的. 三维坐标系总体上可以分为两大类:左手坐标系和右手坐标系. 常用的坐标系:世界坐标系.物体坐标系和摄像机坐标系. 世界坐标 ...
- 【OSG】运行OSG示例出现的奶牛不完整问题
发现一个很奇怪的问题:我用笔记本运行OSG里面的示例,出现的图案总是不完整显示的,以经典的奶牛图案为例,如图. 图一是我电脑上的情况,正常情况应该是图二.不知道这是什么原因,难道是我电脑显卡的原因吗? ...