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(二分图的判定和计算)的更多相关文章

  1. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  2. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. HDU2444 The Accomodation of Students —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...

  4. 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 ...

  5. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

  6. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  7. HDU2444 The Accomodation of Students(二分图最大匹配)

    有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...

  8. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  9. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

随机推荐

  1. iOS实现类似QQ的好友列表,自由展开折叠(在原来TableView的基础上添加一个字典,一个Button)

    //直接代码 只包含 折叠展开字典的处理搭建#import "CFViewController.h" @interface CFViewController ()<UITab ...

  2. iOS App稳定性指标及监测

    一个App的稳定性,主要决定于整体的系统架构设计,同时也不可忽略编程的细节,正所谓"千里之堤,溃于蚁穴",一旦考虑不周,看似无关紧要的代码片段可能会带来整体软件系统的崩溃.尤其因为 ...

  3. bzoj 2427: [HAOI2010]软件安装

    Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...

  4. js-使用JavaScript、jQuery两种方式实现全选/全不选

    html代码 <input type='checkbox' value="10" name="frust"/>苹果10元 <br/> & ...

  5. c#发展前景

    根据育龙网资料评价显示:C#几乎集中了所有关于软件开发和软件工程研究的最新成果:面向对象.类型安全.组件技术.自动内存管理.跨平台异常处理.版本控制.代码安全管理…….尽管像很多人注意到的一样,罗列上 ...

  6. Another option for file sharing(转)

    原文地址  https://security.googleblog.com/2017/02/another-option-for-file-sharing.html Another option fo ...

  7. Xamarin~Android篇~监听返回键,单击返回某个webView,双击退出

    https://www.cnblogs.com/lori/p/5088627.html DateTime? lastBackKeyDownTime; public override bool OnKe ...

  8. Java求循环节长度

    两个整数做除法,有时会产生循环小数,其循环部分称为:循环节.比如,11/13=6=>0.846153846153.....  其循环节为[846153] 共有6位.下面的方法,可以求出循环节的长 ...

  9. robotframework的学习笔记(十三)------Robot Framework常用库简介

    标准库 Robot Framework可以直接导入使用的库,包括: Builtin:包含经常需要的关键字.自动导入无需import,因此总是可用的 Dialogs:提供了暂停测试执行和从用户的输入方式 ...

  10. Python中将函数作为另一个函数的参数传入并调用

    在Python中,函数本身也是对象,所以可以将函数作为参数传入另一函数并进行调用 在旧版本中,可以使用apply(function, *args, **kwargs)进行调用,但是在新版本中已经移除, ...