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
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

 

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