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. css 文字间距

    letter-spacing :  字与字之间的距离 text-indent : 行的抬头间距 line-height : 行高度

  2. moloch1.8.0简单操作手册

    moloch1.8.0简单操作手册 Sessions 页面:Sessions主要通过非常简单的查询语言来构建表达式追溯数据流量,以便分析. SPIView 页面: SPIGraph页面:SPIGrap ...

  3. 02--Java Jshell的使用 最适合入门的Java教程

    JShell JShell目标 Java Shell 工具(简称:JShell)是一个用于学习Java编程语言和构建Java代码原型的交互式工具.JShell是一个Read-Evaluate-Prin ...

  4. Python 的多线程是鸡肋?

    "唉,还没毕业就受到甲方的支配,等以后进了公司可咋整啊."小白嘴里这么吐槽,但心理上还是不敢怠慢,只能恋恋不舍地关掉眼前的游戏,打开了 Python 代码思考了起来. " ...

  5. Java基础(41)AbstractList类

    AbstractList类的子类有AbstractSequentialList(其子类是LinkedList)和ArrayList 1.LinkedList 定义 public class Linke ...

  6. Java网络编程(一)Socket套接字

    一.基础知识 1.TCP:传输控制协议. 2.UDP:用户数据报协议. 二.IP地址封装 1.InetAddress类的常用方法 getLocalHost() 返回本地主机的InetAddress对象 ...

  7. Java基础(二)数据类型

    数据类型主要分为基本类型和引用类型两大类. 一.基本类型 1.基本类型又分为数值类型和boolean类型, (1)数值类型包括浮点数类型.整数类型和字符类型 整型                    ...

  8. leetcode算法小题(3)

    问题描述: 判断一个数是否为回文数 class Solution {      public boolean isPalindrome(int x) {           if(x<0)    ...

  9. java中的时区转换

    目录 java中的时区转换 一.时区的说明 二.时间的表示 三.时间戳 四.Date类和时间戳 五.java中的时区转换 java中的时区转换 一.时区的说明 地球表面按经线从东到西,被划成一个个区域 ...

  10. 【IntelliJ IDEA】 常用快捷键列表

    1.常用Shortcut F2 或Shift+F2 高亮错误或警告快速定位 Ctrl+Up/Down 光标跳转到第一行或最后一行下 Ctrl+B 快速打开光标处的类或方法  CTRL+ALT+B  找 ...