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. SSRS域账号下 User 'XXX' does not have required permissions的处理方法

    SSRS安装完成后,点击Report Manager URL,提示:User 'XXX' does not have required permissions. Verify that suffici ...

  2. 员工管理系统(集合与IO流的结合使用 beta3.0 BufferedReader / ObjectOutputStream)

    Employee.java package cn.employee_io; public class Employee { private String empId; private String n ...

  3. 1043 幸运号码 数位DP

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 设dp[i][j]表示前i位数中,i位数的和为j时的所有情况. 转 ...

  4. js类、原型——学习笔记

    js 内置有很多类,我们用的,都是从这些类实例化出来的. function Object () {} function Array () {} function String () {} functi ...

  5. [ BZOJ 2038 ] Hose

    \(\\\) Description 给出一个数列,多次询问,每次给出一个区间 \([l_i,r_i]\) ,问在区间中随意选两个位置不同的数,是同一个数的概率有多大. \(n,m\le 5\time ...

  6. No rule to make target ...

    在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...

  7. jq一些常用的交互效果

    jq回到顶部: //回到顶部 $(window).scroll(function() { //执行处理的代码 var a = document.body.scrollTop; if($(documen ...

  8. centOS linux 下nginx编译安装详解

    Nginx的官方网站是 www.nginx.org   Nginx的下载地址是:http://nginx.org/en/download.html   由 于官网的设计非常简洁不大气不上档次,所以我们 ...

  9. Java Socket 连接 Client端 和 Server端

    Client端: import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;i ...

  10. CSS3 radio 或checkbox 自定义 样式

    .style-radio {position:relative;width:15px;height:15px;outline:none;} .style-radio:after {position:a ...