hdu 2444(二分图) The Accomodation of Students
http://acm.hdu.edu.cn/showproblem.php?pid=2444
大意是给定n个学生,他们之间可能互相认识,首先判断能不能将这些学生分为两组,使组内学生不认识;
现想将学生两两分组,且保证每一组的学生都认识,这样分组可达到的最大组数为多大?
判断二分图,然后求匈牙利算法求最大匹配数
染色法判断二分图(脑抽用vector跑的特别慢)
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define M 300
vector<int>line[M];
int judge[M],p[M],q[M],used[M];
int n;
int find()
{
memset(q,,sizeof(q));
int start=,end=;
q[start]=;
memset(judge,-,sizeof(judge));
judge[]=;
while (start<end)
{
int w=q[start];
for (int i=;i<line[w].size();i++)
{
int e=line[w][i];
if (judge[e]==-){
judge[e]=(judge[w]+)%;
q[end++]=e;
}
else{
if (judge[e]==judge[w]) return ;
}
}
start++;
}
return ;
}
int sreach(int x)
{
int i,j;
for (j=;j<=n;j++){
for (i=;i<line[j].size();i++)
{
if (line[j][i]==x&&!used[j])
{
used[j]=;
if (!p[j]||sreach(p[j]))
{
p[j]=x;
return ;
}
}
}
}
return ;
}
int main()
{
int t,i,m,a,b;
while (~scanf("%d %d",&n,&m))
{
for (i=;i<=n;i++)
line[i].clear();
while (m--)
{
scanf("%d %d",&a,&b);
line[a].push_back(b);
line[b].push_back(a);
}
memset(p,,sizeof(p));
if (!find()){
printf("No\n");continue;
}
int ans=;
for (i=;i<=n;i++)
{
memset(used,,sizeof(used));
if (sreach(i)) ans++;
}
printf("%d\n",ans/);
}
return ;
}
关系并查集判断二分图,类似于hdu 1829
#include<cstdio>
#include<cstring>
using namespace std;
#define M 300
int father[M],line[M][M],used[M],p[M];
int n,rank[M];
void give()
{
for (int i=;i<=;i++){
father[i]=i;rank[i]=;
}
}
int find(int x)
{
if (x==father[x]) return father[x];
int t=find(father[x]);
rank[x]=(rank[x]+rank[father[x]])%;
father[x]=t;
return father[x];
}
int sreach(int x,int n)
{
int i;
for (i=;i<=n;i++)
{
if (line[i][x]&&!used[i])
{
used[i]=;
if (!p[i]||sreach(p[i],n))
{
p[i]=x;
return ;
}
}
}
return ;
}
int main()
{
int n,m,a,b,i;
while (~scanf("%d %d",&n,&m))
{
give();
int flag=;
memset(p,,sizeof(p));
memset(line,,sizeof(line));
while (m--)
{
scanf("%d %d",&a,&b);
line[a][b]=line[b][a]=;
int sx=find(a);
int sy=find(b);
if (sx!=sy) {
rank[sx]=(rank[a]+rank[b]+)%;
father[sx]=sy;
}
else {
if (rank[a]==rank[b]) flag=;
}
}
if (flag==){
printf("No\n");continue;
}
int ans=;
for (i=;i<=n;i++)
{
memset(used,,sizeof(used));
if (sreach(i,n)) ans++;
}
printf("%d\n",ans/);
}
return ;
}
hdu 2444(二分图) The Accomodation of Students的更多相关文章
- HDU 2444:The Accomodation of Students(二分图判定+匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:给出边,判断这个是否是一个二分图,并求最大匹配. 思路:先染色法求出是否是一个二分图,然后再匈牙利求 ...
- The Accomodation of Students HDU - 2444 二分图判定 + 二分图最大匹配 即二分图-安排房间
/*655.二分图-安排房间 (10分)C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容: 有一群学生,他们之间有的认识有的不认识.现在要求把学生分成2组,其中同一个组的人相互不认 ...
- hdu 2444 二分图判断与最大匹配
题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...
- HDU 2444 二分图判断 (BFS染色)+【匈牙利】
<题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...
- HDU - 2444 二分图最大匹配 之 判断二分图+匈牙利算法
题意:第一行给出数字n个学生,m条关系,关系表示a与b认识,判断给定数据是否可以构成二分图,如果可以,要两个互相认识的人住一个房间,问最大匹配数(也就是房间需要的最小数量) 思路:要看是否可以构成二分 ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
题目来源:HDU 2444 The Accomodation of Students 题意:n个人能否够分成2组 每组的人不能相互认识 就是二分图判定 能够分成2组 每组选一个2个人认识能够去一个双人 ...
- The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- Canvas中 drawImage绘制图片不显示
在学习 html5中的 Canvas.drawImage时写了如下代码: <!doctype html> <html> <head><title>研究& ...
- selenium 浏览器常用设置和部署
一,chrome浏览器设置 from selenium import webdriver # 浏览器选项 chrome_options = webdriver.ChromeOptions() # 使用 ...
- cv2对图像进行旋转和放缩变换
旋转: def get_image_rotation(image): #通用写法,即使传入的是三通道图片依然不会出错 height, width = image.shape[:2] center = ...
- ADO.Net 综合练习题
题目: 第一部分: 新建一个数据库:ADO测试,包含下面两个数据表,使用代码创建,并保留创建的代码文本. 专业表Subject: 专业编号(SubjectCode):nvarchar类型,不能为空,主 ...
- CSS样式表的写作规范
推荐大家使用的CSS书写规范.顺序 写了这么久的CSS,但自己都没有按照良好的CSS书写规范来写CSS代码,东写一段西写一段,命名也是想到什么写什么,过一段时间自己都不知道写的是那一块内容, 这样会影 ...
- ScrollReveal.js 用于创建和管理元素进入可视区域时的动画效果,帮助你的网站增加吸引力。
ScrollReveal.js 用于创建和管理元素进入可视区域时的动画效果,帮助你的网站增加吸引力. 1.http://www.yangqq.com/jstt/css3/2017-08-08/787. ...
- Failed to acquire connection "SAP_PRD_NEW.SAPSR3". Connection may not be configured correctly or you may not have the right permissions
SQLSERVER JOB无法执行 错误提示: Message Executed as user: WORKGROUP\NSDZHSCMFP01$. Microsoft (R) SQL Server ...
- MyBufferedReader
/** 需求:自定义一个包含 readLine 方法的 BufferedReader 来模拟一下 BufferedReader */ import java.io.FileReader; import ...
- no module named cv2
运行python脚本时报错: ImportError: No module named cv2 第一想法: 使用命令: pip install cv2 会报错找不到请求的版本 解决方法: 使用命令 p ...
- CentOS 安装Oracle
转自----------------https://www.cnblogs.com/startnow/p/7580865.html 环境:VM12+centos7 x86_64 minimal - 最 ...