蒟蒻二分图零分求调
查看原帖
蒟蒻二分图零分求调
668649
osmanlin楼主2024/8/9 17:42
#include<iostream>
#include<cstring>
const int N=4e4+10;
const int INF=0x3f3f3f3f;
using namespace std;
int head[N],ne[N],to[N],tot,res;
int T,n,match[N];
bool st[N];
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline void add(int x,int y)
{
	to[++tot]=y;
	ne[tot]=head[x];
	head[x]=tot;
}
inline void init()
{
	memset(ne,0,sizeof(ne));
	memset(to,0,sizeof(to));
	memset(match,0,sizeof(match));
	memset(st,false,sizeof(st));
	res=0;
}
inline bool find(int x)
{
	for(int i=head[x];i;i=ne[i])
	{
		int y=to[i];
		if(!st[y])
		{
			st[y]=true;
			if(!match[y]||find(match[y]))
			{
				match[y]=x;
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	T=read();
	while(T--)
	{
		init();
		n=read();
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				int k;
				k=read();
				if(k)
				{
					add(i,j+n);
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			memset(st,false,sizeof(st));
			if(find(i))
			{
				res++;
			}
		}
		if(res==n)
		{
			cout<<"Yes"<<endl;
		}
		else cout<<"No"<<endl;
	}
	return 0;
}
/*
5
4
1 0 0 0
1 1 1 0
1 0 0 0
1 0 0 1
4
0 1 1 0
0 1 0 1
1 0 0 1
0 0 1 1
3
1 0 0
1 1 1
1 1 0
4
0 1 0 1
1 0 1 1
0 0 0 1
0 0 1 1
4
1 0 0 1
0 0 0 1
0 1 1 1
0 1 1 0
*/
2024/8/9 17:42
加载中...