题意:

  有一堆的学生关系,要将他们先分成两个组,同组的人都不互不认识,如果不能分2组,输出No。若能,则继续。在两组中挑两个认识的人(每组各1人)到一个双人房。输出需要多少个双人房?

思路:

  先判定是否为二分图,可以用黑白着色法(DFS或BFS都行)。若是二分图,再进行匹配,用匈牙利算法,注:给的是整个图,没有区分男女,用邻接表比较好。

 #include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=;
vector< vector<int> > vect;
int col[N];
int girl[N];
int vis[N];
int n, m; bool color(const int x) //x可达的点着色
{
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(col[x]==col[t]) return false; //出错了,同色
if(!col[t])
{
col[t]=-col[x];//性别标为1和2
if(color(t)==false) return false; //只有刚着色的才需要深搜
}
}
return true; //只有单个点才会到这
} bool is2()//二分图判断,注意其可能并不是个连通图
{
memset(col,,sizeof(col));
for(int i=; i<=n; i++) //黑白着色
{
if(!col[i]) //可能有多个连通分量
{
col[i]=;
if(!color(i))
{
//cout<<i<<endl;
return false;
}
}
}
return true;
} int find(int x)
{
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!vis[t])
{
vis[t]=;
if(!girl[t] || find(girl[t])) //未匹配,或能为其对象另匹配
{
girl[t]=x;
return true;
}
}
}
return false;
} int hungary()
{
int cnt=;
memset(girl,,sizeof(girl));
for(int i=; i<=n; i++)
{
memset(vis,,sizeof(vis));
if(find(i)) cnt++;
}
return cnt;
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, c;
while(cin>>n>>m)
{
vect.clear();
vector<int> tmp;
for(int i=; i<=n; i++) vect.push_back(tmp);//切忌用resize,会惨死在这 for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
vect[a].push_back(b);
vect[b].push_back(a);
}
if(!is2()&& (puts("No"),) ) continue; //仅仅为了少一行代码
printf("%d\n",hungary()>>); //二分图匹配,匈牙利
}
return ;
}

AC代码

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(二分匹配 匈牙利算法 邻接表实现)

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

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

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

  4. HDU 2444 The Accomodation of Students二分图判定和匈牙利算法

    本题就是先推断能否够组成二分图,然后用匈牙利算法求出最大匹配. 究竟怎样学习一种新算法呢? 我也不知道什么方法是最佳的了,由于看书本和大牛们写的匈牙利算法具体分析,看了几乎相同两个小时没看懂,最后自己 ...

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

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

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

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

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

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

  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 StudentsTime Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. Jquery.linq 使用示例

    http://linqjs.codeplex.com/ /*Linq JS*/ //range var aa = Enumerable.range(1, 10).toArray(); var kk = ...

  2. 在线最优化求解(Online Optimization)之一:预备篇

    在线最优化求解(Online Optimization)之一:预备篇 动机与目的 在实际工作中,无论是工程师.项目经理.产品同学都会经常讨论一类话题:“从线上对比的效果来看,某某特征或因素对xx产品的 ...

  3. 【Memcache】下载和安装

    下载: Win7 64bit 系统 下载过过很多版本,都无法安装,最后到这里下载,成功安装: http://blog.couchbase.com/memcached-windows-64-bit-pr ...

  4. 提升SQL Server速度整理索引碎片

    转载:http://wenku.baidu.com/view/f64c8a707fd5360cba1adbea.html SQL Server2005索引碎片分析和解决方法   毫无疑问,给表添加索引 ...

  5. xml存储图片 二进制存储图片

    一.保存图片到XML文件 /// <summary> /// 保存图片到XML文件 /// </summary> private void UploadImageToXml() ...

  6. IP地址总结

    1.网际协议IP : 网际协议 IP 是 TCP/IP 体系中两个最主要的协议之一.与 IP 协议配套使用的还有四个协议: 地址解析协议 ARP (Address Resolution Protoco ...

  7. hibernate的简单学习(第一天)

    sql脚本: -- Create table drop table T_PERSON; create table T_PERSON ( id ) PRIMARY KEY, name ), passwo ...

  8. struts2学习笔记(4)——数据类型转换

    回过头来看昨天的那个例子. 在昨天的例子中,只转换了一个Point类,如果想转换多个Point类怎么办呢?在昨天的例子上面做一个小的修改. 首先在input.jsp页面中修改几个输入框. <s: ...

  9. 取消 EditText 自动聚焦弹出输入法界面

    方法一(实测可行): 参考http://www.cnblogs.com/dream-cichan/p/aaaa.html 当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严 ...

  10. SendMessage、PostMessage原理

    SendMessage.PostMessage原理 本文讲解SendMessage.PostMessage两个函数的实现原理,分为三个步骤进行讲解,分别适合初级.中级.高级程序员进行理解,三个步骤分别 ...