HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 500
bool G[maxn][maxn], vis[maxn];
int color[maxn], P[maxn];///黑白染色 -1 黑色 1 白色
int n, m; bool DFS(int u,int c)///判断是否是二分图,黑白染色
{
color[u] = c;
for(int i=; i<=n; i++)
{
if(!G[u][i] )
continue;
if(color[i] == )
{
if( DFS(i, -c) )
continue;
return false;
}
else if(color[i] + color[u])
return false; }
return true;
}
bool Find(int u)
{
for(int i=; i<=n; i++)
{
if(G[u][i] && !vis[i])
{
vis[i] = true;
if(P[i] == - || Find(P[i]))
{
P[i] = u;
return true;
}
}
}
return false;
} int solve()
{
bool ok;
for(int i=; i<=n; i++)
{
if(color[i] == )
ok = DFS(i, );
if(ok == false)
return -;
} int ans = ;
for(int i=; i<=n; i++)
{
memset(vis, false, sizeof(vis));
if(color[i] && Find(i))
ans ++;
}
return ans;
} int main()
{
while(scanf("%d %d",&n, &m) != EOF)
{
int a, b;
memset(G, false, sizeof(G));
memset(P, -, sizeof(P));
memset(color, , sizeof(color)); for(int i=; i<m; i++)
{
scanf("%d %d",&a, &b);
G[a][b] = true;
}
int ans = solve(); if(ans == -)
puts("No");
else
printf("%d\n", ans );
}
return ;
}
HDU 2444 The Accomodation of Students(判断是否可图 + 二分图)的更多相关文章
- 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 判断是否构成二分图 + 最大匹配
此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...
- HDU 2444 The Accomodation of Students(推断是否是二分图)
题目链接 题意:n个学生,m对关系,每一对互相认识的能住一个房间.问否把这些学生分成两组,要求每组的学生都互不认识.求最多须要多少个房间. 能否分成两组?也就是说推断是不是二分图,推断二分图的办法,用 ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- 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 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- linux下ssh免密登陆
如果 有A.B两台主机: 要实现的效果: A主机ssh登录B主机无需输入password: 加密方式选 rsa|dsa均能够.默认rsa 做法: 1.登录A主机 2.ssh-keygen -t [rs ...
- [译]C++如何切分字符串
声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. http://stackoverflow.com/questions/236129/how-to-split-a-string-in ...
- Gradle学习目录总结
如果是你想干的事情,在别人看起来可能是很难的一件事,不过你自己很喜欢,你不会觉得很苦.我开始创业那会是28岁.对我来讲,我创业的目的不是为了自己当老板,我希望有一个平台有一个环境,我可以控制一些资源, ...
- JavaScript自动关闭窗口
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- updatepanel的用法之triggers
triggers有的两种触发器asyncpostbacktrigger和postbacktrigger.asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepane ...
- JNI测试-java调用c算法并返回java调用处-1到20阶乘的和
一,java端: 定义native方法, 'public native long factorial(int n);', 该方法用c/c++实现,计算'1到20阶乘的和',参数中'int n'是前n项 ...
- 安装php时,make步骤报错make: *** [ext/gd/gd.lo] Error 1
安装PHP时,make步骤报错make: *** [ext/gd/gd.lo] Error 1 /usr/local/src/LAMP+memcahed+catci/php-5.4.0/ext/gd/ ...
- 015_xml_函数
015_xml_函数 --环境准备******************************************************************* USE test --f:/t ...
- WPF命令
WPF的命令是经常使用的,在MVVM中,RelayCommand更是用得非常多,但是命令的本质究竟是什么,有了事件为什么还要命令,命令与事件的区别是什么呢?MVVM里面是如何包装命令的呢?命令为什么能 ...
- centos U盘安装
1.版本 LiveCD 和 LiveDVD 是可以直接进入运行系统,类似win PE, 进入系统后有一个图标 install - HHD(从硬盘安装). netinstall 是用于网络安装和系统救援 ...