1. 程式人生 > >【CodeForces - 266C】Below the Diagonal(模擬)

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

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 x

k, 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;
}