思路:用染色法进行二分图判定,相邻的两个点间染不同的颜色,如果遇见相同的颜色,则不是二分图;
不是二分图的话输出No,否则跑Hungary求最大匹配;
点击打开链接
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> using namespace std; #define MEM(a,b) memset(a,b,sizeof a) #define pk push_back template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;} template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;} typedef long long ll; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int uN,vN,n,m; const int maxn = 220; int gg[maxn][maxn]; bool vis[maxn]; int link[maxn]; bool dfs(int u){ for (int i = 0;i < vN;i++){ if (gg[u][i] && !vis[i]){ vis[i] = true; if (link[i] == -1 || dfs(link[i])){ link[i] = u; return true; } } } return false; } inline int Hungary(){ MEM(link, -1); int ret = 0; for (int i = 0;i < uN;++i){ MEM(vis, false); if (dfs(i)) ret++; } return ret; } int color[maxn]; vector<int> vec[maxn]; bool SetColor(int u,int col){ color[u] = col; for (int i = 0;i < vec[u].size();++i){ int v = vec[u][i]; if (color[v] != -1){ if (color[v] == col) return false; continue; } if (!SetColor(v, !col)) return false; } return true; } int num[maxn]; int main() { // ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)){ for (int i = 0;i < n;++i) vec[i].clear(); int u,v; while(m--){ scanf("%d%d",&u,&v); u--,v--; vec[u].push_back(v); } MEM(color, -1); bool flag = true; for (int i = 0;i < n;++i){ if (color[i] != -1) continue; if (!SetColor(i,0)){ flag = false; break; } } if (!flag){ printf("No\n"); continue; } vN = uN = 0; for (int i = 0;i < n;++i){ if (color[i] == 0) num[i] = uN++; else num[i] = vN++; } MEM(gg, 0); for (int i = 0;i < n;++i){ for (int j = 0;j < vec[i].size();j++){ if (color[i] == 0){ u = num[i]; v = num[vec[i][j]]; gg[u][v] = 1; } else{ u = num[vec[i][j]]; v = num[i]; gg[u][v] = 1; } } } printf("%d\n",Hungary()); } return 0; }