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 ...
随机推荐
- sv_target_output dx11
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx
- cookie中转注入实战
随着网络安全技术的发展,SQL注入作为一种很流行的攻击方式被越来越多的人所知晓.很多网站也都对SQL注入做了防护,许多网站管理员的做法就是添加一个防注入程序.这时我们用常规的手段去探测网站的SQL注入 ...
- java 中 ==和equals 的区别
Java中equals和==的区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolea ...
- rsync 文件校验及同步原理及rsync server配置
参考:http://rsync.samba.org/how-rsync-works.html 我们关注的是其发送与接收校验文件的算法,这里附上原文和我老婆(^_^)的翻译: The Sender Th ...
- 一些linux的问题
本文罗列的是我在学习linux与shell编程时所遇到的一些问题.我相信既然存在问题那么就会有需求,记录于此,希望可以快速帮助到大家. 1.在vim中用“/word"查找后,vim会以棕色背 ...
- Servlet编写登录界面
package com.mhb; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servlet ...
- Java API —— Date类
1.Date类概述 类 Date 表示特定的瞬间,精确到毫秒. 2.构造方法 public Date():分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒). public Dat ...
- spring-boot-quartz, 依赖spring-boot-parent
spring-boot-quartz, 依赖spring-boot-parent spring-boot Easyui Quartz 项目启动后输入:http://localhost/ 数据库文件: ...
- sqlsevrer中output的用法
近日,看到代码中有output写法,不知其意,经过一番查找,终于找到了原因,它的作用是将修改影响的结果给输出出来. 比如update语句, 除了修改数据以外, 对于发生更新的列, update语句还可 ...
- HDU 4633 Who's Aunt Zhang(polay计数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4633 题意:有下面一个魔方.有K种颜色.可以为顶点.边.面(每个面有9个小面)染色.两种染色算作一种当 ...