BZOJ 1059 Hungarian Algorithm

https://www.lydsy.com/JudgeOnline/problem.php?id=1059

The provincial competition we didn't solve this problem

It's a pity

The problem is just the maximum matching of bipartite graph

The Wildcow mentor said this problem is a classical model of the bipartite graph

gosh, it's me to weak

I have not learnt the theory of graph before the provincial competition

The code of AC:

#include<bits/stdc++.h>
using namespace std;
const int N=40010;
int A[300][300],head[N],Next[N],ver[N],v[N],match[N];
int tot=0,ans=0;
void add(int x,int y){
	ver[++tot]=y;
	Next[tot]=head[x];
	head[x]=tot;
}
bool dfs(int x){
	for(int i=head[x],y;i;i=Next[i]){
		if(v[y=ver[i]]==ans) continue;
		v[y]=ans;
		if(match[y]==0||dfs(match[y])){
			match[y]=x;
			return 1;
		}
	}
	return 0;
}
int main(){
	int T;
	cin>>T;
	while(T--){
		int n;
		cin>>n;
		tot=0;
		for(int i=0;i<N;++i){
			head[i]=ver[i]=Next[i]=v[i]=match[i]=0;
		}
		for(int i=1;i<=n;++i){
			for(int j=1;j<=n;++j){
				cin>>A[i][j];
				if(A[i][j])
					add(i,j);
			}
		}
		ans=1;
		for(int i=1;i<=n;++i){
			if(dfs(i)) ++ans;
		}
		if(ans-1==n) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80273364
今日推荐