题目:
题意:
给出一个有黑、白两色组成的 n ∗ n n*n n∗n矩阵,现在询问我们该矩阵在经过若干次操作后,能否使 1 , 1 1,1 1,1、 2 , 2 2,2 2,2、 3 , 3 3,3 3,3……上都是黑点
分析:
黑白两色,显然考虑二分图匹配(说得好像我会其他一样的)
我的做法是把行和列当成两个二分图
当
n
=
3
n=3
n=3时:
而题目中问的是对角线上的,即
x
=
y
x=y
x=y,所以原问题就转化成了在一个二分图中,我们能否将黑点所在的行匹配到相应的列,且正好有
n
n
n。那我们就可以用匈牙利算法求解
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<ctime>
#include<iomanip>
#include<string>
#include<bitset>
#include<deque>
#include<set>
#define LL long long
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;
}
int t[205][205],n;
int tf[205],ak_noip[205];
bool dfs(int k)
{
for(int i=1;i<=n;i++)
{
if(t[k][i]&&!tf[i])
{
tf[i]=1;
if(!ak_noip[i]||dfs(ak_noip[i])) {ak_noip[i]=k;return 1;}
}
}
return 0;
}
int main()
{
int q=read();
while(q--)
{
n=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
t[i][j]=read();
int sum=0;
memset(ak_noip,0,sizeof(ak_noip));
for(int i=1;i<=n;i++)
{
memset(tf,0,sizeof(tf));
sum+=dfs(i);
}
if(sum==n) printf("Yes\n");
else printf("No\n");
}
return 0;
}