洛谷 P1129 [ZJOI2007]矩阵游戏


题目:

传送门


题意:

给出一个有黑、白两色组成的 n ∗ n n*n nn矩阵,现在询问我们该矩阵在经过若干次操作后,能否使 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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值