Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 #define bug(x) cout<<"bug"<<x<<" "<<endl; const int N=2e2+10,M=1e6+10,inf=2e9+10,mod=1e9+7; const ll INF=1e18+10; int n,m; int mp[N][N]; int linker[N]; bool used[N]; bool dfs(int a) { for(int i=0;i<n;i++) if(mp[a][i]&&!used[i]) { used[i]=true; if(linker[i]==-1||dfs(linker[i])) { linker[i]=a; return true; } } return false; } int hungary() { int result=0; memset(linker,-1,sizeof(linker)); for(int i=0;i<n;i++) { memset(used,0,sizeof(used)); if(dfs(i)) result++; } return result; } queue<int>q; int color[N]; int bfs(int s) { while(!q.empty())q.pop(); q.push(s); while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0;i<n;i++) { if(mp[x][i]) { if(color[i]==-1) color[i]=color[x]^1,q.push(i); else if(color[i]==color[x]) return 0; } } } return 1; } int check() { memset(color,-1,sizeof(color)); for(int i=0;i<n;i++) { if(color[i]==-1) { color[i]=1; if(!bfs(i))return 0; } } return 1; } int main() { while(~scanf("%d%d",&n,&m)) { memset(mp,0,sizeof(mp)); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); mp[u-1][v-1]=1; } if(!check())printf("No\n"); else { int cnt=hungary(); printf("%d\n",cnt); } } return 0; }
hdu 2444 The Accomodation of Students 判断二分图+二分匹配
原文:http://www.cnblogs.com/jhz033/p/6412989.html