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 ...
随机推荐
- Spring入门篇总结:
本文是对慕课网上"搞定SSM开发"路径的系列课程的总结,详细的项目文档和课程总结放在github上了.点击查看 视频传送门:Spring入门篇 该门课程主要从Spring的Bean ...
- spring boot 遇到 supported setting property http://xml.org/sax/properties/lexical-handler
解决链接:http://apache-fop.1065347.n5.nabble.com/org-xml-sax-SAXNotSupportedException-thrown-by-FOP-td11 ...
- Python简单小程序练习
1.九九乘法表 #!/usr/bin/python for i in range(1,10): for j in range(i): j += 1 print ("%d * %d = %-2 ...
- spring的注解使用
1.注解测试在xml里面配置<context:component-scan base-package="cn.ql"></component-scan>co ...
- 深入.NET数据类型(1)
一.值类型和引用类型 所有的值类型和引用类型的引用都存在"栈"中 1.值类型 命名空间:System.ValueType 值类型数据储存所在的内存区域成为栈 值类型主要包括基本数据 ...
- Java笔记:字符串详解
字符串详解 更新时间:2018-1-6 21:20:39 String 字符串创建 String str1="ABC";//推荐使用 String str2 = new Strin ...
- JS 监听微信、支付宝等移动app及浏览器的返回、后退、上一页按钮的事件方法
在实际的应用中,我们常常需要实现在移动app和浏览器中点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面或执行一些其它操作的需求: 那在代码中怎样监听当点击微信.支付宝.百度糯米.百度钱包 ...
- hadoop的安装和配置(一)本地模式
博主会用三篇文章来为大家详细的说明hadoop的三种模式: 本地模式 伪分布模式 完全分布模式 本地模式: 思路走向 |--------------------| | ①:配置Java环境 | | ...
- webapi 异步返回
两年前我遇到一个难题: https://q.cnblogs.com/q/78177 WebAPI中使用socket如果在server端回复了再返回值? 现在终于做出一种实现了: [HttpGet] ...
- 数据对象转json与md5加密注意事项
项目中遇到将OC数据对象类型转化字符类型,然后进行MD5加密的技术流程,在转化字符数组到字符加密过程中遇到一些问题. 问题 转化后的字符进行md5加密,出现与服务器加密结果不匹配的情况 分析 在对代码 ...