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. NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第二轮Day2题解

    肝了两题... T1一眼题,分解质因数,找出2的个数和5的个数取min输出 #include<iostream> #include<cstring> #include<c ...

  2. 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)

    P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...

  3. HDU4738:Caocao's Bridges(求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  5. C语言超大数据相加计算整理

    在做ACM 1002题时,整理得到. #include<stdio.h>#include<string.h>#define MAX 1000void zero(char *s, ...

  6. HDU1814 2-sat 模板

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. sun.security.x509.CertAndKeyGen;找不到

    导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...

  8. spring和Quartz的集群(二)

    一:前沿 写完了这两篇才突然想起来,忘记了最关键的东西,那就是在配置文件这里的配置,还有数据库的配置.这是郁闷啊!继续吧! 二:内容配置 我们在集成的时候需要自己配置一个quartz.properti ...

  9. UVA 12034 Race

    https://vjudge.net/problem/UVA-12034 题意:n个人比赛,有多少种可能的结果 假设i个人中,有j个人是第一名,方案数为C(i,j) 所以ans=Σ C(n,j)* f ...

  10. iOS 时间转换

    #pragma mark - 获取当前时间戳 -(NSString *)getTimeSp{ NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:]; ...