HDU 2444:The Accomodation of Students(二分图判定+匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2444
题意:给出边,判断这个是否是一个二分图,并求最大匹配。
思路:先染色法求出是否是一个二分图,然后再匈牙利求出最大匹配。注意输出是“No"而不是”NO“!!!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct node
{
int nxt, v;
}edge[*];
int head[], tot;
int col[];
bool vis[];
int match[]; void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} bool check(int u,int c)
{
col[u] = c;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(col[v] == c) return false;
if(col[v] == && !check(v, -c)) return false;
}
return true;
} bool dfs(int u)
{
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
} int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)) {
memset(match, -, sizeof(match));
memset(head, -, sizeof(head));
memset(col, , sizeof(col));
tot = ;
for(int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
bool f = ;
for(int i = ; i <= n; i++) {
if(col[i] == ) {
if(!check(i, )) {
f = ;
puts("No");
break;
}
}
}
if(!f) continue;
int sum = ;
for(int i = ; i <= n; i++) {
memset(vis, , sizeof(vis));
sum += dfs(i);
}
printf("%d\n", sum / );
}
return ;
}
HDU 2444:The Accomodation of Students(二分图判定+匹配)的更多相关文章
- 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 (二分图存在的判定以及最大匹配数)
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 - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
- HDU 2444 The Accomodation of Students (偶图判定,匈牙利算法)
题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
- 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 StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- 强制回收和IDisposable.Dispose方法
如果某对象的 Dispose 方法被调用一次以上,则该对象必须忽略第一次调用后的所有调用. 如果对象的 Dispose 方法被多次调用,该对象一定不要引发异常. 除Dispose 之外的实例方法在资源 ...
- java获取泛型的真实类型
ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass(); Type[] actualTyp ...
- 成员变量NSString类型指针的属性为什么用copy(属性)
创建一个分类Person设置属性@property(nonatomic,strong) NSString * name; 在- (void)viewDidLoad 中打印测试 name的属性是stro ...
- asp.net 登陆验证 Form表单验证的3种方式 FormsAuthentication.SetAuthCookie;FormsAuthentication.RedirectFromLoginPage;FormsAuthenticationTicket
我们在登陆成功后,使用下面的3种方法,都是同一个目的:创建身份验证票并将其附加到 Cookie, 当我们用Forms认证方式的时候,可以使用HttpContext.Current.User.Ident ...
- C# 6.0的新变化
自动属性初始化 (Initializers for auto-properties) 以前我们是这么写的 为一个默认值加一个后台字段是不是很不爽,现在我们可以这样写 只读属性的初始化(Getter-o ...
- Java基础之创建窗口——使窗口在屏幕居中(TryWindow2/TryWindow3)
控制台程序. 1.使用ToolKit对象在屏幕的中心显示窗口,将窗口的宽度和高度设置为屏幕的一半: import javax.swing.JFrame; import javax.swing.Swin ...
- mysql:on duplicate key update与replace into
在往表里面插入数据的时候,经常需要:a.先判断数据是否存在于库里面:b.不存在则插入:c.存在则更新 一.replace into 前提:数据库里面必须有主键或唯一索引,不然replace into ...
- editplus3运行Python程序
editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...
- CGRect 结构体的另外一种写法
// _textF = CGRectMake(textX, textY, textSize.width, textSize.height); _textF = (CGRect){{textX, ...
- 设置 BitmapData 没填充部分为透明色
默认 BitmapData 是以白色填充的 在初始化时,设定上每3,4个参数即可透明: new BitmapData(w,h,true,0) 第4个参数要为0