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对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...
随机推荐
- 微擎 pdo_fetchall() 函数
微擎 pdo_fetchall() 函数 注意点: 该函数内部直接执行原生 SQL 语句 如果在传递表名的时候使用了 tablename .则不加 ims_ 前缀 参数的传递通过 :param 的形式 ...
- 802.11r协议理解
首先阅读了相关协议内容整理出了如下的802.11r时序图所谓基础,然后会详细理解其中的每一个步骤:
- 拎壶学python3-----(5)python之格式化输出
一.格式化输入可以减少代码开发量如下是格式化输出: 上边的%是什么意思呢?%是一个占位符,s代表字符串的类型. 二.我们看下边的例子 看使用%d %i的用法. %d %i 这种格式化只能用数字来填补占 ...
- .NET core3.0 使用Jwt保护api
摘要: 本文演示如何向有效用户提供jwt,以及如何在webapi中使用该token通过JwtBearerMiddleware中间件对用户进行身份认证. 认证和授权区别? 首先我们要弄清楚认证(Auth ...
- linux 6.5操作系统建立
VM上redhat enterprise linux6 ---> 使用仅主机模式网络连接 开始安装: ——>是否检查镜像:skip ——>选择语言: 中文/英文 ——>存储设备 ...
- 投票通过,PHP 8 确认引入 Union Types 2.0
关于是否要在 PHP 8 中引入 Union Types 的投票已于近日结束,投票结果显示有 61 名 PHP 开发组成员投了赞成票,5 名投了反对票. 还留意到鸟哥在投票中投了反对票~) 因此根据投 ...
- 18个awk的经典实战案例
介绍 这些案例是我收集起来的,大多都是我自己遇到过的,有些比较经典,有些比较具有代表性. 这些awk案例我也录了相关视频的讲解awk 18个经典实战案例精讲,欢迎大家去瞅瞅. 插入几个新字段 在&qu ...
- Photoshop CS5软件安装教程
Photoshop CS5(32/64位)精简版下载地址: 链接:https://pan.baidu.com/s/11T-L-aH3JNXlJmSqL0JY6Q提取码:ea5j Photoshop主要 ...
- 相关性不一定等于因果性:从 Yule-Simpson’s Paradox 讲起
1. 两件事伴随发生,不代表他们之间有因果关系 - 从一些荒诞相关性案例说起 在日常生活和数据分析中,我们可以得到大量相关性的结论,例如: 输入X变量,有98%置信度得到Y变量 只要努力,就能成功 只 ...
- React - 组件:类组件
目录: 1. 类组件有自己的状态 2. 继承React.Component-会有生命周期和this 3. 内部需要一个render函数(类组件会默认调用render方法,但不会默认添加,需要手动填写r ...