标签:return ble 无向图 思路 pair some rip may ott
#include <iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int n,m; int Map[210][210]; int flag[210]; int used[210]; int vis[210]; bool bfs_judge(){//染色法判断是否为二分图 for(int i=0;i<210;i++) flag[i]=-1; for(int i=1;i<=n;i++){ if(flag[i]!=-1) continue; flag[i]=0; queue<int > q; q.push(i);//以还未被染过色的i点(还未被搜索过的点)为起始点展开bfs while(!q.empty()){ int head=q.front(); q.pop(); for(int j=1;j<=n;j++){ if(!Map[head][j]) continue; if(flag[j]!=-1&&flag[head]==flag[j]) return false; else if(flag[j]==-1){flag[j]=!flag[head]; q.push(j);} } } } return true; } bool match(int x){ for(int i=1;i<=n;i++){ if(!vis[i]&&Map[x][i]){ vis[i]=1; if(!used[i]||match(used[i])){ used[i]=x; return true; } } } return false; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ memset(Map,0,sizeof(Map)); for(int i=1;i<=m;i++){ int a,b; scanf("%d%d",&a,&b); Map[a][b]=Map[b][a]=1;//无向图 } if(bfs_judge()==false) {printf("No\n"); continue;} int ans=0; memset(used,0,sizeof(used)); for(int i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); if(match(i)) ans++; } printf("%d\n",ans/2);//每一对匹配被计算了两次 } return 0; }
[图论][二分图判断]The Accomodation of Students
标签:return ble 无向图 思路 pair some rip may ott
原文地址:https://www.cnblogs.com/lllxq/p/9007358.html