ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P1129 [ZJOI2007] 矩阵游戏(二分图匹配)

2022-01-20 15:00:43  阅读:114  来源: 互联网

标签:二分 GCC ch int 矩阵 P1129 ZJOI2007 include define


题目链接:点击这里

题目大意:
给定一个 n × n n\times n n×n 的矩阵,每个元素有 0 / 1 0/1 0/1 两种可能 ,可以交换两行或两列,问是否可以通过若干次交换使得矩阵的主对角线上元素都是 1 1 1

题目分析:
可以将行和列抽象成二分图的两个集合,每行每列都是对应集合的一个元素,然后认识 a [ i ] [ j ] = 1 a[i][j]=1 a[i][j]=1 就是 i , j i,j i,j 有一条边
仔细分析后我们会发现,交换行只是相当于给行重命名了,并不改变原图的形状。即我们可以在保持当前二分图结构不变的情况下,把一侧点的编号进行重命名,这与交换是等价的。
随意要想让第一行与第一列匹配,第二行与第二列匹配,只需要让原图的最大匹配等于 n n n 即可

具体细节见代码:

// Problem: P1129 [ZJOI2007] 矩阵游戏
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1129
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
//#define int  ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e3+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
int n,mp[maxn][maxn],p[maxn];
bool vis[maxn];
bool match(int id)
{
	for(int i = 1;i <= n;i++)
	{
		if(mp[id][i] && !vis[i])
		{
			vis[i] = true;
			if(!p[i] || match(p[i]))
			{
				p[i] = id;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int t = read();
	while(t--)
	{
		n = read();
		for(int i = 1;i <= n;i++)
			for(int j = 1;j <= n;j++) mp[i][j] = read();
		for(int i = 1;i <= n;i++) p[i] = 0;
		int res = 0;
		for(int i = 1;i <= n;i++)
		{
			for(int j = 1;j <= n;j++) vis[j] = 0;
			if(match(i)) res++;
		}
		puts(res==n ? "Yes" : "No");
	}
	return 0;
}


标签:二分,GCC,ch,int,矩阵,P1129,ZJOI2007,include,define
来源: https://blog.csdn.net/qq_39641976/article/details/122550991

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有