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. Vue 全家桶,深入Vue 的世界

    内容简介: Vue 实例上的属性 Vue 生命周期 Vue 数据绑定 computed 计算属性 watch 监听器 Vue 组件 Vue 组件 extend Vue 组件高级属性 Vue 的rend ...

  2. #10056. 「一本通 2.3 练习 5」The XOR-longest Path

    题目描述 有一棵带权树,求这些边组成的路径的最大异或和. Solution 考场 SPFA 神奇 70 分代码 #include<cstdio> #include<cstdlib&g ...

  3. DNS原理及解析过程详解

    相信大家在平时工作中都离不开DNS解析,DNS解析是互联网访问的第一步,无论是使用笔记本浏览器访问网络还是打开手机APP的时候,访问网络资源的第一步必然要经过DNS解析流程.下面我们将详细的给大家讲解 ...

  4. 详解AJAX工作原理以及实例讲解(通俗易懂)

    什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...

  5. 从Go语言编码角度解释实现简易区块链

    区块链技术 人们可以用许多不同的方式解释区块链技术,其中通过加密货币来看区块链一直是主流.大多数人接触区块链技术都是从比特币谈起,但比特币仅仅是众多加密货币的一种. 到底什么是区块链技术? 从金融学相 ...

  6. 百万年薪python之路 -- HTML基础

    一. Web标准 web标准: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web标准规范的分类:结构标准.表现标准.行为标准. 结构:html.表示:c ...

  7. ES6基本语法入门

    一.用let代替var声明变量 ES5中,我们可以在代码中任意位置声明变量,甚至可以重写已经声明的变量,ES6引入了一个let关键字,它是新的var. let language = 'javascri ...

  8. jquery链式原理.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. mysql全局变量和局部变量

    全局变量和局部变量 在服务器启动时,会将每个全局变量初始化为其默认值(可以通过命令行或选项文件中指定的选项更改这些默认值).然后服务器还为每个连接的客户端维护一组会话变量,客户端的会话变量在连接时使用 ...

  10. 阿里六面,挂在hrg,我真的不甘心!

    前言最近跟一位朋友聊天,福报场-阿里是大部分程序员的梦想.这位老哥梦想进入阿里很久了,连续两年面试阿里不同bu,有几次是技术面挂,最冤的一次是技术6面了,连P10大老板都面了,但是挂在了hrg. 我以 ...