HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)
题意:
有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No。若能,则继续。在两组中挑两个认识的人(每组各1人)到一个双人房。输出需要多少个双人房?
思路:
先判定是否为二分图,可以用黑白着色法(DFS或BFS都行)。若是二分图,再进行匹配,用匈牙利算法,注:给的是整个图,没有区分男女,用邻接表比较好。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=;
vector< vector<int> > vect;
int col[N];
int girl[N];
int vis[N];
int n, m; bool color(const int x) //x可达的点着色
{
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(col[x]==col[t]) return false; //出错了,同色
if(!col[t])
{
col[t]=-col[x];//性别标为1和2
if(color(t)==false) return false; //只有刚着色的才需要深搜
}
}
return true; //只有单个点才会到这
} bool is2()//二分图判断,注意其可能并不是个连通图
{
memset(col,,sizeof(col));
for(int i=; i<=n; i++) //黑白着色
{
if(!col[i]) //可能有多个连通分量
{
col[i]=;
if(!color(i))
{
//cout<<i<<endl;
return false;
}
}
}
return true;
} int find(int x)
{
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!vis[t])
{
vis[t]=;
if(!girl[t] || find(girl[t])) //未匹配,或能为其对象另匹配
{
girl[t]=x;
return true;
}
}
}
return false;
} int hungary()
{
int cnt=;
memset(girl,,sizeof(girl));
for(int i=; i<=n; i++)
{
memset(vis,,sizeof(vis));
if(find(i)) cnt++;
}
return cnt;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, c;
while(cin>>n>>m)
{
vect.clear();
vector<int> tmp;
for(int i=; i<=n; i++) vect.push_back(tmp);//切忌用resize,会惨死在这 for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
vect[a].push_back(b);
vect[b].push_back(a);
}
if(!is2()&& (puts("No"),) ) continue; //仅仅为了少一行代码
printf("%d\n",hungary()>>); //二分图匹配,匈牙利
}
return ;
}
AC代码
HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)的更多相关文章
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- 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 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU 2444 The Accomodation of Students二分图判定和匈牙利算法
本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- 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 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 ...
随机推荐
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...
- setDepthStencilState
cgfx->hlsl StencilFunc={always,1,0xff}->setDepthStencilState(DepthState,0) StencilFunc={always ...
- 导致Asp.Net站点重启的10个原因 ,记录重启原因
Asp.Net站点有时候会莫名其妙的重启,什么原因导致的却不得而知,经过一番折腾后,我总结了导致Asp.Net站点重启的10个原因 1. 回收应用程序池会导致站点重启,记录的原因是: HostingE ...
- 翻译:用Javascript的Function构造器伪造上下文 by Ben Nadel
在我的jQuery模板标记语言(JTML)项目中,我需要一种方式将JTML模板编译到JS函数,这样它们就可以如期地在任何时候转换成新的HTML标记.但这是一个严峻的问题,因为JTML代码涉及非作用域( ...
- Test a ; vs Test a( ) ;
一. Test a(); Test a; //前提声明了Test类 前者声明一个返回值为Test,名为a的函数,后者声明了Test类的一个对象(把Test当成int) struct Test{ ...
- Graham's Scan算法
原文链接:http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html C++/STL实现: #include <algorith ...
- hdu 4324 Triangle LOVE(拓扑排序,基础)
题目 /***************************参考自****************************/ http://www.cnblogs.com/newpanderking ...
- Genymotion加载so出错解决方案
通过网上所搜得出结论: Genymotion是x86的架构,而我们的so库是arm架构的 解决:安装Genymotion-ARM-Translation.zip 1.下载:http://pan.bai ...
- 李洪强iOS开发之OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...