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 and B know each other, B and C know each other. But this may not imply that A and C know each other.
Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only
paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.
Calculate the maximum number of pairs that can be arranged into these double rooms.
InputFor each data set:
The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.
Proceed to the end of file.
OutputIf these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
Sample Output
No
3
tips:给这些点涂上黑白两色,就可以表示分出集合了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;
const int maxn = 205;
int n, m;
/*struct edge{
int from, to;
edge(int f, int t): from(f), to(t){}
};*/
vector <int> G[maxn];
//vector <edge> edges;
int col[maxn],pre[maxn], flag[maxn];
int check[maxn];
bool dfs(int u, int tar)//黑白标色 找到NO的情况 也就是路径上间隔1的点颜色相同
{
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(col[v] == -1){
check[v] = true;
col[v] = tar ^ 1;
if(!dfs(v, col[v])){
return false;
}
}
else if(col[v] == (tar ^ 1)) check[v] = true;
else if(col[v] == tar) return false;
}
return true;
}
int ffind(int u)//找增广路
{
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
if(!flag[v]){
flag[v] = true;
if(pre[v] == -1 || ffind(pre[v])){
pre[v] = u;
return true;
}
}
}
return false;
}
void init()
{
for(int i = 0; i < n; i++){
G[i].clear();
}
memset(pre, -1, sizeof(pre));
memset(col, -1, sizeof(col));
memset(check, false, sizeof(check));
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF){
//int k = 0;
init();
for(int i = 0; i < m; i++){
int f,t;
scanf("%d%d",&f,&t);
G[f - 1].push_back(t - 1);
//edge e(f - 1,t - 1);
//edges.push_back(e);
}
bool tar = false;
for(int i = 0; i < n; i++){
if(check[i]) continue;
col[i] = 1;
if(!dfs(i, col[i])){
tar = true;
break;
}
}
if(tar){
printf("No\n");
continue;
}
int sum = 0;
for(int i = 0; i < n; i++){
memset(flag, false, sizeof(flag));
sum += ffind(i);
}
printf("%d\n",sum);
}
return 0;
}
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(二分匹配 匈牙利算法 邻接表实现)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 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 (二分图存在的判定以及最大匹配数)
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.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 (偶图判定,匈牙利算法)
题意: 有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No.若能,则继续.在两组中挑两个认识的人(每组各1人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
随机推荐
- EditDistance,求两个字符串最小编辑距离,动态规划
问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...
- PHP易混淆函数的区别及用法汇总
本文实例分析了PHP易混淆函数的区别及用法.分享给大家供大家参考.具体分析如下: 1.echo和print的区别PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.ech ...
- centos 安装五笔
没有五笔怎么打字!ctrl + alt + [F2 - F6]进入控制台模式f2 - f6是五个控制台,想进哪个进哪个!进入之后用alt + [F2 - F6]来切换不同的控制台输入root / 密码 ...
- 查看一个dll是否是强命名[C#]
使用命令行工具SDK Command Prompt,键入:SN -T C:\*****.dll 就会显示出该dll具体的PublicKeyToken 数值. 如果该程序集没有强命名,则不会有Publi ...
- Linux 集群架构
集群介绍 Keepalived 配置高可用集群
- C++ template —— 模板与继承(八)
16.1 命名模板参数许多模板技术往往让类模板拖着一长串类型参数:不过许多参数都设有合理的缺省值,如: template <typename policy1 = DefaultPolicy1, ...
- JSON调试找不到 net.sf.ezmorph.Morpher
JSON中,java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher问题解决 使用JSON,在SERVLET或者STRUTS的ACTION中取得数据 ...
- (转载)Java反射机制
Java反射机制是Java语言被视为准动态语言的关键性质.Java反射机制的核心就是允许在运行时通过Java Reflection APIs来取得已知名字的class类的相关信息,动态地生成此类,并调 ...
- (原创)Windows下使用android ADT工具dmtracedump.exe绘图
在windows下使用dmtracedump绘图时,出现如下错误: 'dot' 不是内部或外部命令,也不是可运行的程序 或批处理文件. 应该是没有dot这个执行程序,安装:Graphviz程序,然后将 ...
- Glide加载图片缓存库出现——You cannot start a load for a destroyed activity
请记住一句话:不要再非主线程里面使用Glide加载图片,如果真的使用了,请把context参数换成getApplicationContext.