HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
链接:
https://vjudge.net/problem/HDU-2444#author=634579757
题意:
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.
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.
有n个关系,他们之间某些人相互认识。这样的人有m对。
你需要把人分成2组,使得每组人内部之间是相互不认识的。
如果可以,就可以安排他们住宿了。安排住宿时,住在一个房间的两个人应该相互认识。
最多的能有多少个房间住宿的两个相互认识。
思路:
先二分图判定,再二分图最大匹配
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e2+10;
vector<int> G[MAXN];
int Linked[MAXN], Vis[MAXN];
int Color[MAXN];
int n, m;
bool Dfs(int x)
{
for (int i = 0;i < G[x].size();i++)
{
int nextnode = G[x][i];
if (Vis[nextnode])
continue;
Vis[nextnode] = 1;
if (Linked[nextnode] == -1 || Dfs(Linked[nextnode]))
{
Linked[nextnode] = x;
return true;
}
}
return false;
}
int Solve()
{
int cnt = 0;
memset(Linked, -1, sizeof(Linked));
for (int i = 1;i <= n;i++)
{
memset(Vis, 0, sizeof(Vis));
if (Dfs(i))
cnt++;
}
return cnt;
}
bool Dfsb(int u, int v)
{
Color[u] = v;
for (int i = 0;i < G[u].size();i++)
{
if (Color[G[u][i]] == v)
return false;
if (Color[G[u][i]] == 0 && !Dfsb(G[u][i], -v))
return false;
}
return true;
}
bool Binary()
{
memset(Color, 0, sizeof(Color));
for (int i = 1;i <= n;i++)
{
if (Color[i] == 0)
{
if (!Dfsb(i, 1))
return false;
}
}
return true;
}
int main()
{
while (cin >> n >> m)
{
for (int i = 1;i <= n;i++)
G[i].clear();
int l, r;
for (int i = 1;i <= m;i++)
{
cin >> l >> r;
G[l].push_back(r);
}
if (!Binary())
{
cout << "No" << endl;
continue;
}
int cnt = Solve();
cout << cnt << endl;
}
return 0;
}
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二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- 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 ...
- 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 (偶图判定,匈牙利算法)
题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
- 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(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- 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 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 ...
随机推荐
- abap seach help 搜索帮助
ABAP 的搜索帮助有很多种方法,掌握下面的几种基本差不多了 *&--------------------------------------------------------------- ...
- vc/vs常见报错:/****error C2106: '=' : left operand must be l-value****/
一.错误信息解析: 1.error,表示这是一条出错信息. C语言信息一般有error(出错)和warning(警告)两种. error是编译器遇到了致命错误,无法继续进行编译,必须修改. warni ...
- 一本值得你反复研读的Python佳作《Python编程从0到1》
现在的Python入门书太多太多了,究竟如何选择呢? 当然选最好的最入门的讲解最清晰的,没有那么多废话的. 现在就推荐一本<Python编程从0到1>,还带视频的,到时候跟大家一起学习沟通 ...
- python关键字以及含义,用法
Python常用的关键字 1.and , or and , or 为逻辑关系用语,Python具有短路逻辑,False and 返回 False 不执行后面的语句, True or 直接返回Tru ...
- 交换机安全学习笔记 第五章 DHCP缺陷攻击
关于DHCP攻击有如下几类攻击方式: 一.耗尽DHCP地址池 通过随机生成源MAC地址,然后伪造DHCPDISCOVER数据包.耗尽DHCP服务器地址池. 免费的攻击工具: Yersi ...
- c语言Ι博客作业04
这个作业属于哪个课程 c语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9771 我在这个课程的目 ...
- spring aop之父子容器
需求;项目对外提供接口,要求每个对外接口都要进行token认证. 解决办法:写一个token认证的工具类,在每个需要认证的接口方法开始的地方,调用工具类中的token认证方法. 问题:因为要满足指定条 ...
- yii框架RBAC權限管理
基于角色的存取控制 (RBAC) 基于角色的存取控制 (RBAC) 提供了一个简单而强大的集中式存取控制机制. 详细的关于 RBAC 和诸多传统的存取控制方案对比的详情,请参阅 Wikipedia. ...
- css3的calc属性不生效问题
css3的 calc:计算属性.由于自己做的项目中这个属性不常用到,偶尔用一次还没效果. 后来研究了下是因为运算符两边没加空格. 错误示例:.content{width:calc(100%-50px) ...
- RabbitMQ入门教程(九):首部交换机Headers
原文:RabbitMQ入门教程(九):首部交换机Headers 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...