hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算)
标签:二分图匹配
题意:
问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两认识的人可以去开房哈。求最大的开房数。
题解:
二分染色,黑白染色来看这个图可以被染成一个二分图,如果可以的话求出二分图匹配的个数即可
注意事项:
dfs来染色,在求解的时候注意点的编号是从0还是从1开始的
代码:
//二分图最好用链表存图
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1010;
int vis[N];
int v[N];
int head[N];
struct Edge{
int to;
int next;
}edge[N*N];
int Ecnt;
int rm[N];
int id[N];
void init()
{
Ecnt = 0;
memset(vis,0,sizeof(vis));
memset(id,-1,sizeof(id));
memset(rm,-1,sizeof(rm));
memset(v,0,sizeof(v));
memset(head,-1,sizeof(head));
}
void add(int from, int to){
edge[Ecnt].to = to;
edge[Ecnt].next = head[from];
head[from] = Ecnt++;
edge[Ecnt].to = from;
edge[Ecnt].next = head[to];
head[to] = Ecnt++;
}
bool dfs(int s,int type){
id[s] = type;
for(int i = head[s]; i != -1; i = edge[i].next)
{
int t = edge[i].to;
if(id[t]!=-1&&id[t]!=!type) return 0;
if(v[t]==0){
v[t] = 1;
if( dfs(t,!type)==0) return 0;
}
}
return 1;
}
int list(int s){
for(int i = head[s]; i!= -1; i= edge[i].next){
int t = edge[i].to;
if(vis[t]) continue;
vis[t] = 1;
if(rm[t]==-1||list(rm[t])){
rm[t] = s;
return 1;
}
}
return 0;
}
int Max_match(int n)
{
int ans = 0;
for(int i = 0; i < n; i++){
memset(vis,0,sizeof(vis));
vis[i] = 1;
if(list(i)) ans++;
}
return ans;
}
int main()
{
int n,m;
int x, y;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i = 0; i < m; i++){
scanf("%d%d",&x,&y);
x--;y--;//加边的时候要注意点的编号是从什么开始的
add(x,y);
}
bool fl = 1;
for(int i = 0; i < n; i++){
if(v[i])continue;
if(dfs(i,0)==0){fl = 0;continue;}
}
if(fl == 0) puts("No");
else {
// puts("haha");
int ans = Max_match(n);
printf("%d\n",ans/2);
}
}
return 0;
}
hdu_2444The Accomodation of Students(二分图的判定和计算)的更多相关文章
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...
- HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)
There are a group of students. Some of them may know each other, while others don't. For example, A ...
- 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 ...
- HDU2444 The Accomodation of Students(二分图最大匹配)
有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...
- 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 ...
随机推荐
- iOS 进阶—— iOS 内存管理
1 似乎每个人在学习 iOS 过程中都考虑过的问题 alloc retain release delloc 做了什么? autoreleasepool 是怎样实现的? __unsafe_unretai ...
- Java I/O---Properties类(持久化键值对)
1.Properties类简介 Properties类(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这 ...
- 解决author波浪线Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click
自从把默认的头注释的author改成自己的名字以后越看越顺眼,但是发现名字下面一直有个波浪线,强迫症简直不能忍. 然后当你把鼠标放上去,再点击提示上的"more",会看到下面的提示 ...
- eKing Cloud基础云平台演进之路
出口转内销.首发于公司微信公众号,作者本人,现转载到此.本来写得比较技术,还算有点干货,但是结果被编辑咔咔咔,就只剩下下面这些内容. 大型企业如何开启自己的快速上云之路? 2017-12-08 易建科 ...
- Qt编写导航按钮
做各种各样的界面的时候,经常需要做一排按钮用于切换到对应界面,俗称导航按钮或者导航菜单,参照过各种各样的主界面导航布局,特意编写导航按钮自定义控件,结合各种情况,继承自QPushButton.已集成在 ...
- Unable to make the module: related gradle configuration was not found. Please, re-import the Gradle project and try again
到stack overflow找到的答案,老外还是专业 I also had a similar problem, Go to : View -> Tool Windows -> Grad ...
- 快速恢复开发环境(系统还原后的思考,附上eclipse注释的xml配置文件)
1.Eclipse/Myeclipse的工作空间,不能放在系统盘 除非你的项目都有实时的云同步或SVN等,才能放在系统固态盘,不然你享受快速启动项目的同时,也需要承担系统奔溃后找不回项目的风险: 公司 ...
- 前端之JavaScript--基础
JavaScript 独立的语言,浏览器具有js解释器 一. JavaScript代码存在形式: - Head中 <script> //javascript代码 alert(123); & ...
- Head First设计模式之访问者模式
一.定义 定义:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 访问者模式适用于数据结构相对稳定的系统, 它把数据结构和作用于数据结构之上的操 ...
- Zabbix 单位换算
直接举一例子,然后再举一反三: 如图: 单位B 则基数为1024(倍数) 我性能参数为KB单位,我们则把单位转换成和我们计数器 保持一致的单位即可,一致后,zabbix 后面会自己准换成自己想要的显示 ...