hdu2444
来源:互联网 发布:ubuntu ant安装 编辑:程序博客网 时间:2024/05/17 03:54
/*
分析:
二分匹配。
判断一个图是不是二分图,是的话求二分匹配最大匹配数。
主要是怎么判断一个图是不是二分图。不妨选取某个点作为起点并
染为某种颜色、同时把与它相邻的元素染为对立的颜色,进行BFS,如果
到那步发现当前点和相邻点的颜色一样,那么就出现了矛盾,就不是二
分图。
2012-12-01
*/
分析:
二分匹配。
判断一个图是不是二分图,是的话求二分匹配最大匹配数。
主要是怎么判断一个图是不是二分图。不妨选取某个点作为起点并
染为某种颜色、同时把与它相邻的元素染为对立的颜色,进行BFS,如果
到那步发现当前点和相邻点的颜色一样,那么就出现了矛盾,就不是二
分图。
2012-12-01
*/
#include"stdio.h"#include"string.h"#include"stdlib.h"#include"queue"using namespace std;#define N 211int n,m;int color[N];int match[N],visit[N];struct Eage{int from,to,next;}eage[N*N];int tot,head[N];void add(int a,int b){eage[tot].from=a;eage[tot].to=b;eage[tot].next=head[a];head[a]=tot++;}void get_map(){int i;int a,b;tot=0;memset(head,-1,sizeof(head));for(i=0;i<m;i++){scanf("%d%d",&a,&b);add(a,b);add(b,a);}}int Color(){int j,v;queue<int>q;int now;int c_next;memset(color,-1,sizeof(color));color[1]=1;q.push(1);while(!q.empty()){now=q.front();q.pop();c_next=1-color[now];for(j=head[now];j!=-1;j=eage[j].next){v=eage[j].to;if(color[v]==-1){color[v]=c_next;q.push(v);}elseif(color[v]==color[now])return 1;}}return 0;}int DFS(int k){int j,v;for(j=head[k];j!=-1;j=eage[j].next){v=eage[j].to;if(visit[v])continue;visit[v]=1;if(match[v]==-1 || DFS(match[v])){match[v]=k;return 1;}}return 0;}int main(){int i,l;int ans;while(scanf("%d%d",&n,&m)!=-1){get_map();if(Color()){printf("No\n");continue;}ans=0;memset(match,-1,sizeof(match));for(i=1;i<=n;i++){memset(visit,0,sizeof(visit));ans+=DFS(i);}printf("%d\n",ans/2);}return 0;}
- hdu2444
- hdu2444
- hdu2444(二分匹配)
- HDU2444(最大匹配)
- Hdu2444二分图
- HDU2444(二分图)
- hdu2444二分匹配
- HDU2444--The Accomodation of Students
- HDU2444 The Accomodation of Students
- hdu2444二分图最大匹配
- hdu2444 二分图判定+匹配
- hdu2444 The Accomodation of Students
- hdu2444 The Accomodation of Students
- 【HDU2444】The Accomodation of Students
- hdu2444(判断二分图)
- The Accomodation of Students---hdu2444
- HDU2444-The Accomodation of Students
- HDU2444 The Accomodation of Students
- 学Java最值得读的10本英文书
- git 分支 主干
- Linux的shell编程——语法
- 条件编译
- Windows 平台的 Git 图形界面客户端
- hdu2444
- 很久以前开心网很火时候的群发代码
- 小甲鱼PE详解之区块表(节表)和区块(节)(PE详解04)
- 学习UEditor
- 在Eclipse中使用sequoyah插件配置Android NDK环境
- Shell编程基础——例子
- win7,win2003防止暴力破解 方法
- VOIP中STUN,TURN和ICE协议
- stl二分用法