标签:
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
No 3
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=220; int mp[maxn][maxn]; int vis[maxn],col[maxn]; int used[maxn],link[maxn]; int n,m,flag; bool bfs(int x) { queue<int>q; q.push(x); col[x]=1; while(!q.empty()) { int xx=q.front(); q.pop(); REPF(i,1,n) { if(mp[xx][i]&&col[i]==-1) { q.push(i); col[i]=!col[xx]; } if(mp[xx][i]&&col[i]==col[xx]) return false; } } return true; } bool ok() { flag=0; REPF(i,1,n) { if(col[i]==-1&&!bfs(i)) { flag=1; break; } } if(flag) return false; return true; } bool dfs(int x) { REPF(i,1,n) { if(mp[x][i]&&!used[i]) { used[i]=1; if(link[i]==-1||dfs(link[i])) { link[i]=x; return true; } } } return false; } void work() { CLEAR(link,-1); int res=0; REPF(i,1,n) { CLEAR(used,0); if(dfs(i)) res++; } printf("%d\n",res>>1); } int main() { int x,y; while(~scanf("%d%d",&n,&m)) { CLEAR(col,-1); CLEAR(mp,0); REP(i,m) { scanf("%d%d",&x,&y); mp[x][y]=mp[y][x]=1; } if(!ok()) puts("No"); else work(); } return 0; }
HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/43463179