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. Redis 3.0中文版学习(二)

    网址:http://wiki.jikexueyuan.com/project/redis-guide/entry-to-master-middle.html 1.Redis的列表: 采用链表的实现方法 ...

  2. CentOS6.5下搭建文件共享服务(Samba)

    Samba服务: 本内容为samba服务学习者提供参考 案例描述: 某公司的管理员需要搭建SAMBA服务器,IP地址及允许的访问网段自定义.SAMBA服务器的安全级别为user级,所在工作组为WORK ...

  3. jenkins中使用变量

    查看jenkins内置变量: 1.新建一个job: 2.构建-增加构建步骤-执行shell: 3.点击  可用的环境变量列表 即可查看 如WORKSPACE : 作为工作空间分配给构建目录的绝对路径 ...

  4. myeclipse 在web-inf/lib中导入包

    今天用myeclipse的时候发现无法在web-inf/lib导入包,如果直接在工程上导入,则进入了一个referenced libraries的文件夹里,而web-inf/lib里面是没有jar包的 ...

  5. redis集群之Codis

    在大数据高并发场景下,单个 Redis 实例往往会显得捉襟见肘.首先体现在内存上,单个 Redis 的内存不宜过大,内存太大会导致 rdb 文件过大,进一步导致主从同步时全量同步时间过长,在实例重启恢 ...

  6. ThreadLocal小试牛刀

    ThreadLocal中保存的数据只能被当前线程私有,不被其它线程可见 证明 声明一个全局的变量threadLocal,初始值为1,通过3个线程对其进行访问修改设置,理论上threadLocal的最终 ...

  7. Redis(十五)Redis 的一些常用技术(Spring 环境下)

    一.Redis 事务与锁机制 1.Redis的基础事务 在Redis中开启事务的命令是 multi 命令, 而执行事务的命令是 exec 命令.multi 到 exec 命令之间的 Redis 命令将 ...

  8. JVM(7) Java内存模型与线程

    衡量一个服务性能的高低好坏,每秒事务处理数(Transactions Per Second,TPS)是最重要的指标之一,它代表着一秒内服务端平均能响应的请求总数,而 TPS 值与程序的并发能力又有非常 ...

  9. java中的Static、final、Static final各种用法详解

    前言 对Static.final.Static final这几个关键词熟悉又陌生?想说却又不知怎么准确说出口?好的,本篇博客文章将简短概要出他们之间的各自的使用,希望各位要是被你的面试官问到了,也能从 ...

  10. CTR@DeepFM

    1. DeepFM算法 结合FM算法和DNN算法,同时提取低阶特征和高阶特征,然后组合.FM算法负责对一阶特征及由一阶特征两两组合成的二阶特征进行特征提取:DNN算法负责对由输入的一阶特征进行全连接等 ...