HDU 2444 The Accomodation of Students
首先是要构造二分图,然后二分图的最大匹配。
还有没完全证明过我的方法的正确性,但是AC了.....
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<algorithm> using namespace std;const int INF=0x7FFFFFFF; const int maxn=200*200+10; const int Maxn=200+10; int N,M; int U[maxn],V[maxn]; int F[Maxn]; vector<int>G[Maxn]; int St; int dis[Maxn],flag[Maxn]; queue<int>Q; int Belong[Maxn];int nx,ny; int g[Maxn][Maxn]; int cx[Maxn],cy[Maxn]; int mk[Maxn];void init() {for(int i=0; i<=N; i++) G[i].clear();memset(F,0,sizeof F);memset(Belong,0,sizeof Belong);for(int i=0; i<=N; i++) dis[i]=INF;nx=N,ny=N;memset(g,0,sizeof(g)); }void SPFA() {while(!Q.empty()) Q.pop();memset(flag,0,sizeof flag);flag[St]=1;dis[St]=0;Q.push(St);while(!Q.empty()){int h=Q.front();Q.pop();flag[h]=0;for(int i=0; i<G[h].size(); i++){if(dis[h]+1<dis[G[h][i]]){dis[G[h][i]]=dis[h]+1;if(!flag[G[h][i]]){flag[G[h][i]]=1;Q.push(G[h][i]);}}}} }int path(int u) {for(int v=1; v<=ny; v++){if(g[u][v]&&!mk[v]){mk[v]=1;if(cy[v]==-1||path(cy[v])){cx[u]=v;cy[v]=u;return 1;}}}return 0; }int MaxMatch() {int res=0;memset(cx,-1,sizeof(cx));memset(cy,-1,sizeof(cy));for(int i=1; i<=nx; i++){if(cx[i]==-1){memset(mk,0,sizeof(mk));res=res+path(i);}}return res; }int main() {while(~scanf("%d%d",&N,&M)){init();for(int i=1; i<=M; i++) scanf("%d%d",&U[i],&V[i]);for(int i=1; i<=M; i++){G[U[i]].push_back(V[i]);G[V[i]].push_back(U[i]);}for(int i=1; i<=N; i++)if(dis[i]==INF) St=i,SPFA();for(int i=1; i<=N; i++){if(dis[i]%2==0) Belong[i]=2;else Belong[i]=1;}int Fail=0;for(int i=1; i<=M; i++)if(Belong[U[i]]==Belong[V[i]]){Fail=1;break;}if(Fail==1) printf("No\n");else{for(int i=1; i<=M; i++){if(Belong[U[i]]==1&&Belong[V[i]]==2)g[U[i]][V[i]]=1;if(Belong[V[i]]==1&&Belong[U[i]]==2)g[V[i]][U[i]]=1;}printf("%d\n",MaxMatch());}}return 0; }
转载于:https://www.cnblogs.com/zufezzt/p/4784590.html
HDU 2444 The Accomodation of Students相关推荐
- HDU 2444 The Accomodation of Students 二分图匹配
HDU 2444 The Accomodation of Students 二分图匹配 题目来源: HDU 题意: 给出学生数n和关系数m,接下来给出m个关系. 要求将学生分成两部分,每一部分不能有互 ...
- HDU 2444 The Accomodation of Students (二部图+染色)
The Accomodation of Students Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Ja ...
- HDU——2444 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(判断二分图,二分图最大匹配)
题意: 题意: 有n个人,m对人相互认识: 问能否分成两个组,组内任意两个人之间不认识: 若不能,则输出No: 若能,则相互认识的两个人一间房,求最多需要几间房: 给出一些学生的认识情况,比如A和B认 ...
- HDU - 2444 The Accomodation of Students(二分图判断+二分图最大匹配)
题目链接:点击查看 题目大意:给出n个学生,他们之间有m个认识关系,但认识关系不具有传递性,比如A认识B,B认识C,A不一定认识C,现在给出认识关系,先判断是否能够将所有学生分为两个集合A和B中去,集 ...
- HDU 2444:The Accomodation of Students(二分图判定+匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:给出边,判断这个是否是一个二分图,并求最大匹配. 思路:先染色法求出是否是一个二分图,然后再匈牙利求 ...
- (匹配)The Accomodation of Students --HDU --2444
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2444 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 【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 ...
- hdu 2444(二分图的判断以及求最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2444思路:首先要判断能否构成二分图,用bfs对当前点u染色,对u的邻接点v的颜色进行判断,如果为染色, ...
最新文章
- java 获取泛型的type,如何获取泛型的Type类型
- 热点 | Excel不“香”了,数据分析首选Pyhton!
- 如何将spring源码作为导入eclipse中,变成一个普通的项目(git、github)
- 用FIO测试存储性能
- A problem occurred starting process 'command '/home/jason/Android/Sdk/build-tools/23.0.3/aapt
- 在生产环境中,阿里云如何构建高性能云原生容器网络?(含 PPT 下载)
- java.lang.NoClassDefFoundError:如何解决–第3部分
- Java-计算程序运行时间
- BAT警示:没试过这个方法就别说会数据分析!!
- 数据结构与算法顺序表数组版
- python清理日志脚本_Python日志:如果在程序运行时删除了日志文件,则创建新的日志文件(RotatingFileHandler)...
- 批量修改图幅lisp_AutoCAD中自动批量修改图形文件的程序
- 联通屏蔽80端口后利用NAT端口映射穿透解决WEB网站应用发布
- 论运营型CRM和分析型CRM
- Excel VBA简介
- 达人篇:3.1.3)FAI 首件检验
- 计算机专业应届研究生面试自我介绍,计算机专业应届生面试自我介绍
- Alpha课堂展示(麻瓜制造者)
- IOS视频编辑,视频裁剪,视频拼接,音频处理,视频处理
- linux密码是什么加密方式,Linux系统的几种加密
热门文章
- [转载]实现Application Tile 更新
- 8086汇编语言命令速查与详解
- sublime Text3安装和verilog安装
- oracle数据库12528,解决(Oracle)ORA-12528: TNS: 监听程序: 所有适用例程都无法建立新连接 问题...
- 如何彻底解决pip install慢的问题
- Awk 实战详解教程
- 单片机学不会怎么办?单片机从哪里开始学?
- php网站怎么伪静态,php怎么实现网页伪静态
- 计算机音乐谱打上花火,原神乐谱打上花火
- 【MySQL】MySQL表的增删改查(进阶-下)