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人)到一个双人房.输出需要多少个双人房? 思路: 先判定是 ...
随机推荐
- MTK 关闭安全模式
1.当 Android 设备在安全模式(Safe Mode)下工作时,任何的第三方应用程序或相关文件(主要为apk应用程序文件)都不可以使用,但可以使用 Android 设备的任务管理器选项进行卸载或 ...
- [Scikit-learn] 2.1 Clustering - Gaussian mixture models & EM
原理请观良心视频:机器学习课程 Expectation Maximisation Expectation-maximization is a well-founded statistical algo ...
- Java利用while循环计算1+1/2!+1/3!……+1/20!
编写程序,用while语句计算1+1/2!+1/3!……+1/20!,并在控制泰山输出计算结果.要求1+1/2!+1/3!……+1/20!,其实就是求1+1*1/2+1*1/2*1/3+……+1*1/ ...
- Android学习——在Android中使用OpenCV的第一个程序
刚開始学习Android,因为之前比較熟悉OpenCV,于是就想先在Android上执行OpenCV试试 =============================================== ...
- ios开发之--跳转到指定的TabBarViewController中的某一个VIewController
比较简单,也很实用,方法大同小异,仅做记录,方法的系统记录如下: [self dismissViewControllerAnimated:YES completion:^{ // 这是从一个模态出来的 ...
- linux下kill -9 pid 强制不能杀掉进程原因
今天安装集群的时候,发现一个进程一直存在,kill -9 pid 也干不掉,就找找原因了. kill -9发送SIGKILL信号将其终止,但是以下两种情况不起作用:a.该进程处于"Zomb ...
- CentOS6.5下安装Oracle11g
一.安装前系统准备 1. 修改主机名 #sed -i "s/HOSTNAME=localhost.localdomain/HOSTNAME=oracledb.01/" /etc/s ...
- (原)一句mpAudioPolicy->get_input引发的血案
今天分析Android的Audio系统时,对mpAudioPolicy->get_input进行了分析,没想到这一句话的背后如此复杂,简直是一句话引出的血案啊! 分析结果如下:(关于排版:各个变 ...
- Linux平台下mysql的ODBC配置方法
在安装配置之前, 需要先大概了解一下MyODBC的架构. MyODBC体系结构建立在5个组件上,如下图所示: Driver Manager: 负责管理应用程序和驱动程序间的通信, 主要功能包括: 解析 ...
- 网络编程之HttpClient类(转)
12.2 网络编程之HttpClient类 除了可以使用HttpWebRequest类来实现HTTP网络请求之外,我们还可以使用HttpClient类来实现.对于基本的请求操作,HttpClient类 ...