题目链接: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. session和request的区别

    request 请求对象 请求中保存请求过程中需要的参数 比如另一个页面需要使用的对象编号,list,map等,请求结束,就失效了 session 会话对象 除非关闭会话(到时间通常为30分钟,或者关 ...

  2. mac ssh会话复制

    第一次登录服务器,需要输入账户.密码, 如果你需要重新开启另一个终端窗口,再次登录此服务器不需要输入账户.密码(类似免登).你只要让本地保存你的SSH会话即可. Mac 保持会话,需要在修改 ssh配 ...

  3. eclipse项目导入到Android Studio Plugin with id 'android-library' not found

    在主项目的build.gradle 中加入以下代码buildscript { repositories { mavenCentral() } dependencies { classpath 'com ...

  4. 46 关于Linux的I/O重定向

    I/O重定向是一个过程,这个过程捕捉一个文件.或命令.或程序.或脚本.甚至代码块(code block)的输出,然后把捕捉到的输出,作为输入发送给另外一个文件.或命令.或程序.或脚本. 1.I/O重定 ...

  5. java 十六进制数的转换

    今天晚上做了一道java基础题,题目看起来简单,但是实现起来却花了我近两个小时的时间,认真的做这道题,你会发现它特别考你的基本功.有兴趣的可以试一下哦. 题目: 请用此语言编写如下函数,采用自己的算法 ...

  6. android报错及解决2--Sdcard进行文件的读写操作报的异常

    报错描述: 对Sdcard进行文件的读写操作的时候,报java.io.FileNotFoundException: /sdcard/testsd.txt (Permission denied),在往S ...

  7. PAT 1005

    1005. Spell It Right (20) Given a non-negative integer N, your task is to compute the sum of all the ...

  8. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  9. 《编写高质量代码—改善java程序的151个建议》知识整理一

    1.用偶判断,不用奇判断 案例:在代码i%2==1?"奇数":"偶数" (java中的求余算法相当于:divided-divided/divisor*divis ...

  10. ios快捷键

    分屏:cmd + option + return 退出分屏:cmd + return cmd + option + [ 代码上跳 cmd + [ 代码左移