The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8939    Accepted Submission(s): 3925
Problem Description
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.
 
Input
For 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.
 
Output
If 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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0xffffff
using namespace std;
const int my_max = ; int nn, mm, n, m, a, b, my_line[my_max][my_max], my_G[my_max][my_max],
my_left[my_max], my_right[my_max], my_color[my_max], my_book[my_max]; bool my_dfs(int x)
{
for (int i = ; i <= n; ++ i)
{
if(my_color[i] == && !my_book[i] && my_G[x][i])
{
my_book[i] = ;
if (!my_right[i] || my_dfs(my_right[i]))
{
my_right[i] = x;
return true;
}
}
}
return false;
} bool my_bfs(int x)
{
queue <int> Q;
Q.push(x);
my_color[x] = ; while (!Q.empty())
{
int my_now = Q.front();
for (int i = ; i <= n; ++ i)
{
if (my_G[my_now][i])
{
if (my_color[i] == -)
{
Q.push(i);
my_color[i] = !my_color[my_now];
} else if (my_color[my_now] == my_color[i])
return true;
}
}
Q.pop();
} return false;
} int my_hungarian()
{
int my_ans = ; for (int i = ; i <= n; ++ i)
{
memset(my_book, , sizeof(my_book));
if (my_color[i] == && my_dfs(i))
my_ans ++;
}
return my_ans;
} int main()
{
while (~scanf("%d%d", &n, &m))
{
memset(my_line, , sizeof(my_line));
memset(my_right, , sizeof(my_right));
memset(my_color, -, sizeof(my_color));
memset(my_G, , sizeof(my_G)); while (m --)
{
scanf("%d%d", &a, &b);
my_G[a][b] = my_G[b][a] = ;
} bool flag_is_bipG = true;
for (int i = ; i <= n; ++ i)
if (my_color[i] == - && my_bfs(i))
{
flag_is_bipG = false;
printf("No\n");
break;
}
if (!flag_is_bipG) continue; printf("%d\n", my_hungarian());
}
return ;
}

hdu 2444 The Accomodation of Students (判断二分图,最大匹配)的更多相关文章

  1. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  2. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  4. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

  5. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  6. hdu 2444 The Accomodation of Students 判断是否构成二分图 + 最大匹配

    此题就是求最大匹配.不过需要判断是否构成二分图.判断的方法是人选一点标记为红色(0),与它相邻的点标记为黑色(1),产生矛盾就无法构成二分图.声明一个vis[],初始化为-1.通过深搜,相邻的点不满足 ...

  7. HDU 2444 The Accomodation of Students(二分图判定+最大匹配)

    这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...

  8. 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 ...

  9. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

随机推荐

  1. 数据挖掘:关联规则的apriori算法在weka的源码分析

    相对于机器学习,关联规则的apriori算法更偏向于数据挖掘. 1) 测试文档中调用weka的关联规则apriori算法,如下 try { File file = new File("F:\ ...

  2. CSS中重要的BFC

    CSS中有个重要的概念BFC,搞懂BFC可以让我们理解CSS中某些原本看似诡异的地方. 简介 在解释BFC之前,先说一下文档流.我们常说的文档流其实分为定位流.浮动流.普通流三种.而普通流其实就是指B ...

  3. 基于STL的堆略解

    什么是STL 以下内容摘自这儿. STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Le ...

  4. Python开发【第十一篇】函数

    函数 什么是函数? 函数是可以重复执行的语句块,可以重复调用并执行函数的面向过程编程的最小单位. 函数的作用: 函数用于封装语句块,提高代码的重用性,定义用户级别的函数.提高代码的可读性和易维护性. ...

  5. CVE-2016-7124漏洞复现

    CVE-2016-7124漏洞复现 __wakeup()魔术方法绕过 实验环境 操作机:Windows 10 服务器:apache 2.4 数据库:mysql 5.0 PHP版本:5.5 漏洞影响版本 ...

  6. Libevent::evhttp服务器下载

    void http_handler_Get_Download(struct evhttp_request *req, void *arg) { if (req == NULL) { return; } ...

  7. javascript学习总结之对象的深拷贝和浅拷贝

    前言 最近在写ES6的文章的时候发现重复遇到关于javascript深拷贝和浅拷贝的问题,然后查找了一些资料,根据资料和自己的理解做了以下笔记,毕竟javascript关于深拷贝和浅拷贝的问题在一些面 ...

  8. 玩转PubSubClient MQTT库

    1.前言     在ESP8266学习系列中,博主一直使用HTTP协议.HTTP连接属于短连接,而在物联网应用中,广泛应用的却是MQTT协议.所以,本篇我们将学习Arduino平台上的MQTT实现库 ...

  9. 百万年薪python之路 -- 小数据池和代码块

    1.小数据池和代码块 # 小数据池 -- 缓存机制(驻留机制) # == 判断两边内容是否相等 # a = 10 # b = 10 # print(a == b) # is 是 # a = 10 # ...

  10. Enum枚举学习- java5之前和之后的写法

    enum枚举 JDK1.5以前枚举的写法 /** * java5.0以前,定义枚举类.类中的对象是有限的 * @author amber * */ public class Season { priv ...