http://acm.hdu.edu.cn/showproblem.php?pid=2444

【DFS染色】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm> using namespace std;
const int maxn=2e2+;
const int maxm=maxn*maxn;
struct edge
{
int to;
int nxt;
}e[maxm];
int tot;
int head[maxn];
int col[maxn];
bool vis[maxn];
int link[maxn];
void init()
{
tot=;
memset(head,-,sizeof(head));
memset(col,,sizeof(col));
memset(link,-,sizeof(link));
} void add(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
bool Color(int u)
{
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(!col[v])
{
col[v]=!col[u];
if(!Color(v))
{
return false;
}
}
else if(col[v]==col[u])
{
return false;
}
}
return true;
}
bool find(int u)
{
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(!vis[v])
{
vis[v]=true;
if(link[v]==-||find(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
col[]=;
if(!Color())
{
puts("No");
continue;
}
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
{
ans++;
}
}
printf("%d\n",ans/);
}
return ;
}

【BFS染色】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=2e2+;
const int maxm=maxn*maxn;
struct edge
{
int to;
int nxt;
}e[maxm];
int tot;
int head[maxn];
int col[maxn];
bool vis[maxn];
int link[maxn];
void init()
{
tot=;
memset(head,-,sizeof(head));
memset(col,,sizeof(col));
memset(link,-,sizeof(link));
} void add(int u,int v)
{
e[tot].to=v;
e[tot].nxt=head[u];
head[u]=tot++;
}
bool Color(int u)
{
queue<int> Q;
Q.push(u);
while(!Q.empty())
{
u=Q.front();
Q.pop();
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(!col[v])
{
col[v]=!col[u];
Q.push(v);
}
else if(col[v]==col[u])
{
return false;
}
}
}
return true;
}
bool find(int u)
{
for(int i=head[u];i!=-;i=e[i].nxt)
{
int v=e[i].to;
if(!vis[v])
{
vis[v]=true;
if(link[v]==-||find(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
col[]=;
if(!Color())
{
puts("No");
continue;
}
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
{
ans++;
}
}
printf("%d\n",ans/);
}
return ;
}

匈牙利算法:

http://www.cnblogs.com/itcsl/p/6741020.html

【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students的更多相关文章

  1. 匈牙利算法求最大匹配(HDU-4185 Oil Skimming)

    如下图:要求最多可以凑成多少对对象 大佬博客: https://blog.csdn.net/cillyb/article/details/55511666 https://blog.csdn.net/ ...

  2. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

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

  3. (二分匹配“匈牙利算法”)无题II --HDU --2236

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2236 代码: #include<cstdio> #include<cstring> ...

  4. HDU-3729 二分匹配 匈牙利算法

    题目大意:学生给出其成绩区间,但可能出现矛盾情况,找出合理组合使没有说谎的人尽可能多,并按maximum lexicographic规则输出组合. //用学生去和成绩匹配,成绩区间就是学生可以匹配的成 ...

  5. hdu 2063 (二分匹配 匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. Wrestling Match---hdu5971(2016CCPC大连 染色法判断是否是二分图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5971 题意:有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个 ...

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

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

  9. 染色法判断是否是二分图 hdu2444

    用染色法判断二分图是这样进行的,随便选择一个点, 1.把它染成黑色,然后将它相邻的点染成白色,然后入队列 2.出队列,与这个点相邻的点染成相反的颜色 根据二分图的特性,相同集合内的点颜色是相同的,即 ...

随机推荐

  1. [转]为革命保护视力 --- 给 Visual Studio 换颜色

    本文转自:http://www.cnblogs.com/stg609/p/3723968.html “为革命,保护视力,预防近视,眼保健操开始......” 这个应该是最老版本的眼保健操了,你听过? ...

  2. WinForm 对话框,流

    private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...

  3. 8.JAVA-向上转型、向下转型

    父子对象之间的转换分为了向上转型和向下转型,它们区别如下: 向上转型 : 通过子类对象(小范围)实例化父类对象(大范围),这种属于自动转换 向下转型 : 通过父类对象(大范围)实例化子类对象(小范围) ...

  4. avd manager或sdk manager无法打开

    最近开始搞安卓,使用AS启动项目时老是报各种错误,而网上这方面的资料很多都解决不了.只能边实验边做. 定位到avd manager或sdk manager无法打开,网上找了很多资料,都不能解决,知道看 ...

  5. Android 在代码中安装 APK 文件

    废话不说,上代码 private void install(String filePath) { Log.i(TAG, "开始执行安装: " + filePath); File a ...

  6. springmvc系列一 之配置介绍(包含官网doc)

    1.springmvc 官网参考地址: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 2 ...

  7. JavaScript——class与原型对象

    原型对象的意义 通过new 一个构造函数,我们能够获得一个实例,在new 的过程中,程序会在内存中申请一块区域,同时我们可以加参数,所以每个对象都不一样. 原型对象则是同一个构造函数 new 出来的所 ...

  8. AIX 10G HA RAC卸载

    删除 1:crs_stat –t资源都停掉 2:停ha 3: 删除oracle 4:删除crs 5: 删除ha smit hacmp 6: 删除vg exportvg 7;卸载hacmp smitty

  9. OpenFlow_tutorial_2_Install_Required_Software

    一.Required Software 我操作系统用的 ubuntu 18.04.vm image的OS是ubuntu14.04,这两个系统的GUI应该已经不兼容了,如果使用ubuntu18.04的主 ...

  10. Relational Algebra 关系代数

    Relational Algebra Relational Algebra is the mathematical basis for the query language SQL Introduct ...