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人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
随机推荐
- Symbol.iterator的理解
https://blog.csdn.net/margin_0px/article/details/82971545
- python打造线程池
# coding=utf-8 import threading import Queue import time import traceback class ThreadPoolExecutor(o ...
- [原]IOS 后台发送邮件
skpsmtpmessage 是ios第三方后台发送邮件库 https://github.com/jetseven/skpsmtpmessage.git -(void)statrUpLoad:(id) ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 静态服务
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 静态服务 有时候希望人工管理服务提供者的上线和下线,此时需将注册中心标识为非动态管 ...
- COM组件没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
1.解决办法:在项目属性里设置“生成”=>“目标平台”为x86而不是默认的ANY CPU:
- python的httplib、urllib和urllib2的区别及用
慢慢的把它们总结一下,总结就是最好的学习方法 宗述 首先来看一下他们的区别 urllib和urllib2 urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Re ...
- RF实现多次失败重跑结果合并的基础方法和优化方法
实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...
- 一个简单的demo学习Android远程Service(AIDL的使用)
这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...
- linux下getsockopt和setsockopt详解及测试
linux下getsockopt和setsockopt详解及测试 NAME 名字 getsockopt, setsockopt - get and set options on sockets 获取或 ...
- SpringMVC系列之URL匹配问题
一.工程目录 二.web.xml配置文件及与其他文件的关系 三.控制器部分 四.返回值 五.url前后缀 六.项目源代码 http://files.cnblogs.com/files/xujian20 ...