题目链接: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. Oracle中的AS和IS

    Oracle中的AS和IS是ORACLE为了方便而设置的同义词基本上没有不同 . 使用规则: 1.在创建存储过程(PROCEDURE)/函数(FUNCTION),以及自定义类型(TPYE)和包(PAC ...

  2. SQLite使用教程10 运算符

    SQLite 运算符 SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算. 运算符用于指定 SQLite 语句中的条件 ...

  3. svn 如何解决冲突

    项目中,往往不止你一人开发,多人开发,难免会有代码的冲突.彼此间谁也不能保证不会修改同个文件.如果修改了同个方法的内容.这时提交到svn是会提示代码冲突的. 当然,冲突是可控的,但不能避免.每次写代码 ...

  4. 最牛X的编码套路

    最近,我大量阅读了Steve Yegge的文章.其中有一篇叫"Practicing Programming"(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反 ...

  5. Android开发之有效获取状态栏(StatusBar)高度

    获取状态栏高度 一.传统方式:有时获取为0,解决方法看  二 1 2 3 4 代码 Rect frame = new Rect(); getWindow().getDecorView().getWin ...

  6. 转载:iPhone 6 Plus 屏幕宽度问题 375 vs 414

    首先看一张比较简单明了的 iPhone 6 与 iPhone 6 Plus 对比图,来自 PaintCode 的<The Ultimate Guide To iPhone Resolutions ...

  7. LINUX怎么远程连接ORACLE数据库

    A电脑装了ORACLE客户端,B是ORACLE服务器,都在LINUX环境下,请问A电脑怎么访问B上的数据库?该怎么改配置?求高手解答,越详细越好 很简单,你在A电脑安装一个ORACLE 客户端,然后, ...

  8. java_利用session校验图片认证码

    RegisterServlet:检验server,client验证码是否一致 ImageServlet: 产生验证码 <!DOCTYPE html> <html> <he ...

  9. Android(java)学习笔记121:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解

    先看看网路上的说法: android.intent.action.MAIN决定应用程序最先启动的 Activity android.intent.category.LAUNCHER决定应用程序是否显示 ...

  10. Unix 网络编程(2)——TCP API

    TCP C/S套接口函数一般调用过程及基本函数 如上图所示的TCP连接的基本过程.一般来说,服务器先于客户端运行,服务器程序运行的基本过程是: socket()函数创建服务器段socket. bind ...