The Accomodation of Students

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

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
题意:一些学生存在认识与不认识的关系,但这种关系不传递。问能否将学生分为两组,每组中的学生互相不认识(即二分图判定)。若可以,把相互认识的同学放在一个双人间,问最多需要几个双人间(即二分图最大匹配)。
#include"cstdio"
#include"cstring"
#include"vector"
#include"algorithm"
using namespace std;
const int MAXN=;
vector<int> G[MAXN];
int V,E;
bool bfs(int s)//二分图判断
{
int que[MAXN],rear=,front=;
int color[MAXN];
memset(color,-,sizeof(color));
color[s]=;que[rear++]=s;
while(rear!=front)
{
int v=que[front++];
for(int i=;i<G[v].size();i++)
{
int u=G[v][i];
if(color[u]==-)
{
color[u]=!color[v];//若没有染色则染成相反颜色
que[rear++]=u;
}
else if(color[u]==color[v]) return false;//若相邻结点颜色相同则不是二分图
}
}
return true;
}
int match[MAXN];
int vis[MAXN];
bool dfs(int u)//二分图匹配
{
vis[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i],w=match[v];
if(w==||(!vis[w]&&dfs(w)))
{
match[u]=v;
match[v]=u;
return true;
}
}
return false;
}
int matching()
{
memset(match,,sizeof(match));
int ans=;
for(int i=;i<=V;i++)
{
if(!match[i])
{
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
}
return ans;
}
int main()
{
while(scanf("%d%d",&V,&E)!=EOF)
{
for(int i=;i<=V;i++) G[i].clear();
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
if(bfs()) printf("%d\n",matching());
else printf("No\n");
}
return ;
}

HDU2444(二分图判定+最大匹配)的更多相关文章

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

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

  2. HDU2444(KB10-B 二分图判定+最大匹配)

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

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

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

  4. HDU2444 【二分图判定+最大匹配】

    套模板很好的题? #include<bits/stdc++.h> using namespace std; const int N=2e2+10; const int M=4e4+10; ...

  5. CF687A. NP-Hard Problem[二分图判定]

    A. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. hdu3729 I'm Telling the Truth (二分图的最大匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...

  7. POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)

    题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...

  8. COJ 0578 4019二分图判定

    4019二分图判定 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个具有n个顶点(顶点编号为0,1,… ...

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

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

随机推荐

  1. 视图交互--表视图(UITableView)的cell交互析略

    在表视图UITableView的cell上经常有一些交互,根据项目开发中的情况,需要对此进行一些规范.总结出了几种交互方法,这些方法在其他视图的交互上同样可以适用.用一个简单的例子来举例说明一下,其他 ...

  2. Android加壳native实现

    本例仅在Android2.3模拟器跑通过,假设要适配其它机型.请自行研究,这里不过抛砖引玉. 0x00 在Android中的Apk的加固(加壳)原理解析和实现,一文中脱壳代码都写在了java层非常ea ...

  3. shell-判断循环

    shell条件测试 test 每个完整的合理的编程语言都具有条件判断的功能. bash可以使用test命令,[]和()操作,还有if/then结构 字符串判断 -n string 判断字符串长度非零 ...

  4. kubernetes调度之pod优先级和资源抢占

    系列目录 Pod可以拥有优先级.优先意味着相对于其它pod某个pod更为重要.如果重要的pod不能被调度,则kubernetes调度器会优先于(驱离)低优先级的pod来让处于pending状态的高优先 ...

  5. Ubuntu 16.04 引导修复(Boot Repair)----lianwang----anzhuang windows hou(双系统修复一)

    2016-01-26 20:54 12548人阅读 评论(1) 收藏 举报 分类: =======学习心得=======(23) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...

  6. erlang启动参数记录

    不管在erlang的shell下还是脚本里,启动参数都是非常有用的,抽空儿整理下erlang的常用启动参数: +A size   异步线程池的线程数,范围为0~1024,默认为10 +P Number ...

  7. EasyCamera海康摄像机向EasyDarwin云平台推送音视频数据的缓存设计

    本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376 EasyCamera在向EasyDarwin云平台推送音视频数据时,有时一个I帧会很 ...

  8. Memory usage of a Java process java Xms Xmx Xmn

    http://www.oracle.com/technetwork/java/javase/memleaks-137499.html 3.1 Meaning of OutOfMemoryError O ...

  9. spring boot redis分布式锁 (转)

    一. Redis 分布式锁的实现以及存在的问题 锁是针对某个资源,保证其访问的互斥性,在实际使用当中,这个资源一般是一个字符串.使用 Redis 实现锁,主要是将资源放到 Redis 当中,利用其原子 ...

  10. WIN7系统设置wifi

    *&->20170302 112700 WIN7系统设置wifi, 开启win7的隐藏功能,即虚拟wifi功能和虚拟无线AP功能,即可实现将电脑变成wifi 供无线上网, 1.开始-命令 ...