hdu2444The Accomodation of Students
思路:
二分图判断+最大匹配模板
二分图判断的方法很好想,没有离散的基础凭空给你个图让你判断也很容易想到染色法,简单的介绍下就是用queue来做,标记一个点为x则他所有的邻点都为x',然后递归的执行下去。
接下来会面临一个比较有趣的问题,我们确定现在的图是二分图,然后我们要求它的最大匹配——这里涉及到一个很关键的问题,就是一个图我们说他自己是一个二分图,那么是他内部的一些点会分成两部分,分别写成两列变成了形式上的二分图。而我们用find求二分图的时候是分别写成两列的话是一个图的所有点,因此总数最后是要除以2的。
AC代码:
#include <iostream>
#include <cstring>
#include <queue>
#define maxn 207
#define INF 9999999
using namespace std; int n,m;
int G[maxn][maxn];
int mark[maxn];
int vis[maxn];
int match[maxn];
int x,y; bool find(int x)
{
for(int i = ;i <= n;i++)
if(!vis[i] && G[x][i])
{
vis[i] = ;
if(match[i]==- || find(match[i]))
{
match[i] = x;
return true;
}
}
return false;
} int main()
{
while(cin>>n>>m)
{
int flag = ;
queue<int> qv;
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
memset(mark,-,sizeof(mark));
int t1,t2;
for(int i = ;i <= m;i++)
{
cin>>t1>>t2;
G[t1][t2] = G[t2][t1] = ;
}
mark[] = ;
qv.push();
while(!qv.empty())
{
int col = qv.front();
qv.pop();
if(vis[col]) continue;
vis[col] = ;
for(int i = ;i <= n;i++)
if(G[col][i])
{
if(mark[i] == -) {
mark[i] = mark[col]==?:;
qv.push(i);
}
else if(mark[i] != mark[col])
continue;
else {
flag = ;
break;
}
}
if(flag == ) break;
}
if(!flag) {
cout<<"No"<<endl;
continue;
}
int ans = ;
memset(match,-,sizeof(match));
for(int i = ;i <= n;i++)
{
memset(vis,,sizeof(vis));
if(find(i)) ans++;
}
cout<<ans/<<endl;
}
return ;
}
hdu2444The Accomodation of Students的更多相关文章
- hdu2444The Accomodation of Students (最大匹配+推断是否为二分图)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HDU-2444-The Accomodation of Students(二分图判定,最大匹配)
链接: https://vjudge.net/problem/HDU-2444#author=634579757 题意: There are a group of students. Some of ...
- hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)
题意 首先判断所有的人可不可以分成两部分,每部分内的所有人都相互不认识.如果可以分成 则求两部分最多相互认识的对数. 解题 类似分成两组,同组互不相关,就可能使判断是否为二分图 能否分成两部分 则是判 ...
- HDOJ 2444 The Accomodation of Students
染色判读二分图+Hungary匹配 The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- HD2444The Accomodation of Students(并查集判断二分图+匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 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(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
随机推荐
- Bottle 中文文档
译者: smallfish (smallfish.xy@gmail.com) 更新日期: 2009-09-25 原文地址: http://bottle.paws.de/page/docs (已失效) ...
- linux grep常用参数
# grep [-acinv] [--color=auto] '搜寻字符串' filename选项与参数:-c :计算找到 '搜寻字符串' 的次数-i :忽略大小写的不同,所以大小写视为相同-n :顺 ...
- Eclipse利用代理快速安装插件
在eclipse启动时增加以下参数: eclipse.exe -vmargs -DproxySet=true -DproxyHost=aProxyAddress -DproxyPort=aProxyP ...
- 常用的CSS属性
1.CSS背景属性(background) 属性 描述 background 在一个声明中设置所有的背景属性 background-attachment 设置背景图像是否固定或者随着页面的其余部分滚动 ...
- DNN模块开发之利器篇:七种武器
我们在进行DNN模块开发时经常需要调用Dotnetnuke.dll中的方法函数,模块开发用到DNN的方法函数会让你的开发更加得心应手,下面我们就来介绍一下. 1) PortalModuleBase ...
- javascript 广告移动特效
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- python记录
1. 序列的分片操作:需要提供两个索引作为边界,第1个索引的元素包含在分片内,第2个索引的元素不包含在分片内. 为了能让分片部分能够包含列表的最后一个元素,必需提供最后一个元素的下一个元素所对应的索引 ...
- jQuery1.9(辅助函数)学习之——.serializeArray();
.serializeArray();返回一个Array 描述: 将用作提交的表单元素的值编译成拥有name和value对象组成的数组.例如[ { name: a value: 1 }, { name: ...
- PHPCMS V9二次开发便捷自定义后台入口文件夹
phpcms v9二次开发便捷自定义后台入口文件夹 最新发布的phpcms v9由于采用了mvc的设计模式,所以它的后台访问地址是固定的,虽然可以通过修改路由配置文件来实现修改,但每次都修改路由配置文 ...
- js鼠标滑动图片显示隐藏效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...