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对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...
随机推荐
- Ansible之playbook拓展
一.handlers和notify结合使用触发条件 handlers同tasks是属同级,相当于一个特殊任务列表,这些任务同前文说的tasks里的任务没有本质的不同,用于当关注的资源发生变化时,才会采 ...
- 监听器以及在监听类里面获得bean的方法
1实现HttpSessionListener和ServletContextListener,2个接口 2然后在contextInitialized初始化方法里面: ServletContext app ...
- 每天一道算法题-leetcode136-只出现一次的数字
前言 打卡第一天 2019.10.26日打卡 算法,即解决问题的方法.同一个问题,使用不同的算法,虽然得到的结果相同,但是耗费的时间和资源是不同的.这就需要我们学习算法,找出哪个算法更好. 大家都知道 ...
- SSE图像算法优化系列三十:GIMP中的Noise Reduction算法原理及快速实现。
GIMP源代码链接:https://gitlab.gnome.org/GNOME/gimp/-/archive/master/gimp-master.zip GEGL相关代码链接:https://gi ...
- java笔试面试第二天
没想到第二次面试到了第二周,也是我在上海找工作的第二周,说实话,没有真本事找工作是真的难,虽然正在召开的十九大上,大大们纷纷表态国力正盛,经济稳步增长,就业压力逐渐缓解,但是社会终究是社会,要么靠实力 ...
- T-SQL Part IV: ORDER BY
ORDER BY 返回一个Cursor,并不返回结果集.而试图将Cursor作为输入将产生了错误. 所以,下列的SQL语句将产生错误: SELECT VerID, IsComplete VerID, ...
- tomcat 部署springboot 项目
Springboot项目默认jar包,且内置Tomcat.现需要将项目打成war包,并部署到服务器tomcat中. 1.修改pom.xml文件.将jar修改为war. <packaging> ...
- UEFI+GPT电脑Win10下安装openSUSE Leap 42.2双系统
安装过程仅供参考,最后实现方式不完美. 1 准备工具,一个8G以上U盘,已装好win10的UEFI+GPT电脑(本机为SSD+HDD双硬盘) 2 所需软件: 2.1 ...
- pat 1008 Elevator(20 分)
1008 Elevator(20 分) The highest building in our city has only one elevator. A request list is made u ...
- 力扣(LeetCode)查找常用字符 个人题解
给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 ...