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人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
随机推荐
- [Arch] 02. Design principle and Software Pattern
Ref: 软件设计的七大原则 有时间的话,还需进一步深入理解. Figure, 重要的前五个原则 单一职责原则 (Simple responsibility pinciple SRP) 类的设计趋向于 ...
- 在Linux上安装jdk,mysql,tomcat的准备工作
准备工作: 因为JDK,TOMCAT,MYSQL的安装过程中需要从网上下载部分支持包才可以继续,所以要提前安装下载好下面四个依赖 yum install glibc.i686 yum -y insta ...
- C#中的Abstract、Virtual、Interface理解
容易混淆是必须的,都是与继承有关系,并且涉及到override的使用 一.Virtual方法(虚方法) virtual 关键字用于在基类中修饰方法.virtual的使用会有两种情况: 情况1:在基类中 ...
- java命令启动jar包
本人对这些命令也是一知半解,记录备用. 1. 使用java命令行执行java文件 # 设置命令窗口标题 title test1 # 开启输出 @echo on # 设置环境变量JAVA_HOME se ...
- hbase2.0.0-安装部署
依赖hadoop 环境,我这边的版本是hadoop-2.6.5 选择hbase2.0.0版本的时候,去官网查看支持的hadoop版本 1.伪分布式安装 下载:http://mirror.bit.edu ...
- 执行RF设置顶级测试套件的名称
场景1:通过pybot进行单个output文件情况下设置 -N --name name 设置顶级测试套件的名称.名称中的下划线将转换为空格. 默认名称为执行的数据源的名称. 场景2:通过rebot进行 ...
- U3D各键值说明
KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键. 值 对应键 Backspace 退格键 Delete Delete ...
- 集群瓶颈为什么是磁盘io
阅读本文思考: 1.对磁盘IO了解多少 2.为什么是磁盘IO是瓶颈,有没有自己的答案 想了解磁盘io可以查看此帖:集群瓶颈:磁盘IO必读 (磁盘IO:磁盘输出输出) 集群的瓶颈提出多种看法,其中网络和 ...
- Linux平台上用C语言实现与MySQL数据库的连接
安装编译工具 ---- 这将安装gcc/g++/gdb/make 等基本编程工具: sudo apt-get install build-essential 输入命令"sudo apt-ge ...
- 《Lua程序设计》第3章 表达式 学习笔记
3.1 算术操作符“+”(加法).“-”(减法).“*”(乘法).“/”(除法).“^”(指数).“%”(取模).3.2 关系运算符< > <= >= == ~=3.3 逻辑操 ...