bzoj1059[ZJOI2007]矩阵游戏

因为是不断的交换对吧,所以呢,一列的话,不就是所谓的会不会存在行和列的完美匹配嘛,1A开心

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++) 
using namespace std;
inline LL read()
{
	LL d=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
	return d*f;
}
#define N 1005
int a[N][N],l[N];
bool y[N];
int n,m;

bool match(int x)
{
	fo(i,1,n)
	if (y[i]==0&&a[x][i])
	{
		y[i]=1;
		if (l[i]==0||match(l[i]))
		{
			l[i]=x;
			return 1;
		}
	}
	return 0;
}

bool check()
{
	fo(i,1,n)
	{
		memset(y,0,sizeof(y));
		if (match(i)==0) return 0;
	}
	return 1;
}

int main()
{
	int t=read();
	while(t--)
	{
		memset(l,0,sizeof(l));
		memset(a,0,sizeof(a));
		n=read();
		fo(i,1,n)
		fo(j,1,n)
		{
			int x=read();
			if(x)a[i][j]=1;
		}
		if(check())cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}


你可能感兴趣的:(二分图匹配)