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

Problem 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
No
3
 
Source
 
题意:给出 n 个学生,将这些学生互不认识的人分成一组,剩下的人分成另一组,然后在这两组中互相认识的人配对,问最多能够配多少对?
题解:先用染色法对无向图进行判断是否为二分图,不是的话直接输出No,是的话进行最大匹配。网上有很多解法其实是有问题的,题目并没有说是强连通图,都是从1开始判断,但是这组数据就是过不了的,所以我们要对所有点进行判断。
无向图G为二分图的充分必要条件是,G至少有两个顶点,且其所有回路的长度均为偶数。
4 3
2 3
3 4
4 2
ans:No
#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(染色法判断二分图+最大匹配)的更多相关文章

  1. 【交叉染色法判断二分图】Claw Decomposition UVA - 11396

    题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...

  2. poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图

    http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...

  3. 染色法判断是否是二分图 hdu2444

    用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...

  4. Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5971 题意:有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个 ...

  5. Catch---hdu3478(染色法判断是否含有奇环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 题意:有n个路口,m条街,一小偷某一时刻从路口 s 开始逃跑,下一时刻都跑沿着街跑到另一路口,问 ...

  6. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. [HDU] 2063 过山车(二分图最大匹配)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...

  8. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  9. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

随机推荐

  1. 【极值问题】【CF1063B】 Labyrinth

    传送门 Description 给你一个\(n~\times~m\)的矩阵,一开始你在第\(r\)行第\(c\)列.你的上下移动不受限制,向左最多移动\(x\)次,向右最多移动\(y\)次.求你最多能 ...

  2. JavaScript身份证号码有效性验证

    最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验证规则太过简单,只是简单的验证了身份证长度,现在业务需要加强下身份证验证规则,网上找到了不少资料,不过都不合偶的心意,无奈只好直接写一个 ...

  3. JavaScript中的函数与栈

    Javascript中会经常用到setTimeout来推迟一个函数的执行,如: setTimeout(function(){ alert("Hello World"); },100 ...

  4. node.js 与java 的主要的区别是什么

    node.js 与java都是服务器语言,但是两者存在很大区别:(1)Node.js比Java更快 :node.js开发快,运行的效率也算比较高,但是如果项目大了就容易乱,而且javascript不是 ...

  5. phalcon安装

    参考网站:https://docs.phalconphp.com/zh/latest/reference/tools.html (中文版)cento6.5环境安装:cd ~mkdir phalconc ...

  6. noip模拟赛 保留道路

    [问题描述] 很久很久以前有一个国家,这个国家有N个城市,城市由1,2,3,…,N标号,城市间有M条双向道路,每条道路都有两个属性g和s,两个城市间可能有多条道路,并且可能存在将某一城市与其自身连接起 ...

  7. [linux/net]策略路由实现特定主机特定路径

        echo 200 silence >> /etc/iproute2/rt_tables ip rule add from 10.192.0.230 table silence ip ...

  8. 分割png图片

    /*** * 分割图片 */public function cut_img(){ $filename = 'site_upload/form_file_forinput/20180313/201803 ...

  9. UVA 11440 Help Tomisu

    https://vjudge.net/problem/UVA-11440 题意: 求2——n! 之间有多少个整数x,满足x的所有素因子都大于m 保证m<=n x的所有素因子都大于m 等价于 x和 ...

  10. Chrome浏览器启动页被360导航篡改解决方法

    右键Chrome浏览器快捷方式,选择“属性”,在“目标”的结尾处有添加的网址,删了即可. 2 如果在结尾处没有任何网址,可以添加“ -nohome”,这样下次启动时,就会打开一个空白页,也就不会打开被 ...