B - Permutation Recovery

Statements

Vladislav invented a new algorithm of the distributed key construction.

The distributed key consists of 2n numerical sequences of length n. To construct the key Vladislav gets some permutation p of length n2. Next step is to put the elements of p into a square matrix a. The first n elements of p form the first row of a, next n elements form the second row, etc. Finally, 2n numerical sequences of the distributed key are rows and columns of the matrix a.

These 2n sequences were given to Vladislav in some arbitrary order. He spent a while trying to come up with an algorithm for recovering the permutation. Try to restore the permutation before the end of the round!

Input

The first input line contains one integer n (1 ≤ n ≤ 100).

Each of the next 2n lines contains n integers from 1 to n2, rows and columns of the matrix a in some order.

It is guaranteed that the input is correct, there is at least one suitable permutation p.

Output

Print a permutation p of integers from 1 to n2.

Example
Input
2
1 2
1 3
2 4
3 4
Output
1 2 3 4
Input
4
1 6 14 7
5 4 1 13
13 16 12 10
8 3 6 16
11 2 7 10
15 9 14 12
4 3 9 2
5 8 15 11
Output
5 4 1 13 8 3 6 16 15 9 14 12 11 2 7 10


题意:

一个n*n的矩阵,由行列随机拆成2n行,还原他。

算法:

更据第一列中相同的2个元素所在的索引,找出第一行第一列。

之后暴力,矩阵的转置,2种情况不做区分。


代码:

#include<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
int n;
int a[205][105];
int b[105][105];
int main()
{
memset(a,sizeof(a),0);
memset(b,sizeof(b),0);
scanf("%d",&n);
int first,second;
for(int i=1;i<=2*n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<2*n;i++)
{
for(int j=i+1;j<=2*n;j++)
{
if(a[i][1]==a[j][1])
{
first=i;
second=j;
}
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",a[second][i]);
}

for(int i=2;i<=n;i++)
{
int j=0;
for(j=1;j<=(2*n);j++)
{
if(j==first)
continue;
if(a[j][1]==a[first][i])
break;
}
// cout<<j<<endl;
for(int z=1;z<=n;z++)
printf("%d ",a[j][z]);
}
return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值