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 ( ...
随机推荐
- ACM-ICPC之路
自从了解到了ACM,我就坚定了参加这个比赛的信心.虽然零基础开始,但是阻挡不了我的前进之路.从大一上学期的完成二十道题,到假期完成四十道题:从第一次校赛不了解退出循环方式只完成了一道题,到大一预选赛第 ...
- Docker 总结
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] Docker总结 简单介绍 1 Docker 架构 安装和环境配置 1 mac 11 brew安装 11 dmg文件安装 1 ...
- 用sql实现汉字转拼音
有时我们会需要将汉字转为拼音,例如需要将省市转为拼音后当做编码存储(尽管国家有统一的标识码,但有时候我们还是会用到),网络上也有工具提供汉字转拼音的功能,但各有优劣,一般转拼音后还会存在带声调的字母, ...
- HTML canvas图像裁剪
canvas drawImage方法的图像裁剪理解可能会比较耗时,记录一下,以便供人翻阅! context.drawImage(img,sx,sy,swidth,sheight,x,y,width,h ...
- 写入XML文件
public static void writeXMLFile(Document doc,String xmlFileName) throws IOException{ OutputFormat f ...
- UNICODE与ANSI的区别
什么是ANSI,什么又是UNICODE呢?其实这是两种不同的编码方式标准,ANSI中的字符采用8bit,而UNICODE中的字符采用16bit.(对于字符来说ANSI以单字节存放英文字符,以双字节存放 ...
- Strusts2--课程笔记7
国际化: 国际化是指,使程序在不做任何修改的情况下,就可以使用在不同的语言环境中.国际化在一般性项目中是不常用的.在编程中简称 i18n. 国际化是通过读取资源文件的形式实现的.资源文件的定义与注册, ...
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- Qt 5.7 > Qt Applications
本文翻译自Qt官方文档: http://doc.qt.io/qt-5/qmlapplications.html QML 应用 QML是声明式语言,它使得用户界面以及交互行为可以被"描述&qu ...
- Java中自己实现枚举
public class MyEnum { private final String name; public static final MyEnum red = new MyEnum(" ...