链接:

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(二分图判定,最大匹配)的更多相关文章

  1. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  2. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  3. 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 ...

  4. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  5. HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)

    题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...

  6. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  8. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. hdu 2444 The Accomodation of Students (判断二分图,最大匹配)

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. java:Spring框架3(AOP,SSH集成Demo)

    1.AOP: Spring提供了4种实现AOP的方式: 1.经典的基于代理的AOP 2.@AspectJ注解驱动的切面 3.纯POJO切面 4.注入式AspectJ切面 aop.xml: <?x ...

  2. vim开发配置

    需求:使用vim开发python,可以进行简单配置 cd 到用户宿主目录下 vim .vimrc 粘贴以下内容: 版本一: set encoding=utf-8"去掉vi的一致性" ...

  3. LeetCode.953-验证外语字典顺序(Verifying an Alien Dictionary)

    这是悦乐书的第364次更新,第392篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第226题(顺位题号是953).在外语中,令人惊讶的是,他们也使用英文小写字母,但可能使 ...

  4. 灰度图像的自动阈值分割(Otsu 法)

    关于otsu分割方法,这个文章讲的是最好的,清晰易懂,一看就是作者认真思考过的. 因为在看这个算法的时候我就想,如果一个很大的图像上,大部分像素值都在0 - 50范围内,但是有很小一块像素值在240的 ...

  5. Python3.7 下安装pyqt5

    第一步:首先进入python安装目录下的 [scripts]. 第二步:执行安装pyqt5的命令:python37 -m pip install pyqt5 出现以下安装过程代表安装成功. 第三步:在 ...

  6. 基于Spark的电影推荐系统

    数据文件: u.data(userid  itemid  rating  timestamp) u.item(主要使用 movieid movietitle) 数据操作 把u.data导入RDD, t ...

  7. mysql 官网下载太慢了,来这里!!!

    RT.去官网下载mysql简直是折磨,太慢了!!! 但我还是坚持住了,下载下来了,我自己下载的是 MAC 5.7.27版本,网盘分享下,有需要的自提吧: 链接:https://pan.baidu.co ...

  8. SpringMVC-JSON数据交换

    在上Springmvc-JSON数据交换的时候,老师提出了两个问题: 1.JSON数据交互两个注解的作用? 2.静态资源访问的几种配置方式,并简述? 老师刚提出这两个问题的时候我一头雾水的.JSON数 ...

  9. Gogs官方帮助文档

    环境要求 数据库(选择以下一项): MySQL:版本 >= 5.7 PostgreSQL MSSQL TiDB(实验性支持,使用 MySQL 协议连接) 或者 什么都不安装 直接使用 SQLit ...

  10. 17.AutoMapper 之配置(Configuration)

    https://www.jianshu.com/p/031ff68797dd 配置(Configuration) 通过构造函数创建并初始化MapperConfiguration实例: config = ...