The Accomodation of Students

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

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
 

边数是个坑点,加到2W才过,WA好几次,题目本身比较水,就是拿来熟悉一下最大匹配的hungary算法,做了几道题发现这个算法主要思想就是不停地尝试匹配,比如A匹配B,如果B没被匹配过那就最好,匹配数+1;若B被匹配过,那就尝试看看匹配B的那个人(匹配B的此时肯定不是A)把B换掉,比如匹配B的是C,而C又可以匹配D,因此把C匹配的对象变成D,那B就空出来了,就可以被A匹配了。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
const int M=20010;
struct info
{
int to;
int pre;
}E[M];
int head[M],cnt,n,m;
int vis[N],match[N],color[N];
queue<int>Q;
void add(int s,int t)
{
E[cnt].to=t;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void init()
{
MM(head,-1);
MM(match,-1);
MM(color,0);
cnt=0;
while (!Q.empty())
Q.pop();
}
bool canmatch(int s)
{
int i;
color[s]=1;
Q.push(s);
while (!Q.empty())
{
int now=Q.front();
Q.pop();
for (i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!color[v])
{
color[v]=(color[now]==1?2:1);
Q.push(v);
}
else if(color[v]==color[now])
return false;
}
}
return true;
}
bool dfs(int now)
{
int i;
for (i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
if(!vis[v])
{
vis[v]=1;
if(match[v]==-1||dfs(match[v]))
{
match[v]=now;
match[now]=v;
return true;
}
}
}
return false;
}
int hung()
{
int r=0,i;
for (i=1; i<=n; i++)
{
MM(vis,0);
if(dfs(i))
r++;
}
return r;
}
int main(void)
{
int a,b,i;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
!canmatch(1)?puts("No"):printf("%d\n",hung());
}
return 0;
}

HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)的更多相关文章

  1. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  2. hdu2444(判二分图+最大匹配)

    传送门:The Accomodation of Students 题意:有n个学生,m对相互认识的,问能否分成两队,使得每对中没有相互认识的,如果可以求最大匹配,否则输出No. 分析:判断二分图用染色 ...

  3. HDU 2063 过山车(模板—— 二分图最大匹配问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2063  解题思路: 二分图最大匹配模板题. AC代码: #include<stdio.h> ...

  4. 【HDU 2063】过山车(二分图最大匹配模板题)

    题面 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每 ...

  5. 51nod 2006 飞行员配对(二分图最大匹配) 裸匈牙利算法 求二分图最大匹配题

    题目: 题目已经说了是最大二分匹配题, 查了一下最大二分匹配题有两种解法, 匈牙利算法和网络流. 看了一下觉得匈牙利算法更好理解, 然后我照着小红书模板打了一遍就过了. 匈牙利算法:先试着把没用过的左 ...

  6. 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] #include<iostream> #include<cstdio&g ...

  7. HDU 1068 Girls and Boys(模板——二分图最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 Problem Description the second year of the univ ...

  8. hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)

    //推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图.否则是二分图. //推断回路奇偶性:把相邻两点染成黑白两色.假设相邻两点出现颜色同样则存在奇数回路. 也就是非二分图. # incl ...

  9. 过山车 HDU 2063 (二分图匹配裸题)

    Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生 ...

随机推荐

  1. 设置office首字母不变大小的手段

    选项->校对—〉自动更正选项->“自动更正”页,句首字母大写,取消就行了

  2. vijos 1034 家族(水题日常)

    描述 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 规定:x和y是亲戚,y和z是亲戚,那么x和z也是亲戚.如果x,y是亲戚 ...

  3. 如何在ABAP里用函数式编程思想打印出非波拉契Fibonacci(数列)

    在JavaScript里可以用ES6提供的FunctionGenerator这种黑科技来打印非波拉契数列,具体细节参考我这篇文章. 在ABAP里也有很多种方式实现这个需求. 下面这个report分别用 ...

  4. Android(java)学习笔记146:网页源码查看器(Handler消息机制)

    1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...

  5. MIPS—冒泡排序

    SORT 使用冒泡排序对整数数组进行排序,这种排序虽然不是最快的,但却是最简单的. C语言代码 #include<stdio.h> #include<iostream> usi ...

  6. urllib基础-利用网站结构爬取网页-百度搜索

    有的时候爬取网页,可以利用网站额结构特点爬取网页 在百度搜索框中输入搜索内容,单击搜索,浏览器会发送一个带有参数的url请求.尝试删除其中的一些参数,只剩下wd这个参数.发现wd是搜索内容.这样程序可 ...

  7. python常用模块之json和pickle模块

    json模块 json.dumps     将 Python 对象编码成 JSON 字符串 json.loads       用于解码 JSON 数据.该函数返回 Python 字段的数据类型. pi ...

  8. python之list [ 列表 ]

    1. 列表是什么? list [ ] 逗号隔开 是一个容器 可以存放任意类型 列表 == 书包 书包里可以放水杯.衣服.袜子.钱包 钱包里可以放钱.身份证件,可以包套包 2. 列表能干什么? 存储大量 ...

  9. 实验十一 团队作业7:团队项目设计完善&编码

    实验十一 团队作业7:团队项目设计完善&编码 实验时间 2019-6-6 Deadline: 2019-6-12 10:00,以团队随笔博文提交至班级博客的时间为准. 评分标准: 按时交 – ...

  10. 什么是Java内存模型中的happens-before

    Java内存模型JMM Java内存模型(即Java Memory Model , 简称JMM),本身是一种抽象的概念,并不真实存在,它描述的是一组规则或规范,通过这组规范定义了程序个各个变量(包括实 ...