匈牙利算法
https://blog.youkuaiyun.com/sixdaycoder/article/details/47680831
将找增广路径转化为找到出发点集合m里的另一点,match其实只对二分图非起点集的另一半有效
但是这道题并不明确哪一部分是起点集,我们是全部都搜,所以会重复,最后结果除以二
还有就是染色法,一开始是边输入边赋0,1。。。
个人感觉染色法两个关键,1,一定要是连通图,比如1,2认识,3,4认识,2,3再一认识,全乱套
2 取模运算
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <vector>
#define MAX 210
using namespace std;
int n,m,a,b,flag,ans;
int g[MAX][MAX];
int mark[MAX];
int color[MAX];
int match[MAX];
queue<int >q;
int findPath(int x){
int y;
for(int i=1;i<=n;i++){
if(g[x][i]){
if(!mark[i]){
mark[i]=1;
if(match[i]==-1||findPath(match[i])){
match[i]=x;
return 1;
}
}
}
}
return 0;
}
int BFS(){
while(!q.empty())
q.pop();
memset(mark,0,sizeof(mark));
memset(color,-1,sizeof(color));
color[1]=0;
q.push(1);
while(!q.empty()){
int x=q.front();
q.pop();
mark[x]=1;
for(int i=1;i<=n;i++){
if(g[x][i]){
if(color[i]==-1){
color[i]=(color[x]+1)%2;
q.push(i);
}
else if(color[x]+color[i]!=1)
return false;
}
}
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
ans=0;
memset(g,0,sizeof(g));
memset(match,-1,sizeof(match));
memset(color,-1,sizeof(color));
while(m--){
scanf("%d%d",&a,&b);
g[a][b]=g[b][a]=1;
}
if(!BFS()){
printf("No\n");
}
else {
for(int i=1;i<=n;i++){ //之前是只二分图半块做起点,现在相当全图都搜,match数量翻倍
memset(mark,0,sizeof(mark));
if(findPath(i))
ans++;
}
printf("%d\n",ans/2);
}
}
return 0;
}