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 ...
随机推荐
- cento7忘记root密码怎么办
1.首先开启系统,一直按 e 键 进入编辑选项 2.光标下移,在UTF-8行这一段修改两处,首相找到ro改为rw,即只读改为可读写权限:然后在这段的尾部加入 init=/bin/sh 3.此时按住Ct ...
- VBA删除空白行列
'删除空行 Sub DeleteEmptyRows() Dim LastRow As Long, r As Long LastRow = ActiveSheet.UsedRange.Rows.Coun ...
- 自己实现一个list比较器 实现Comparator()接口
一:一个实体类 成员变量有名字,年龄,分数 )))))); List<User> list = new ArrayList<>(); list.add(user1); list ...
- springboot--websocket简单demo(一):session chat
最近跟着大佬 https://tycoding.cn/2019/06/16/project/boot-chat/ 敲了2个关于websocket的demo,总结一下 从将会话信息保存在session中 ...
- mysql中基本的语句
操作字段: 添加字段 ALTER TABLE 表名 ADD 字段 varchar(20) COMMENT '别名'; 修改表字段的属性等(除了修改表名称) ALTER TABLE 表名 MODIFY ...
- A1139-引爆炸弹 计蒜客 bfs剪枝
题目链接 https://nanti.jisuanke.com/t/A1139 在一个 n \times mn×m 的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上 ...
- USACO3.3 Home on the Range【思维】
做完之后看到题解里面很多bfs,dfs,甚至还有dp? 写了一个不知道怎么称呼它的方法,暂且叫他乱搞吧. 用数组a[][]预处理出以当前行作为最底层,这一列从上往下的最长的1的长度. 如果这个格子为0 ...
- 图片下载&&非同源图片下载&&同源下载&&网页点击下载图片
非同源图片下载(html添加canvas标签) 方法1: downloadImgByBase64(url){ console.log() // 创建隐藏的可下载链接 // let blob = ...
- C#动态调用带有SoapHeader验证的WebServices
http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...
- java--编码规范易漏
1:命名规范 类名用大驼峰式 参数变量·函数·成员变量·局部变量 小驼峰式 常亮命名全部大些单词用_隔开 抽象类用Abstract开头·异常类用Excetpion结尾·测试类用Test结尾 *POJO ...