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:

No
3
题意:
n个学生,m对互相认识彼此,问能不能把互相认识的分到同一个房间,不能的话输出No,能的话输出最大对数。
 
题解:
考虑能的情况,显然是二分图的最大匹配,A和B一个房间,可以看作A,B以选中,A,B自然不能和其它人在一个房间,其余的也同理。
题干中说的是分成两个group,每一组中的人互不认识,这样就可以构成一个二分图。如果存在二分图,那么就必然有最大匹配。
如果同一组中有认识的人,那么就不能构成二分图,所以还需要二分图的判断(二分图染色)。
注意一点细节:由于要二分图染色,所以连的是双向边,最后求最大匹配的时候求了两次。
 
代码如下:
#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(二分图染色+二分图匹配)的更多相关文章

  1. HDU2444 The Accomodation of Students —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...

  2. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  3. HDU2444 The Accomodation of Students

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  4. HDU2444 The Accomodation of Students(二分图最大匹配)

    有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...

  5. The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间

    /*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 |  C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...

  6. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  7. HDU2444 The Accomodation of Students【匈牙利算法】

    题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...

  8. 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 ...

  9. hdu_2444The Accomodation of Students(二分图的判定和计算)

    hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...

随机推荐

  1. angular-使用iframe做独立页(iframe传值到angular和iframe里请求后台数据)

    这个方法使用过两次.一次是在项目中嵌入一个表达式生成器.因为用别人做好的网页变成组件很难,而且里面用了jq,与angular思想相反不能用.另一次是因为想要单独引用样式.而innerHTML使用的样式 ...

  2. sqlalchemy 转json 的几种常用方式

    sqlalchemy 转json 的几种常用方式 # -*- coding:utf-8 -*- import datetime from flask import Flask, json, jsoni ...

  3. 关于java中“使用了未经检查或不安全的操作、有关详细信息,请使用 ——Xlint:unchecked重新编译”

    今天看<算法 第4版>排序章节时,发现了一个了一个小问题.先贴一下代码: public class Selection{ public static void main(String[]a ...

  4. 记一次Log4j2日志无法输出的 心酸史

    问题描述:部分日志无法输出到日志文件中. 项目中的代码: @Resource private ConfigInfo configInfo; private static final Logger lo ...

  5. Python入门(1)

    一.Python的安装 进入Python官方网站:https://www.python.org/,按照下图操作,下载Python的安装器 下载完成,打开下载好的可执行文件,可以看到如下界面. 然后等待 ...

  6. https的主体过程

    https其实就是基于SSL的http.加密后的http信息按理是不会被篡改和查看的. https的过程总体上是按照下面来进行的: 1.客户端发起请求,服务端返回一个SSL证书,证书里面有一公钥A. ...

  7. vuejs学习之 项目打包之后的首屏加载优化

    vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...

  8. Python中的print

    Python 3.X的print 在Python 3.X中,print是一个内置函数,完整的声明形式如下: print([object, ...][, sep=' '][, end='\n'][, f ...

  9. Python中的slice操作

    Python中slice操作的完整语法: # i默认是0 # j默认是len(S) # k的步长,默认为+1 S[i:j:k] 其中i,j,k都可以是负数: 若i < 0或者k<0,等价于 ...

  10. ZooKeeper server &&client

    写了一个关于zookeepeer应用的简单demo 服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表 服务端代码: #includ ...