hdu 2444(染色法判断二分图+最大匹配)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5225 Accepted Submission(s): 2374
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.
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.
these students cannot be divided into two groups, print "No".
Otherwise, print the maximum number of pairs that can be arranged in
those rooms.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int n,m;
int graph[N][N],mp[N][N];
int linker[N];
bool vis[N];
int color[N]; ///染色数组
bool dfs(int u){
for(int v=;v<=n;v++){
if(graph[u][v]&&!vis[v]){
vis[v] = true;
if(linker[v]==-||dfs(linker[v])){
linker[v] = u;
return true;
}
}
}
return false;
}
bool bfs(int s){
queue<int> q;
color[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i=;i<=n;i++){
if(graph[u][i]){
if(color[i]==-){///未染色
color[i] = !color[u];
q.push(i);
}else{
if(color[i]==color[u]) return false;
}
}
}
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
memset(graph,,sizeof(graph));
memset(color,-,sizeof(color));
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
graph[u][v] = ;
graph[v][u] = ;
}
bool flag = false;
for(int i=;i<=n;i++){
if(color[i]!=-) continue; ///已染色
if(!bfs(i)) {
flag = true;
break;
}
}
if(flag){
printf("No\n");
continue;
}
memset(linker,-,sizeof(linker));
int res = ;
for(int i=;i<=n;i++){
memset(vis,false,sizeof(vis));
if(dfs(i)){
res++;
}
}
printf("%d\n",res/);
}
return ;
}
hdu 2444(染色法判断二分图+最大匹配)的更多相关文章
- 【交叉染色法判断二分图】Claw Decomposition UVA - 11396
题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
- poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图
http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...
- 染色法判断是否是二分图 hdu2444
用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...
- Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5971 题意:有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个 ...
- Catch---hdu3478(染色法判断是否含有奇环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意:有n个路口,m条街,一小偷某一时刻从路口 s 开始逃跑,下一时刻都跑沿着街跑到另一路口,问 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- [HDU] 2063 过山车(二分图最大匹配)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
随机推荐
- 快速搭建http服务:共享文件--Java的我,不知Python你的好
在 Linux 服务器上或安装了 Python 的机器上, 我们可以在指定的文件目录下,使用 python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web ...
- TYVJ2032 升降梯上
Description: 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe ...
- 【DP】【P1586】四方定理
传送门 Description Input 第一行为一个整数T代表数据组数,之后T行每行一个数n代表要被分解的数 Output 对于每个n输出一行,为方案个数 Sample Input Sample ...
- 使用Faster-Rcnn进行目标检测(实践篇)转载
原 文链接:http://blog.csdn.net/gavin__zhou/article/details/52052915 原理 上一篇文章,已经说过了,大家可以参考一下,Faster-Rcnn进 ...
- 旋转 3d
建议chorme浏览器浏览,有样式兼容性问题. 图片可以根据自己本地路径设置路径,js库引用jquery. 写的不好,多多建议,谢谢大家. <html onselectstart="r ...
- JavaScript实现35选7并记录历史状态
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABQCAYAAACnOs9vAAAJy0lEQVR4nO2dbWwUxxnH/2c5SElQSl ...
- overflow:auto产生的滚动条在安卓系统下能平滑滚动,而在ios下滚动不平滑
由于系统的问题,加上-webkit-overflow-scrolling : touch; 即可解决平滑滚动问题
- XAMPP 启动mysql报错 InnoDB: Error: could not open single-table tablespace file……
昨天安装了最新版本XAMPP for Windows 1.8.3. 今天早上打开XAMPP双击mysql Start按钮报错,如下(部分截取): 2013-09-17 10:12:02 9012 [E ...
- 使用awk批量杀进程的命令
在做系统运维的过程中,有时候会碰到需要杀掉某一类进程的时候,如何批量杀掉这些进程,使用awk命令是很好的选择. ps -ef|grep aaa|grep -v grep|awk '{print &qu ...
- 【20161109】noip模拟赛
1.Game [题目描述] 明明和亮亮在玩一个游戏.桌面上一行有n个格子,一些格子中放着棋子.明明和亮亮轮流选择如下方式中的一种移动棋子(图示中o表示棋子,*表示空着的格子): 1) 当一枚棋子的右边 ...