hdu 2444The Accomodation of Students 二分匹配+dfs分组
来源:互联网 发布:淘宝可以说好评返现吗 编辑:程序博客网 时间:2024/05/16 15:42
题目大意:输入有n个人和m对关系,要求把他们分成两组,每组的人中不能有互相认识的,如果不可以分成这样的两组,输出NO,否则输出这两组中最多人的人数。
用染色法,如果有环的话,环里面的点数为奇数的话,就会出现矛盾的情况,就不能分成两组。
#include<stdio.h>#include<cstring>#include<iostream>#include<vector>using namespace std;#define N 211#define clr(a,b) (memset(a,b,sizeof(a)))int link[N],col[N];bool vis[N];vector<int>Map[N];void init(){ clr(link,-1); clr(vis,false); clr(col,-1); for(int i=0;i<N;i++) Map[i].clear();}int find(int u){ for(int i=0;i<Map[u].size();i++) { int v=Map[u][i]; if(!vis[v]) { vis[v]=true; if(link[v]==-1||find(link[v])) { link[v]=u; return 1; } } } return 0;}bool dfs(int u,int tar){ for(int i=0;i<Map[u].size();i++) { int v=Map[u][i]; if(col[v]==-1) { vis[v]=true; col[v]=tar^1; if(!dfs(v,col[v])) return false; } else if(col[v]==(tar^1)) vis[v]=true; else if(col[v]==tar) return false; } return true;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); Map[a].push_back(b); } int flag=0; for(int i=1;i<=n;i++) { if(vis[i]) continue; col[i]=1; if(!dfs(i,col[i])) { flag=1; break; } } if(flag) { puts("No"); continue; } int ans=0; for(int i=1;i<=n;i++) { clr(vis,false); if(find(i)) ans++; } printf("%d\n",ans); } return 0;}
0 0
- hdu 2444The Accomodation of Students 二分匹配+dfs分组
- hdu The Accomodation of Students(二分匹配)
- hdu 2444-The Accomodation of Students(二分匹配)
- hdu 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students (交叉染色+二分匹配)
- 【HDU】2444 The Accomodation of Students 二分匹配
- 【二分匹配】 HDU 2444 The Accomodation of Students
- hdu 2444 The Accomodation of Students(二分匹配)
- HDU 2444 The Accomodation of Students 二分匹配
- hdu 2444 The Accomodation of Students 【二分图匹配】
- hdu2444 二分匹配 The Accomodation of Students
- The Accomodation of Students (二分匹配)
- HDU 2444 The Accomodation of Students 判断是否为二分图,二分图的最大匹配
- hdu 2444 The Accomodation of Students(是否二分图,找二分最大匹配数)
- hdu 2444 The Accomodation of Students (判断二分图,二分图最大匹配)
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
- hdu 2444 The Accomodation of Students 二分图判定+二分匹配个数
- iOS动画--UIView自带动画效果、Block动画
- 虚拟机入门
- 单例模式
- 学习笔记-SQL基础语法
- 两个背景图片滚动的效果
- hdu 2444The Accomodation of Students 二分匹配+dfs分组
- 第12周项目2 求N组数的最大公约数
- mini2440的串口在Qt上实现
- postgreSQL9.3日志配置
- DMA技术
- 刘首岐将演电影《谁的青春不叛逆》 叛逆少女转型成演员
- GIT操作基本详解(1)
- 各种资源(持续更新中)
- scala的极强编辑器:sublime3