hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
#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 判断二分图+二分匹配的更多相关文章
- HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students (判断二分图,最大匹配)
The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- 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 ...
随机推荐
- linux下nproc的作用
文章来源: http://blog.csdn.net/odailidong/article/details/50561257 nproc是操作系统级别对每个用户创建的进程数的限制,在Linux下运行多 ...
- 洛谷P2444 病毒【AC自动机】
题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...
- rk3188 双屏异显分析
首先是android层: PhoneWindow.java 中加入了GestureDetector成员, 来实现全局滑屏手势监听 onFling方法中,调用了mDecor.getRootWindo ...
- 转载:Linux内核调试方法
转载文章请注明作者和二维码及全文信息. 转自:http://blog.csdn.net/swingwang/article/details/72331196 不会编程的程序员,不是好的架构师,编程和内 ...
- (2.5)DDL增强功能-触发器trigger
SQL Server:触发器详解 1. 概述 2. 触发器的分类 3. Inserted和Deleted表 4. 触发器的执行过程 5. 创建触发器 6. 修改触发器: 7. 删除触发器: 8. ...
- elasticsearch-hadoop使用
elasticsearch-hadoop是一个深度集成Hadoop和ElasticSearch的项目,也是ES官方来维护的一个子项目,通过实现Hadoop和ES之间的输入输出,可以在Hadoop里面对 ...
- [WorldWind学习]18.High-Performance Timer in C#
In some applications exact time measurement methods are very important. 一些应用程序中精确的时间测量是非常重要的. The of ...
- telnet到RedHat Linux失败--解决办法
失败原因: 1.telnet包未安装,检查telnet包是否安装: [root@vm-rhel root]# rpm -qa telnet telnet-0.17-25 表示已安装 2.telnet包 ...
- python16_day34【设计模式】
一.简单工厂模式 # coding : utf-8 # create by ztypl on 2017/5/24 from abc import abstractmethod, ABCMeta cla ...
- JS 动态加载脚本 执行回调
JS 动态加载脚本 执行回调 关于在javascript里面加载其它的js文件的问题可能很多人都遇到过,但很多朋友可能并不知道怎么判断我们要加载的js文件是否加载完成,如果没有加载完成我们就调用文件 ...