题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444

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(<n<=), 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 Sample Output
No

题意:有n个学生,a与b认识,b与c认识,但a与c不认识,问把学生分到两个房间,使每个房间的人都不认识,一个房间最多多少人?

方法:先判断是否能成二分图 不能输出No,能求最大匹配数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
#define N 609
#define ll long long
#define INF 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
int Map[N][N],vis[N],used[N],cl[N];
int n;
int dfs()
{
int cot;
met(cl,-);
queue<int> Q;
Q.push();
cl[]=;
while(Q.size())
{
cot=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(Map[cot][i])
{
if(cl[i]==-)
{
cl[i]=-cl[cot];
Q.push(i); }
else if(cl[i]==cl[cot])
return ;
}
}
}
return ;
}
int han(int u)
{
for(int i=;i<=n;i++)
{
if(!vis[i] && Map[u][i])
{
vis[i]=;
if(!used[i] || han(used[i]))
{
used[i]=u;
return ;
}
}
}
return ;
}
int main()
{
int t,m,a,b;
while(scanf("%d %d",&n,&m)!=EOF)
{
met(Map,); for(int i=;i<m;i++)
{
scanf("%d %d",&a,&b);
Map[a][b]=Map[b][a]=;
}
if(dfs()==)///判断是否能成二分图
{
printf("No\n");
continue;
}
else
{
int sum=;met(used,);
for(int i=;i<=n;i++)
{
met(vis,);
sum+=han(i);
} printf("%d\n",sum/);
}
}
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 (判断二分图,最大匹配)

    The Accomodation of StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  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. PowerDesigner 面向对象模型(OOM)

    PowerDesigner 面向对象模型(OOM)说明 nulljavasystemstringpowerbuilderclass   目录(?)[+]   一.     OOM 简介 Object- ...

  2. c++中类长度解析

    通常我们定义一个类,它所占的空间有多大呢? 首先我们看一下下面的这个类 class A{ public: void func1(void){ printf("11111heihei\n&qu ...

  3. Linux 下 expect 脚本语言中交互处理常用命令

    Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...

  4. python(6)

    python(6) 6.1 面向对象编程:OOP相信学过编程的就会了解它有多重要了.当然c什么的就算了 实际上面向对象就是把对象拥有的数据和方法封装在对象里面,由对象调用.    面向对象最重要的是类 ...

  5. 域名的MX设置及校验方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. Lotus分析

    一 Lotus的任务 Lotus是一个消息通知服务,topic和subscription是多对多的关系.后面我加了一个发送自定义邮件和自定义短信的功能. 产品里面有个监控报警和通知列表.监控报警里创建 ...

  7. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  8. 封装的分页jq

    (function ($) { $.fn.page = function (options) { var defaults = { divid: "pagediv", count: ...

  9. php代理请求

    $url = 'http://192.168.5.241:8893/index.php?a=SendMessage&m=taskSend'; $ci = curl_init ();/* Cur ...

  10. View绘制详解,从LayoutInflater谈起

    自定义View算是Android开发中的重中之重了,很多小伙伴可能或多或少都玩过自定义View,对View的绘制流程也有一定的理解.那么现在我想通过几篇博客来详细介绍View的绘制流程,以便使我们更加 ...