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 ...
随机推荐
- Screen Painter 程序设计
一.Screen 的创建及维护, TCode:SE51 输入程序名称,单击[建立], 程序1000为SAP预留屏幕号,屏幕号必须定义1000外的其他数字,且最多不超过四位, 本例定义屏幕为SAP预留屏 ...
- django 如何传递id 参数
urls.py 注意这里的bid
- 浏览器从输入URL到渲染出页面发生了什么
总体来说分为以下几个过程: 1. DNS解析 2. TCP连接 3. 发送HTTP请求 4. 服务器处理请求并返回HTTP报文 5. 浏览器解析渲染页面 6. 连接结束 参考资料:[https:// ...
- Nginx Server 上80,443端口。http,https共存
server{ listen 80; listen 443 ssl; server_name www.iamle.com; index index.html index.htm index.php; ...
- LeetCode.942-DI字符串匹配(DI String Match)
这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...
- 转·带你用实例理解C语言回调函数
原文出处:https://segmentfault.com/a/1190000008293902?utm_source=tag-newest 前言: 如不懂函数指针,请先查阅关于函数指针内容的资料(h ...
- 应用安全 - 工具 - 中间件 - Apache - Apache Tika - 漏洞汇总
CVE-2016-6809 Date2016 类型远程代码执行 影响范围Apache Tika 1.6-1.13 CVE-2018-1335 Date2018 类型命令注入 影响范围Tika-serv ...
- 图片下载&&非同源图片下载&&同源下载&&网页点击下载图片
非同源图片下载(html添加canvas标签) 方法1: downloadImgByBase64(url){ console.log() // 创建隐藏的可下载链接 // let blob = ...
- Mac OS X 11中的/usr/bin 的“Operation not permitted”
更新了 Mac OS X 11后发现,MacVim 不再能够通过Terminal用命令打开了. mvim hello.txt 于是尝试将 mvim 重新复制到/usr/bin/中去 sudo cp - ...
- [百度百科]PCI-E的速度
在早期开发中,PCIe最初被称为HSI(用于高速互连),并在最终确定其PCI-SIG名称PCI Express之前,将其名称更改为3GIO(第三代I / O). 名为阿拉帕霍工作组(AWG)的技术工作 ...