HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4915 Accepted Submission(s): 2260
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.
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.
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
3
边数是个坑点,加到2W才过,WA好几次,题目本身比较水,就是拿来熟悉一下最大匹配的hungary算法,做了几道题发现这个算法主要思想就是不停地尝试匹配,比如A匹配B,如果B没被匹配过那就最好,匹配数+1;若B被匹配过,那就尝试看看匹配B的那个人(匹配B的此时肯定不是A)把B换掉,比如匹配B的是C,而C又可以匹配D,因此把C匹配的对象变成D,那B就空出来了,就可以被A匹配了。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
const int M=20010;
struct info
{
int to;
int pre;
}E[M];
int head[M],cnt,n,m;
int vis[N],match[N],color[N];
queue<int>Q;
void add(int s,int t)
{
E[cnt].to=t;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void init()
{
MM(head,-1);
MM(match,-1);
MM(color,0);
cnt=0;
while (!Q.empty())
Q.pop();
}
bool canmatch(int s)
{
int i;
color[s]=1;
Q.push(s);
while (!Q.empty())
{
int now=Q.front();
Q.pop();
for (i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!color[v])
{
color[v]=(color[now]==1?2:1);
Q.push(v);
}
else if(color[v]==color[now])
return false;
}
}
return true;
}
bool dfs(int now)
{
int i;
for (i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!vis[v])
{
vis[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=now;
match[now]=v;
return true;
}
}
}
return false;
}
int hung()
{
int r=0,i;
for (i=1; i<=n; i++)
{
MM(vis,0);
if(dfs(i))
r++;
}
return r;
}
int main(void)
{
int a,b,i;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
!canmatch(1)?puts("No"):printf("%d\n",hung());
}
return 0;
}
HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)的更多相关文章
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu2444(判二分图+最大匹配)
传送门:The Accomodation of Students 题意:有n个学生,m对相互认识的,问能否分成两队,使得每对中没有相互认识的,如果可以求最大匹配,否则输出No. 分析:判断二分图用染色 ...
- HDU 2063 过山车(模板—— 二分图最大匹配问题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2063 解题思路: 二分图最大匹配模板题. AC代码: #include<stdio.h> ...
- 【HDU 2063】过山车(二分图最大匹配模板题)
题面 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每 ...
- 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题
题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...
- 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...
- HDU 1068 Girls and Boys(模板——二分图最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...
- hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)
//推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...
- 过山车 HDU 2063 (二分图匹配裸题)
Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生 ...
随机推荐
- Json字符串转excel表格文件
假如我们有一段json串,该json串是由一系列结构相同的数据集合组成,如下: { "data": [ { "groupId": "com.test. ...
- Azure 项目构建 – 部署 Drupal 网站
通过完整流程详细介绍了如何通过 Azure Web 应用. MySQL DB on Azure 等服务在 Azure 平台上快速搭建 Drupal 服务器,并将其连接到 MySQL 数据库. 此系列的 ...
- mongodb-3.2.8 单机复制集安装
规划: replSet 复制集名称: rs1 MongoDB数据库安装安装路径为:/usr/local/mongodb/ 复制集成员IP与端口: 节点1: localhost:28010 (默认的 ...
- COGS 1578. 次小生成树初级练习题
☆ 输入文件:mst2.in 输出文件:mst2.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 求严格次小生成树 [输入格式] 第一行包含两个整数N 和M,表 ...
- 设置windows status bar隐藏
info.plist View controller-based status bar appearance 为 NO CGContextSaveGState: invalid context 0x0 ...
- SVN中的check out与export的区别
http://blog.csdn.net/zndxlxm/article/details/7763116 check out跟check in对应,export跟import对应. check out ...
- ndarray数组自动创建
为了实现某些运算,需要快速构造符合要求的大数组 Numpy函数生成的数组,如不指定类类型,几乎全为浮点型(arange除外,它是整形),因为科学计算中测量值,例如温度.长度,都是浮点数 import ...
- NASM 之 helloworld1
SECTION .data msg: db "Hello World!", 0x0a len: equ $-msg SECTION .text global _main kerne ...
- 如何关闭OSX 10.11 SIP (System Integrity Protection)
http://www.jianshu.com/p/0572336a0771 注意:SIP功能是Apple在OSX上推出的系统完整性保护功能,对于普通MAC用户来说是一项安全保护功能,如果不了解他的作用 ...
- 网络流的$\mathfrak{Dinic}$算法
网络流想必大家都知道,在这不过多赘述.网络流中有一类问题是让你求最大流,关于这个问题,许多计算机学家给出了许多不同的算法,在这里--正如标题所说--我们只介绍其中的一种--\(\tt{Dinic}\) ...