The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5451    Accepted Submission(s): 2491

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

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

思路:二分判定用染色法,找最大匹配数用匈牙利算法

代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
const int MAX=1e5+;
using namespace
std;
vector<int>mp[MAX];
int
d[MAX],n,m,vis[MAX],link[MAX];
int
dfs(int x,int f) //染色法
{

d[x]=f;
for
(int i=; i<mp[x].size(); i++)
{

if
(d[x]==d[mp[x][i]])
return
;
int
u=mp[x][i];
if
(d[u]==)
if
(!dfs(u,-f))
return
;
}

return
;
}

int
dfs2(int x) //找最大匹配
{

for
(int i=; i<mp[x].size(); i++)
{

int
v=mp[x][i];
if
(!vis[v])
{

vis[v]=;
if
(link[v]==-||dfs2(link[v]))
{

link[v]=x;
return
;
}
}
}

return
; }
int
find() //找最大匹配
{

int
res=;
memset(link,-,sizeof(link));
for
(int i=; i<=n; i++)
{

memset(vis,,sizeof(vis));
if
(dfs2(i))
res++;
}

return
res;
}

int
main()
{

int
a,b;
while
(cin>>n>>m)
{

for
(int i=; i<=n; i++)
if
(mp[i].size())
mp[i].clear();
for
(int i=; i<m; i++)
{

scanf("%d%d",&a,&b);
mp[a].push_back(b);
mp[b].push_back(a);
}

int
flag=;
memset(d,,sizeof(d));
for
(int i=; i<=n; i++) //染色
{

if
(mp[i].size())
if
(!d[i])
if
(!dfs(i,))
{

flag=;
break
;
}
}

if
(!flag)
cout<<"No"<<endl;
else

{

cout<<find()/<<endl;
} }
}

hdu-2444-二分图判定+最大分配的更多相关文章

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

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

  2. hdu 2444(二分图) The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 大意是给定n个学生,他们之间可能互相认识,首先判断能不能将这些学生分为两组,使组内学生不认识: 现想将学生 ...

  3. hdu 2444 二分图判断与最大匹配

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

  4. HDU 2444 二分图判断 (BFS染色)+【匈牙利】

    <题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...

  5. HDU - 2444 二分图最大匹配 之 判断二分图+匈牙利算法

    题意:第一行给出数字n个学生,m条关系,关系表示a与b认识,判断给定数据是否可以构成二分图,如果可以,要两个互相认识的人住一个房间,问最大匹配数(也就是房间需要的最小数量) 思路:要看是否可以构成二分 ...

  6. HDU 5971 二分图判定

    Wrestling Match Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

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

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

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

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

  9. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

  10. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

随机推荐

  1. JVM类加载

    JVM的类加载机制就是:JVM把描述类的class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被JVM直接使用的Java类型 ClassLoader JVM中的ClassLoade ...

  2. 创建ABPboilerplate模版项目

    本文是根据角落的白板报的<通过ABPboilerplate模版创建项目>一文的学习总结,感谢原文作者角落的白板报. 1 准备 开发环境: Visual Studio 2015 update ...

  3. [转载]SQL语句中的日期计算

    1. 本月的第一天SELECT  DATEADD(mm,  DATEDIFF(mm,0,getdate()),  0) 2. 本月的最后一天SELECT  dateadd(ms,-3,DATEADD( ...

  4. 浅谈Web自适应

    前言 随着移动设备的普及,移动web在前端工程师们的工作中占有越来越重要的位置.移动设备更新速度频繁,手机厂商繁多,导致的问题是每一台机器的屏幕宽度和分辨率不一样.这给我们在编写前端界面时增加了困难, ...

  5. P/Invoke:C#调用C++

    P/Invoke的全称是Platform Invoke (平台调用) 它实际上是一种函数调用机制通 过P/Invoke我们就可以调用非托管DLL中的函数. P/Invoke依次执行以下操作: 1. 查 ...

  6. React 其实比 MVVM 架构更加卡顿

    React 号称通过引入 Virtual DOM 带来了性能的提升,而其实 React 之所以需要 Virtual DOM,是因为它的架构下,state 的变更是全量的,然后触发 render 返回全 ...

  7. [译]ZOOKEEPER RECIPES-Queues

    队列 分布式队列是一种常见的数据结构.为了在ZooKepeer中实现分布式队列,第一步是要使用一个znode代表队列本身.分布式客户端通过create()方法将内容放入一个名叫"queue- ...

  8. CI Weekly #7 | Instgram/Quora 等大公司如何做持续部署?

    终于,你们期待的 flow.ci iOS 项目持续集成 开始公测了.在这几个工作日, flow.ci 做了些许「功能优化」与「问题修复」,性能和体验都在持续优化中.比如: iOS 快速入门文档更新: ...

  9. 【Win 10应用开发】延迟共享

    延迟共享是啥呢,这么说吧,就是在应用程序打开共享面板选择共享目标时,不会设置要共享的数据,而是等到共享目标请求数据时,才会发送数据,而且,延迟操作可以在后台进行. 这样说似乎过于抽象,最好的诠释方法, ...

  10. 6.LibSVM核函数

    libsvm的核函数类型(svmtrain.c注释部分): "-t kernel_type : set type of kernel function (default 2)\n" ...