dfs版二分图判定+无向二分图的最大匹配

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
vector<int> p[201];
int vis[201];
int color[201];
int result[201];
int n;

//-------------------------------------//dfs版判断二分图 
//搜索到点u,并且给u上色为c 
bool judge(int u,int c){
	color[u]=c;
	int len=p[u].size(); 
	for(int i=0;i<len;i++){
		//如果邻接点已经染色,并且和其颜色相同,说明有奇环 
		if(color[p[u][i]]==c)return false;
		//如果邻接点没有染色,判断将其染成不同的颜色后继续深搜判断 
		if(color[p[u][i]]==-1&&!judge(p[u][i],1-c)){
			return false;
		}
	}
	//没有奇环的形成便可以形成二分图 
	return true;
} 
//其实Color函数是为了解决非连通图的二分图判定
//若此无向图是连通图,只需要judge函数即可 
bool Color(){
	for(int i=1;i<=n;i++){
		if(color[i]==-1){
			//找到了新的连通分块
			//如果有一个连通分块有奇环便不是二分图 
			if(!judge(i,1))return false;
		}
	}
	return true;
}
//---------------------------//匈牙利算法求无向图的最大匹配 
bool dfs(int u){
	int len=p[u].size();
	for(int i=0;i<len;i++){
		if(!vis[p[u][i]]){
			vis[p[u][i]]=1;
			if(result[p[u][i]]==-1||dfs(result[p[u][i]])){
				result[p[u][i]]=u;
				result[u]=p[u][i];//有向图可以没有此句 
				return true;
			}
		}
	}
	return false;
}
int solve(){
	memset(result,-1,sizeof(result));
	int ans=0;
	for(int i=1;i<=n;i++){
		if(result[i]!=-1)continue;
			memset(vis,0,sizeof(vis));
			if(dfs(i))ans++;
	}
	return ans;
}
//----------------------------------
int main(){
	int k,x,y;
	while(~scanf("%d %d",&n,&k)){
		for(int i=1;i<=k;i++){
			scanf("%d %d",&x,&y);
			p[x].push_back(y);//建立无向图 
			p[y].push_back(x);
		}
		//--------先判二分图 
		memset(color,-1,sizeof(color));
		if(Color()){//如果是二分图 
			cout<<solve()<<endl;
		}
		else cout<<"No"<<endl;
		//一定记得清干净vector 
		for(int i=1;i<=n;i++){
			p[i].clear();
		}
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章