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 ;
}

最新文章

  1. JS中获得当前时间的年月
  2. codevs3163 抄书问题2
  3. Android 5.0属性
  4. Git本地仓库
  5. Windows 64位操作系统和32位操作系统在注册表上的有一点不一样
  6. Javascript模块化编程之路——(require.js)
  7. 【转】cvs2svn 把CVS档案库转换为SVN档案库
  8. perl 变量详解
  9. 记一个问题的AC
  10. BZOJ1611: [Usaco2008 Feb]Meteor Shower流星雨
  11. Android studio 中引用jar的其实是Maven?(一)
  12. IoC容器的接口设计
  13. Golang基础之函数
  14. 【黑客免杀攻防】读书笔记6 - PE文件知识在免杀中的应用
  15. 如何使用Hive&amp;R从Hadoop集群中提取数据进行分析
  16. 2018.01.04 bzoj5291: [Bjoi2018]链上二次求和(线段树)
  17. HDU 4004 The Frog&#39;s Games(二分答案)
  18. Asterisk——part 1
  19. 关于使用 ps脚本来处理图片的排层问题
  20. Hadoop Map/Reduce的工作流

热门文章

  1. 5028: 小Z的加油店(线段树)
  2. 2016多校联合训练1 B题Chess (博弈论 SG函数)
  3. TYVJ2032 升降梯上
  4. 【HASH】【UVA 10125】 Sumset
  5. 除了love和hate,还能怎么表达那些年的“爱恨情仇”?
  6. 如何修改即时聊天websocket的端口号
  7. mysql的主从复制原理与实现
  8. 解决Sublime Install Package的There are no packages available for install问题(channel_v3.json)
  9. 适配器模式 C#
  10. Asp.net Web Api 2 FORM Authentication Demo