hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)
//推断是否为二分图:在无向图G中,假设存在奇数回路,则不是二分图。否则是二分图。
//推断回路奇偶性:把相邻两点染成黑白两色。假设相邻两点出现颜色同样则存在奇数回路。 也就是非二分图。
# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
int vis[210],map[210][210],cott[210];
int c[210];
int flag,n,m;
void dfs(int i,int color)//染色法推断是否是二分图
{
for(int j=1; j<=n; j++)
{
if(map[i][j])
{
if(c[j]==0)
{
c[j]=-color;
dfs(j,-color);
}
else if(c[j]==color)
{
flag=false;
return ;
}
if(!flag)
return ;
}
}
}
int check()
{
flag=1;
memset(c,0,sizeof(c));
c[1]=1;//一号为黑色,与他相邻的都染为白色
dfs(1,1);//从一号開始
return flag;
} int bfs(int x)
{
for(int i=1; i<=n; i++)
{
if(!vis[i]&&map[x][i])
{
vis[i]=1;
if(!cott[i]|bfs(cott[i]))
{
cott[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int a,b;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
for(int i=0; i<m; i++)
{
scanf("%d%d",&a,&b);
map[b][a]=map[a][b]=1;
}
if(!check())
{
printf("No\n");
}
else
{
int cot=0;
memset(cott,0,sizeof(cott));
for(int i=1; i<=n; i++)//以x集合为准找了一遍。又以y集合为准找了一遍。匹配数量增倍
{
memset(vis,0,sizeof(vis));
if(bfs(i))
cot++;
}
printf("%d\n",cot/2); } }
return 0;
}
hdu2444 The Accomodation of Students(推断二分匹配+最大匹配)的更多相关文章
- HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...
- HDU2444 The Accomodation of Students
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 StudentsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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 ...
- HDU2444 The Accomodation of Students【匈牙利算法】
题意: 有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出N ...
- HDU2444 The Accomodation of Students(二分图最大匹配)
有n个关系,他们之间某些人相互认识.这样的人有m对.你需要把人分成2组,使得每组人内部之间是相互不认识的.如果可以,就可以安排他们住宿了.安排住宿时,住在一个房间的两个人应该相互认识.最多的能有多少个 ...
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- B - The Accomodation of Students - hdu 2444(最大匹配)
题意:现在有一些学生给你一下朋友关系(不遵守朋友的朋友也是朋友),先确认能不能把这些人分成两组(组内的人要相互不认识),不能分的话输出No(小写的‘o’ - -,写成了大写的WA一次),能分的话,在求 ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
随机推荐
- Entity Framework(实体框架 EF)
什么是Entity Framework呢(下面简称EF)? EF(实体框架)是ADO.NET中的一组支持开发面向数据的软件应用程序的技术,是微软的一个ORM框架.ORM(对象关系映射框架):指的是面向 ...
- [HAOI2012]外星人
题目大意: 告诉你一个数n,求满足φ^x(n)=1的x. 思路: 首先我们可以发现满足φ(n)=1的数只有2,也就是说你得到最终的结果,最后一步肯定是φ(2). 同时,可以发现φ(φ(2^k))=φ( ...
- ElasticSearch学习笔记--1、安装以及运行
Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎,多的我就不细说了. 相关实验环境 Centos:7.3 ElasticSearch:5.6 java:1.8 1. ...
- 【8.30校内测试】【找规律模拟】【DP】【二分+贪心】
对于和规律或者数学有关的题真的束手无策啊QAQ 首先发现两个性质: 1.不管中间怎么碰撞,所有蚂蚁的相对位置不会改变,即后面的蚂蚁不会超过前面的蚂蚁或者落后更后面的蚂蚁. 2.因为所有蚂蚁速度一样,不 ...
- Codeforces Round #302 (Div. 2) B. Sea and Islands 构造
B. Sea and Islands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/p ...
- Tasker文件夹说明
################导入类#################路径 类型 后缀名 /tasker/profiles/ 配置 .prf.xml /tasker/projects/ 项目 .pr ...
- eclipse的项目和配置文件 .project .cproject .classpath .metadata
eclipse CDT建立project后在project name对应的目录下面会生成.project和.cproject两个隐藏文件. eclipse java建立project后在projec ...
- Using TXMLDocument, Working with XML Nodes
Using TXMLDocument The starting point for working with an XML document is the Xml.XMLDoc.TXMLDocumen ...
- AX5511 Boost Converter
GENERAL DESCRIPTION The AX5511 is a current mode step up converter intended for small, low powera ...
- Supported_Hardware#4G_LTE_cards_and_modems
https://wiki.mikrotik.com/wiki/Supported_Hardware#4G_LTE_cards_and_modems