The Accomodation of Students

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

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
 
Source

题意: 有n个人,m条关系,问通过这些关系判断,所有人是否可以分成两个组,如果可以則求出一组最多多少人。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std; const int N = ;
int head[N],total,visit[N];
int link[N],color[]; struct nodes
{
int e,next;
} Edge[N]; void add(int x,int y)//加边,邻接表形式建图
{
Edge[total].e = y;
Edge[total].next = head[x];
head[x] = total++;
} int dfs(int f) //匈牙利算法中的二分匹配dfs实现
{
for(int i = head[f]; i != -; i = Edge[i].next)//邻接表遍历与起始点f相连的所有点
{
int s = Edge[i].e; //与起始点相连的点
if(visit[s]) continue;//访问过就跳过
visit[s] = ;
if(link[s] == - || dfs(link[s]))//寻找增广路径
{
link[s] = f ;
return ;
}
}
return ;
} void init()//初始化
{
total = ;
memset(head,-,sizeof(head));
memset(link,-,sizeof(link));
}
bool bcolor(int u,int x)
{
if(color[u] != -)//如果从上个点出发过来,发现该点已染色
{
if(color[u] == x)//如果此点颜色与邻点相同
return false; //不是二分图,返回染色失败
}
else
{
color[u] = x^;//该点未染色,則染与上个点不同的颜色
for(int i = head[u]; i != -; i = Edge[i].next)//遍历与该点相连的所有边
{
if(!bcolor(Edge[i].e,color[u]))//如果下一层染色失败,則返回失败
return false;
}
}
return true;
}
bool judge(int n)
{
memset(color,-,sizeof(color));//先初始化所有点为-1,代表未染色
for(int i = ; i <= n; i++)//依次检查点是否染过色
{
if(color[i] == -)//若没访问过,則开始填色
{
if(!bcolor(i,))
return false;
}
}
return true;
}
int main(void)
{
int n,i,cnt,m;
while(scanf("%d",&n) != -)
{
init();
scanf("%d",&m);
for(i = ; i < m; i++)
{
int x,y;
scanf("%d %d",&x,&y);
add(x,y);
//add(y,x);
}
if(!judge(n)) // 判断是否为二分图
{
printf("No\n");
}
else //匈牙利算法
{
for(cnt = ,i = ; i <= n; i++)
{
memset(visit,,sizeof(visit));
if(dfs(i))
cnt++;
}
printf("%d\n",cnt);//若建的双向边則最大匹配数统计了两次,因为是双向的,所以需除以2 ;若建的单向边,則不需要除以2.
} }
return ;
}

hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)的更多相关文章

  1. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

  2. HDU 2444 The Accomodation of Students 二分图判定+最大匹配

    题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...

  3. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  4. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

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

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

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

  7. HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)

    [题目链接]:pid=2444">click here~~ [题目大意]: 给出N个人和M对关系,表示a和b认识,把N个人分成两组,同组间随意俩人互不认识.若不能分成两组输出No,否则 ...

  8. HDU 2444 The Accomodation of Students(判断二分图+最大匹配)

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

  9. HDU——2444 The Accomodation of Students

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

随机推荐

  1. js中的继承和重载

      js中有三种继承方式:一.通过原型(prototype)实现继承 二.借用构造函数式继承,可分为通过call()方法实现继承和通过apply()方法实现继承 仅仅通过原型继承我们可以发现在实例化子 ...

  2. 短URL系统、301/302重定向

    短 URL 系统是怎么设计的?https://yq.aliyun.com/articles/87600 短网址(short URL)系统的原理及其实现 https://hufangyun.com/20 ...

  3. [转]Windows钩子

    Windows钩子 Windows应用程序的运行模式是基于消息驱动的,任何线程只要注册了窗口类就会有一个消息队列来接收用户的输入消息和系统消息.为了取得特定线程接收或发送的消息,就要 Windows提 ...

  4. C语言作用域、链接属性和存储类型

    C/C++中作用域详解 作用域 编译器可以确认的4种作用域-代码块作用域.文件作用域.函数作用域和原型作用域,一般来说,标识符(包括变量名和函数名)声明的位置决定它的作用域. (1)代码块作用域 一对 ...

  5. 尝试一下LLJ大佬的理论AC大法

    1.BZOJ 3522 Poi2014 Hotel DFS 给定一棵树,求有多少无序三元组(x,y,z)满足x,y,z互不相等且Dis(x,y)=Dis(y,z)=Dis(x,z) 枚举中心点,分别d ...

  6. MyBatis配置文件(五)--objectFactory对象工厂

    我们在使用MyBatis执行查询语句的时候,通常都会有一个返回类型,这个是在mapper文件中给sql增加一个resultType(或resultMap)属性进行控制.resultType和resul ...

  7. MyBatis配置文件(二)--settings配置

    settings是MyBatis中最复杂的配置,它能影响MyBatis底层的运行,大部分情况下使用默认值,只需要修改一些常用的规则即可.常用规则有自动映射.驼峰命名映射.级联规则.是否启动缓存.执行器 ...

  8. 【One by one系列】一步步开始使用Redis吧(一)

    One by one,一步步开始使用Redis吧(一) 最近有需求需要使用redis,之前也是随便用用,从来也没有归纳总结,今天想睡觉,但是又睡不着,外面阳光不错,气温回升了,2019年6月1日,成都 ...

  9. 设置div背景图片填满div

    可以设置div的样式为 background:url('+UPLOAD_PATH+data.url+') no-repeat; background-size: 100%;width:100%;hei ...

  10. 手写Function.bind函数

    if(!Function.prototype.bind){ Function.prototype.bind = function(oThis){ if(typeof this !=="fun ...