The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
用染色法,即从其中一个顶点开始,将跟它邻接的点染成与其不同的颜色,如果邻接的点有相同颜色的,则说明不是二分图,每次用bfs遍历即可。
二分匹配:套版子;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<" "<<endl;
const int N=2e2+,M=1e6+,inf=2e9+,mod=1e9+;
const ll INF=1e18+;
int n,m;
int mp[N][N];
int linker[N];
bool used[N];
bool dfs(int a)
{
for(int i=;i<n;i++)
if(mp[a][i]&&!used[i])
{
used[i]=true;
if(linker[i]==-||dfs(linker[i]))
{
linker[i]=a;
return true;
}
}
return false;
}
int hungary()
{
int result=;
memset(linker,-,sizeof(linker));
for(int i=;i<n;i++)
{
memset(used,,sizeof(used));
if(dfs(i)) result++;
}
return result;
}
queue<int>q;
int color[N];
int bfs(int s)
{
while(!q.empty())q.pop();
q.push(s);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=;i<n;i++)
{
if(mp[x][i])
{
if(color[i]==-)
color[i]=color[x]^,q.push(i);
else if(color[i]==color[x])
return ;
}
}
}
return ;
}
int check()
{
memset(color,-,sizeof(color));
for(int i=;i<n;i++)
{
if(color[i]==-)
{
color[i]=;
if(!bfs(i))return ;
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(mp,,sizeof(mp));
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u-][v-]=;
}
if(!check())printf("No\n");
else
{
int cnt=hungary();
printf("%d\n",cnt);
}
}
return ;
}

hdu 2444 The Accomodation of Students 判断二分图+二分匹配的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配

    此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...

  6. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  7. HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  8. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  9. hdu 2444 The Accomodation of Students 【二分图匹配】

    There are a group of students. Some of them may know each other, while others don't. For example, A ...

随机推荐

  1. [SQL] 获取 Microsoft SQL Server 2008 的数据表结构

    then d.name else '' end , 表说明 then isnull(f.value,'') else '' end , 字段序号 = a.colorder , 字段名 = a.name ...

  2. Intellij IDEA同时打开多个项目

    extends:http://www.kaifazhe.me/java/99.html 使用eclipse习惯的同学知道是可以同时多个项目查看的,只需要import就可以了,但Intellij IDE ...

  3. struts2的占位符*在action中的配置方法

    转自:https://blog.csdn.net/u012546338/article/details/68946633 在配置<action> 时,可以在 name,class,meth ...

  4. pythonMD5加密

    #MD5加密def md5_key(arg): hash = hashlib.md5() hash.update(arg) return hash.hexdigest()

  5. apache工作模式worker以及prefork的切换

    apache比较常用的工作模式有worker以及prefork两种方式. 如果在编译时候不指定,系统默认的是prefork模式:如果需要换成worker模式,需要在编译的时候带上编译参数:--with ...

  6. Service学习笔记

    一 什么是Service Service作为安卓四大组件之一,拥有重要的地位.Service和Activity级别相同,只是没有界面,是运行于后台的服务.这个运行“后台”是指不可见,不是指在后台线程中 ...

  7. Git源码安装

    系统自带yum安装的git版本较老,需要安装最新版本可以使用源码安装 下载最新安装包,下载地址https://github.com/git/git/releases 安装依赖包 yum install ...

  8. HanLP https://pypi.python.org/pypi/sumy/

    HanLP - 汉语言处理包 http://hanlp.linrunsoft.com/doc.html https://pypi.python.org/pypi/sumy/

  9. 关于mysql5.7的一些变化

    最近接了个项目,使用的数据库是5.7的,开始没太在意,但是在接手的过程中发送了些小插曲,特意记录下来. 首先,我想自己安装个noinstall版本的,结果发信下载下来的和之前版本的不一样,没有data ...

  10. Python开发【模块】:CSV文件 数据可视化

    CSV模块 1.CSV文件格式 要在文本文件中存储数据,最简单的方式是讲数据作为一系列逗号分隔的值(CSV)写入文件,这样的文件成为CSV文件,如下: AKDT,Max TemperatureF,Me ...