HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
来源:互联网 发布:java 获取根目录路径 编辑:程序博客网 时间:2024/05/22 13:38
题意:给你N个学生与M对关系,每个关系形如(i,j),表示第i个学生与第j个学生相互认识. 现在要你将N个学生分成两组,每组中的任意两个学生都不相互认识.如果N个学生能分组的话,那么就将这N个学生放到尽量多的双人房中去.每个房间放2个相互认识的人.问你最多需要多少房间?
思路:先判断是否二分图,然后和POJ1466同样的做法
#include<cstdio>#include<cstring>#include<vector>#include<cmath>using namespace std;const int maxn=1000;struct Max_Match{ int n,m; vector<int> g[maxn]; bool vis[maxn]; int left[maxn]; int color[maxn]; void init(int n) { this->n=n; // this->m=m; for(int i=1;i<=n;i++) g[i].clear(); memset(left,-1,sizeof(left)); } bool bipartite(int u){for (int i = 0;i<g[u].size();i++){int v = g[u][i];if (color[v] == color[u])return false;else if (color[v]==0){color[v]=3-color[u];if (!bipartite(v))return false;}}return true;} bool match(int u) { for(int i=0;i<g[u].size();i++) { int v=g[u][i]; if(!vis[v]) { vis[v]=true; if(left[v]==-1 || match(left[v])) { left[v]=u; return true; } } } return false; } int solve() {memset(color,0,sizeof(color));for (int i = 1;i<=n;i++)if(!color[i]){color[i]=1;if (!bipartite(i))return -1;} int ans=0; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(match(i)) ans++; } return ans; }}MM;int T;int main(){int n,m; while (scanf("%d%d",&n,&m)!=EOF){MM.init(n);for (int i = 1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);MM.g[u].push_back(v);MM.g[v].push_back(u);}int ans = MM.solve();if (ans == -1)printf("No\n");elseprintf("%d\n",ans/2);}}
0 0
- 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 2444The Accomodation of Students 二分图判定+找最大匹配
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- HDU 2444 The Accomodation of Students (二分图判定+最大匹配
- The Accomodation of Students(HDU-2444)(二分图判定与最大匹配)
- 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(是否二分图,找二分最大匹配数)
- hdu 2444 The Accomodation of Students【二分图染色+最大二分匹配数】
- 网页的宽度是采用百分比还是像素
- HDU 5479 Scaena Felix
- Android 的电池消耗优化 I-优化网络连接
- 联想G50笔记本如何安装系统
- android的五种布局模式
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
- 天声人語 20160213 アインシュタインの宿題
- 03环信好友管理 - 监听"加好友"的请求
- android 图片加载工具
- 单片机C语言 串口传输 结构体
- opencv 下载地址
- 日经春秋 20160213
- C语言memset详解
- LVS NAT 模式