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. python编程系列---tcp服务端的简单实现

    流程如下: """tcp服务端创建流程1. 创建服务端的tcp socket : server_socket 用于监听客户端的请求2. 绑定端口3. server_soc ...

  2. Markdown的基本使用指南

    目录 1.标题 2.列表 2.1无序列表 2.1有序列表 3.引用 4.图片和链接 5.粗体和斜体 6.分割线 7.代码框和代码块 8.列表 9.可选框 10.添加目录 Markdown 是一种用来写 ...

  3. MySQL操作(一)用户及权限

    一.mysql 里的所有用户都是存储在数据库mysql的user表里 二.创建普通用户.赋权.撤销权限 的操作 1.创建用户(需要先用root进去mysql)格式:create  user  '用户名 ...

  4. Newman基本使用

    简介 Newman 是 Postman 推出的一个 nodejs 库,直接来说就是 Postman 的json文件可以在命令行执行的插件. Newman 可以方便地运行和测试集合,并用之构造接口自动化 ...

  5. FastReport快速实现条形码,二维码面单打印

    一.什么是FastReport? FastReport是功能齐全的报表控件,使开发者可以快速并高效地为·NET/VCL/COM/ActiveX应用程序添加报表支持. FastReport有很多产品,如 ...

  6. django-模板之URL标签(五)

    book/views.py from django.shortcuts import render def index(request): return render(request,"in ...

  7. anaconda重装jupyter notebook后启动jupyter报错的问题

    问题描述: 由于jupyter出现难以解决的问题,采用重新安装来解决问题,但是重装之后启动jupyter报错ImportError: libsodium.so.23: cannot open shar ...

  8. Java基础系列1:Java面向对象

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 概述: Java是面 ...

  9. 第一篇 Flask初识

    一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...

  10. 认识JVM的内存分配

    当我们在JVM中运行一段程序代码,JVM初始运行的时候都会分配好Method Area(方法区)和Heap(堆),而JVM每遇到一个线程,就为其分配一个Program Counter Register ...