C. Nastya Is Transposing Matrices

探讨通过特定操作将矩阵A转换成矩阵B的可能性,分析发现元素仅能与同斜线元素交换,利用set存储比较,实现矩阵的有效变换验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Nastya Is Transposing Matrices

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task.

Two matrices AA and BB are given, each of them has size n×mn×m. Nastya can perform the following operation to matrix AA unlimited number of times:

  • take any square square submatrix of AA and transpose it (i.e. the element of the submatrix which was in the ii-th row and jj-th column of the submatrix will be in the jj-th row and ii-th column after transposing, and the transposed submatrix itself will keep its place in the matrix AA).

Nastya's task is to check whether it is possible to transform the matrix AA to the matrix BB.

Example of the operation

As it may require a lot of operations, you are asked to answer this question for Nastya.

A square submatrix of matrix MM is a matrix which consist of all elements which comes from one of the rows with indeces x,x+1,…,x+k−1x,x+1,…,x+k−1 of matrix MM and comes from one of the columns with indeces y,y+1,…,y+k−1y,y+1,…,y+k−1 of matrix MM. kk is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes).

Input

The first line contains two integers nn and mm separated by space (1≤n,m≤5001≤n,m≤500) — the numbers of rows and columns in AA and BBrespectively.

Each of the next nn lines contains mm integers, the jj-th number in the ii-th of these lines denotes the jj-th element of the ii-th row of the matrix AA (1≤Aij≤1091≤Aij≤109).

Each of the next nn lines contains mm integers, the jj-th number in the ii-th of these lines denotes the jj-th element of the ii-th row of the matrix BB (1≤Bij≤1091≤Bij≤109).

Output

Print "YES" (without quotes) if it is possible to transform AA to BB and "NO" (without quotes) otherwise.

You can print each letter in any case (upper or lower).

Examples

input

Copy

2 2
1 1
6 1
1 6
1 1

output

Copy

YES

input

Copy

2 2
4 4
4 5
5 4
4 4

output

Copy

NO

input

Copy

3 3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 6
3 8 9

output

Copy

YES

Note

Consider the third example. The matrix AA initially looks as follows.

 

题意:

     1.有一种操作,选出一个子矩阵,通过旋转交换元素

     2.给你2个矩阵,能不能通过有限次变换,把矩阵1,变为矩阵2

 

思路:

     你可以通过模拟,得出一个结论,任意一个元素只能和为同一斜线的元素进行交换,而且可以通过有限次的交换,得到任意一种序列,那就好办了,直接一个set或者multiset把同一斜线的元素存起来,再比较就完事了

 

code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<set>
using namespace std;
int a[510][510],b[510][510],n,m,x=-1,y=1,flag=0;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>a[i][j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>b[i][j];
	for(int i=1;i<=n;i++)
	{
		x=-1,y=2;
		multiset<int>aa,bb;
		aa.insert(a[i][1]);
		bb.insert(b[i][1]);
		while(i+x>=1&&y<=m)
		{
			aa.insert(a[i+x][y]);
			bb.insert(b[i+x][y]);
			x--;
			y++;
		}
		set<int>::iterator ta,tb;
		ta=aa.begin();
		tb=bb.begin();
		while(ta!=aa.end())
		{
			if(*ta!=*tb)
			{
				flag=1;
				break;
			}
			ta++;
			tb++;
		}
	}
	for(int i=1;i<=m;i++)
	{
		x=1,y=n-1;
		multiset<int>aa,bb;
		aa.insert(a[n][i]);
		bb.insert(b[n][i]);
		while(i+x<=m&&y>=1)
		{
			aa.insert(a[y][i+x]);
			bb.insert(b[y][i+x]);
			x++;
			y--;
		}
		set<int>::iterator ta,tb;
		ta=aa.begin();
		tb=bb.begin();
		while(ta!=aa.end())
		{
			if(*ta!=*tb)
			{
				flag=1;
				break;
			}
			ta++;
			tb++;
		}
	}
	if(flag)
		cout<<"NO"<<endl;
	else
		cout<<"YES"<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值