HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)
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.
InputFor 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.
OutputIf 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
题解:
题目是二分图模板题,判断是否是二分图的方法是:对于两部分的点,不存在一条边两个点在同一个集合里;BFS搜索即可,对于一条变的两个点大不同的标记,如果有矛盾,则不是二分图,否则是:
参考代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int g[maxn][maxn];
int match[maxn],vis[maxn];
int col[maxn]; bool bfs(int s,int n)
{
queue<int> q;
q.push(s);
col[s] = -;
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=;i<=n;i++)
{
if(g[u][i])
{
if(col[i]==col[u]) return false;
else if(col[i]==)
{
col[i] = -col[u];
q.push(i);
}
}
}
}
return true;
} bool judge(int n)
{
memset(col,,sizeof(col));
for(int i=;i<=n;i++)
{
if(col[i]==)
if(!bfs(i,n)) return false;
}
return true;
} bool dfs(int u,int n)
{
for(int i=;i<=n;i++)
{
if(vis[i] || !g[u][i]) continue;
vis[i] = ;
if(!match[i] || dfs(match[i],n))
{
match[i] = u; match[u] = i;
return true;
}
}
return false;
} int main(void)
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
memset(match,,sizeof(match));
memset(g,,sizeof(g));
for(int i=;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x][y]=;
}
if(!judge(n)) puts("No");
else
{
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i,n)) ans++;
}
printf("%d\n",ans);
}
}
return ;
}
HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- 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 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
[题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...
- 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(推断是否是二分图)
题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...
随机推荐
- 删除Linux的依赖库并进入救援模式恢复
删除Linux的依赖库并进入救援模式恢复 模拟一下依赖库文件被删,并进入救援模式恢复 系统:CentOS7 一.删除mv的依赖库文件 删除/lib64/libc.so.6 [root@centos7 ...
- 苹果客户端input时页面自动放大的问题
一.问题: 最近在用vue测试的时候发现,安卓端在输入框input时不存在页面自动缩放的问题,苹果客户端认为是考虑到用户的体验效果,才出现输入框自动放大的功能.但也收到了不少用户反馈体验效果不周. 二 ...
- 获取jar包内部的资源文件
通常获取一个资源文件很简单,问题是对于jar包内的资源文件,可能会发生意外.假如这里有一个文件操作的类: public class FileLoader { public boolean exists ...
- spring源码1
1.beans核心类 1.DefaultListableBeanFactory xmlBeanFactory xmlBeanFactory继承自DefaultListableBeanFactory,D ...
- opencv之常用还是忘,那咋办嘛
相机标定:https://blog.csdn.net/y2c58s43d69g8h7G_g/article/details/97239418 畸变参数个数要是镜头太凸的话,就像鱼眼相机和哨兵150视角 ...
- opencv随笔1
图像处理技术一般包括图像压缩,增强和复原,匹配 描述和l识别 3 个部分. 图像处理一般指数字图像处理 ( Digitallmage Processing). 其中,数字图像是指用工业相机.摄像机.扫 ...
- HTML的footer置于页面最底部
vue项目中,使用element-ui的布局,仍然出现footer不固定页面底部的情况,网上找到的一个管用的 方法是:footer高度固定+绝对定位 <html> <head> ...
- pyhton3 之 time模块实例小结
一.实例1:实现秒表: import time print('按下回车开始计时,按下 Ctrl + C 停止计时.') while True: try: input() # 如果是 python 2. ...
- 用CodePush在React Native App中做热更新
最近在学React Native,学到了CodePush热更新. 老师讲了两种实现的方法,现将其记录一下. 相比较原生开发,使用React Native开发App不仅能节约开发成本,还能做原生开发不能 ...
- 甲小蛙战记:PHP2Java 排雷指南
(马蜂窝技术原创内容,申请转载请在公众后后台留言,ID:mfwtech ) 大家好,我是来自马蜂窝电商旅游平台的甲小蛙,从前是一名 PHP 工程师,现在可能是一名 PHJ 工程师,以后...... 前 ...