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 ...
随机推荐
- java如何随机生成定长的字符串
小数,字符串.时间等示例代码 String base = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 public c ...
- 网络下载功能实现(downloader ) ---- HTML5+
模块:downloader Downloader模块管理网络文件下载任务,用于从服务器下载各种文件,并支持跨域访问操作.通过plus.downloader获取下载管理对象.Downloader下载使用 ...
- 焦作网络赛L-Poor God Water【矩阵快速幂】
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- Navicat运行sql文件报错out of memory
下载并安装mysql workbench:
- matplotlib-曲线和折线案例
matplotlib-曲线和折线案例 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-5, 5, 100) pr ...
- centos Linux下磁盘管理 parted,df ,du,fdisk,partprobe,mkfs.ext4,mount,/etc/fstab,fsck,e2fsck,mk2efs,tmpfs ,nr_inodes, LVM,传统方式扩容文件系统 第七节课
centos Linux下磁盘管理 parted,df ,du,fdisk,partprobe,mkfs.ext4,mount,/etc/fstab,fsck,e2fsck,mk2efs,tmpf ...
- input输入框type=file时accept中可以限制的文件类型(转载)
转载自: input type=file accept中可以限制的文件类型 在上传文件的时候,需要限制指定的文件类型. <input type="file" accept=& ...
- Jenkins的持续集成
持续集成:不需要人工干预,持久化.重复的运行一个任务.将代码自动的更新到最新,然后自动运行. 新建项目之前要再Jenkins的全局工具配置里面把git的路径设置好.[全局工具配置]-->[Git ...
- 20165324_mypwd
20165324_mypwd 实验要求 实验要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd 背景知识 文件存储结构 介绍文件 ...
- stringbuffer 和 stringbuilder区别
stringbuffer 和 stringbuilder速度 小于 线程安全 线程非安全 单线程操作大量数据用stringbui ...