The Accomodation of Students

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3775    Accepted Submission(s): 1771

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
 
/* ***********************************************
Author :pk28
Created Time :2015/10/6 19:53:21
File Name :hdu2444.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 40000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; bool cmp(int a,int b){
return a>b;
}
int n;
int m,l,tot;
int pre[],match[];
int vis[maxn]; struct node{
int v,next;
}edge[maxn];
void init(){
l=;
memset(pre,-,sizeof pre);
memset(match,-,sizeof match);
}
void add(int u,int v){
edge[l].v=v;
edge[l].next=pre[u];
pre[u]=l++;
}
int dfs(int u){
for(int i=pre[u];i+;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){
vis[v]=;
if(match[v]==-||dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary(){
tot=;
for(int i=;i<=n;i++){
cle(vis);
if(dfs(i))tot++;
}
return tot;
}
int color[maxn];
bool bs(int u){
for(int i=pre[u];i+;i=edge[i].next){
int v=edge[i].v;
if(color[v]==color[u])return false;
if(!color[v]){
color[v]=-color[u];
if(!bs(v))return false;
}
}
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int a,b;
while(cin>>n>>m){
init(); for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
cle(color);
color[]=;
if(!bs())puts("No");
else printf("%d\n",hungary()/);
}
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 StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  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. RSA工作原理

    摘自:http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html 一.基础数论 1.互质关系 如果两个正整数,除了1以外,没有 ...

  2. bzoj1225 [HNOI2001] 求正整数

    1225: [HNOI2001] 求正整数 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 762  Solved: 313[Submit][Statu ...

  3. bzoj3196 二逼平衡树 树套树(线段树套Treap)

    Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4697  Solved: 1798[Submit][Status][D ...

  4. 远程管理 KVM 虚机

    上一节我们通过 virt-manager 在本地主机上创建并管理 KVM 虚机.其实 virt-manager 也可以管理其他宿主机上的虚机.只需要简单的将宿主机添加进来 填入宿主机的相关信息,确定即 ...

  5. Codeforces961F. k-substrings

    $n \leq 1000000$的字符串,对每一个子串$i$~$n-i+1$,求他最长的一个既是前缀又是后缀的子串. 这题要求的东西具有“对称性”,不充分利用难以解决.这里的“对称性”不仅指询问是对称 ...

  6. 转载 gdb调试程序

    转载自csdn,作者haoel,链接http://blog.csdn.net/haoel/article/details/2879 用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一 ...

  7. golang-random随机数

    在Golang中,有两个包提供了rand,分别为 "math/rand" 和 "crypto/rand",  对应两种应用场景. 一."math/ra ...

  8. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...

  9. JS--截取字符串常用方法详细

    使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=the ...

  10. Go -- 判断chan channel是否关闭的方法

    如果不判断chan是否关闭 Notice: 以下代码会产生死循环 代码如下: package main import ( "fmt" ) func main() { c := ma ...