The Accomodation of Students(判断二分图以及求二分图最大匹配)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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.
Input
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.
Output
Sample Input
Sample Output
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int n,m;
int f[];
int A[],B[],match[],book[];
vector <int> V[];//邻接表储存边的关系
void init()
{
memset(match,,sizeof(match));
for (int i=;i<=n;i++)
V[i].clear();
for (int i=; i<=*n; i++)
f[i]=i;
}
int find(int x)
{
int r=x,i=x,t;
while (r!=f[r]) r=f[r];
while (i!=r)
{
t=f[i];
f[i]=r;
i=t;
}
return r;
}
void mix(int x,int y)
{
int fx=find(x),fy=find(y);
f[fx]=fy;
}
bool IsTwo()//染色法求二分图
{
memset(book,,sizeof(book));
queue <int> Q;
Q.push();
book[]=;
while (!Q.empty())
{
int temp=Q.front();
Q.pop();
for (int i=;i<V[temp].size();i++)
{
int num=V[temp][i];
if (book[num]==)
{
book[num]=-book[temp];
Q.push(num);
}
else if (book[num]==book[temp]) return ;
}
}
return ;
}
int dfs(int u)
{
for (int i=; i<V[u].size(); i++)
{
int pos=V[u][i];
if (book[pos]==)
{
book[pos]=;
if (match[pos]==||dfs(match[pos]))
{
match[pos]=u;
return ;
}
}
}
return ;
}
int solve()
{
int ans=;
for (int i=; i<=n; i++)
{
memset(book,,sizeof(book));
if (dfs(i)) ans++;
}
return ans;
}
int main()
{
int a,b;
while (scanf("%d%d",&n,&m)>)
{
init();
int ok=;
while (m--)
{
scanf("%d%d",&a,&b);
if (find(a)==find(b))
ok=;
if (ok)
{
mix(a,b+n);
mix(b,a+n);
V[a].push_back(b);
V[b].push_back(a);
}
}
if (!ok)
{
puts("No");
continue;
}
/*if (!IsTwo())
{
puts("No");
continue;
}*/
printf("%d\n",solve()/);
}
return ;
}
The Accomodation of Students(判断二分图以及求二分图最大匹配)的更多相关文章
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- 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 ...
- HDU 2444 The Accomodation of Students(推断是否是二分图)
题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...
- hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HD2444The Accomodation of Students(并查集判断二分图+匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- 1张图看懂RAID功能,6张图教会配置服务器【转】
RAID 包含一组或者一个集合甚至一个阵列.使用一组磁盘结合驱动器组成 RAID 阵列或 RAID 集.将至少两个磁盘连接到一个 RAID 控制器,而成为一个逻辑卷,也可以将多个驱动器放在一个组中.一 ...
- linux下常见问题
1.配置path 环境变量 # vim /etc/profile 在文档最后,添加: export PATH="/opt/STM/STLinux-2.3/devkit/sh4/bin:$PA ...
- svn is already locked解决办法
在出错文件夹下,鼠标右键
- CVE-2016-0143 漏洞分析(2016.4)
CVE-2016-0143漏洞分析 0x00 背景 4月20日,Nils Sommer在exploitdb上爆出了一枚新的Windows内核漏洞PoC.该漏洞影响所有版本的Windows操作系统,攻击 ...
- 从NPM到CNPM
从NPM到CNPM 原文 http://www.cnblogs.com/hufeng/p/5166479.html 主题 npm 引用NPM网站上的一句话:npm loves you ! NPM ...
- linux下安装nginx+php
参考:http://blog.csdn.net/ihelloworld/article/details/7029796 http://blog.chinaunix.net/uid-21374062-i ...
- LYF电子书制作工具(CHM格式)
可以制作CHM电子书 可以制作电子相册 下载地址:http://files.cnblogs.com/files/blogLYF/LYF电子书制作安装包.zip http://files.cnblogs ...
- Vue.js + Seajs 实例(包含vue-router使用)
这个Demo 相关JS: Sea.js : Version 2.3.0 seajs-text : Version 2.3.0 vue.js : Version 1.0.24 vue-router: ...
- 《Windows编程循序渐进》——MFC封装机制详解
单文档
- hdu_2328_Corporate Identity(暴力枚举子串+KMP)
题目链接:hdu_2328_Corporate Identity 题意: 给你n个串,让你找这n个串的最大公共子串 题解: 串比较小,暴力枚举第一个的子串,然后KMP判断是否可行 #include&l ...