C. Brr Brrr Patapim

time limit per test

2 seconds

memory limit per test

256 megabytes

Brr Brrr Patapim is trying to learn of Tiramisù's secret passcode, which is a permutation∗ of 2⋅n elements. To help Patapim guess, Tiramisù gave him an n×n grid G, in which Gi,j (or the element in the i-th row and j-th column of the grid) contains pi+j, or the (i+j)-th element in the permutation.

Given this grid, please help Patapim crack the forgotten code. It is guaranteed that the permutation exists, and it can be shown that the permutation can be determined uniquely.

∗A permutation of m integers is a sequence of m integers which contains each of 1,2,...,m exactly once. For example, 1,3,2 and 2,1 are permutations, while 1,2,4 and 1,3,2,3 are not.

Input

The first line contains an integer t --- the number of test cases (1≤t≤200).

The first line of each test case contains an integer n (1≤n≤800).

Each of the following n lines contains n integers, giving the grid G. The first of these lines contains G1,1,G1,2,...,G1,n; the second of these lines contains G2,1,G2,2,...,G2,n, and so on. (1≤Gi,j≤2⋅n).

It is guaranteed that the grid encodes a valid permutation, and the sum of n over all test cases does not exceed 800.

Output

For each test case, please output 2n numbers on a new line: p1,p2,...,p2n.

Example

Input

Copy

复制代码

3

3

1 6 2

6 2 4

2 4 3

1

1

2

2 3

3 4

Output

Copy

复制代码
5 1 6 2 4 3 
2 1 
1 2 3 4 

解题说明:此题是一道数学题,题目给一个 n × n 的网格 G,其中 G[i][j] 等于一个长度为 2n 的排列 p 中第 (i + j) 个元素的值。也就是说,网格暴露了 p[2] 到 p[2n] 的所有值,但 p[1] 没有直接给出。需要还原整个排列 p。因为 p 是 1 到 2n 的排列,所以只要用数组 b 标记网格中出现过的数字,再从 1 到 2n 扫一遍,第一个没被标记的就是 p[1]。

cpp 复制代码
#include<stdio.h>
int main() 
{
	int t;
	scanf("%d", &t);
	int a[850][850];
	while (t--)
	{
		int n = 0;
		int b[4005] = { 0 };
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				scanf("%d", &a[i][j]);
				b[a[i][j]] = 1;
			}
		}
		for (int i = 1; i <= 2 * n; i++)
		{
			if (b[i] == 0)
			{
				printf("%d ", i);
				break;
			}
		}
		int i = 1, j = 1, count = 1;
		while (i <= n && j <= n)
		{
			printf("%d ", a[i][j]);
			if (count % 2 == 0)
			{
				j++;
			}
			else
			{
				i++;
			}
			count++;
		}
		printf("\n");
	}
	return 0;
}
相关推荐
路溪非溪2 小时前
python标准库使用总结
开发语言·python
Zwawa3 小时前
嵌入式OLED表驱动菜单:用一张表、一个栈和一个窗口重写表驱动菜单
c语言·单片机·嵌入式
SomeOtherTime3 小时前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan3 小时前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso5 小时前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
huaqianzkh6 小时前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
沐晓时光17 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
Zwawa20 小时前
C语言函数指针与宏实战:从回调、表驱动菜单到X-Macro,讲透嵌入式代码解耦
c语言·单片机·嵌入式
David猪大卫1 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习