You are given a table consisting of n rows and m columns.
Numbers in each row form a permutation of integers from 1 to m.
You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total.Operations can be performed in any order.
You have to check whether it's possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.
Each of next n lines contains m integers — elements of the table. It's guaranteed that numbers in each line form a permutation of integers from 1 to m.
If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes).
2 4 1 3 2 4 1 3 4 2
YES
4 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3
NO
3 6 2 1 3 4 5 6 1 2 4 3 5 6 1 2 3 4 6 5
YES
In the first sample, one can act in the following way:
- Swap second and third columns. Now the table is
1 2 3 4 1 4 3 2 - In the second row, swap the second and the fourth elements. Now the table is
1 2 3 4 1 2 3 4
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 10007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int a[100][100];
int judge();
int n,m;
int main()
{
int i,j;
int t;
scanf("%d %d",&n,&m);
for( i=1;i<=n;i++)
{
for( j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
if(m==1)
{
printf("Yes\n");
return 0;
}
if(judge())
{
printf("Yes\n");
return 0;
}
for(int i1=1;i1<=m;i1++)
{
for(int i2=i1+1;i2<=m;i2++)
{
//printf("i1=%d i2=%d\n",i1,i2);
for(int k=1;k<=n;k++)
{
int t;
t =a[k][i1];
a[k][i1] =a[k][i2];
a[k][i2] =t;
}
//printf("%d",judge());
if(judge())
{
printf("Yes\n");
return 0;
}
for(int k=1;k<=n;k++)
{
int t;
t =a[k][i1];
a[k][i1] =a[k][i2];
a[k][i2] =t;
}
}
}
printf("No\n");
return 0;
}
int judge()
{
int count=0;
int flag = 0;
int i,j;
for( i=1;i<=n;i++)
{
count=0;
for( j=1;j<=m;j++)
{
if(a[i][j]!=j)
{
count++;
}
}
if(count>2)
{
flag =1;
}
}
if(flag == 0)
return 1;
else
return 0;
}