【CodeForces - 266C】Below the Diagonal(模拟)

本文探讨了一种特殊矩阵转换问题,目标是通过行与列的交换,确保所有数值'1'位于矩阵主对角线之下。文章提供了详细的算法思路及AC代码实现,采用递归思想和暴力模拟方法,适用于数据规模不大于1000的情况。

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

You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix:

  1. Swap i-th and j-th rows of the matrix;
  2. Swap i-th and j-th columns of the matrix.

You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j.

Input

The first line contains an integer n (2 ≤ n ≤ 1000) — the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 ≤ xk, yk ≤ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one.

It is guaranteed that all positions are distinct.

Output

Print the description of your actions. These actions should transform the matrix to the described special form.

In the first line you should print a non-negative integer m (m ≤ 105) — the number of actions. In each of the next m lines print three space-separated integers t, i, j(1 ≤ t ≤ 2, 1 ≤ i, j ≤ n, i ≠ j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively.

Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them.

Examples

Input

2
1 2

Output

2
2 1 2
1 1 2

Input

3
3 1
1 3

Output

3
2 2 3
1 1 3
1 1 2

Input

3
2 1
3 2

Output

0

题意:给你一个n*n矩阵,其中n-1个格子填上了数字1,交换行和列,保证所有的1都在主对角线的下面。

思路:

一遇到这种矩阵交换的就感觉头痛,这道题数据量在1000以内,并且操作不超过1e5,时间也给了2000ms,因此可以暴力模拟一下(以后遇到想不出方法,但数据量不大的,就暴力一发试试)。除了暴力模拟这道题其实也有递归的思想在里面(就是不断的把问题规模缩小,只不过这道题是先解决n的再求n-1的,并且不用返回去而已),每次的操作相同,就是先把最后一列,与前面全部为0的一列交换,然后,如果最后一行全是0,就找一行不全是0的与它交换。n*n的矩阵的最后一行与一列操作完后,就把问题缩小,看成(n-1)*(n-1)的矩阵,重复以上操作,在对n-1的矩阵进行操作时,就不用考虑最后一行和最后一列了,因为不论怎么操作最后的行和列都是符合条件,因此我们就不用去更改它们了(因为并没有要求我们输出矩阵)。

ac 代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
struct node{
	int op,x,y;
}op[110000];
int mp[1500][1500]={0};
int main()
{
	int n,cnt=0;
	scanf("%d",&n);
	int x,y;
	int h[1510]={0},l[1510]={0};
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d",&x,&y);
		mp[x][y]=1;
		l[x]++;
		h[y]++;
	}
	for(int i=n;i>=1;i--)
	{
		if(h[i]!=0)
		for(int j=i-1;j>=1;j--)
		{
			if(h[j]==0)
			{
				op[cnt].op=2;
				op[cnt].x=i;
				op[cnt++].y=j;
				swap(h[i],h[j]);
				for(int k=1;k<=i;k++)
				{
					if(mp[k][i])
					{
						mp[k][j]=1;
						mp[k][i]=0;
					}
				}
				break;
			}
		}
		if(l[i]==0)
		for(int j=i-1;j>=1;j--)
		{
			
			if(l[j])
			{
				op[cnt].op=1;//一开始这一块写在if的外面了runtime error了
				op[cnt].x=i;
				op[cnt++].y=j;
				swap(l[j],l[i]);
				for(int k=1;k<i;k++)
				{
					if(mp[j][k])
					{
						mp[i][k]=mp[j][k];
						mp[j][k]=0;
						h[k]--;
					}
				}
				break;
			}
		}
		else
		{
			for(int k=1;k<i;k++)
			{
				if(mp[i][k])
				h[k]--;
			}
		}
		
	}
	cout<<cnt<<endl;
	for(int i=0;i<cnt;i++)
	{
		printf("%d %d %d\n",op[i].op,op[i].x,op[i].y);
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值