Codeforces Round #345 (Div. 1) C. Table Compression

Little Petya is now fond of data compression algorithms. He has already studiedgz, bz,zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to namedis.

Petya decided to compress tables. He is given a table a consisting ofn rows and m columns that is filled with positive integers. He wants to build the tablea' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some rowi of the initial table ai, j < ai, k, then in the resulting tablea'i, j < a'i, k, and ifai, j = ai, k thena'i, j = a'i, k. Similarly, if in some columnj of the initial table ai, j < ap, j then in compressed tablea'i, j < a'p, j and ifai, j = ap, j thena'i, j = a'p, j.

Because large values require more space to store them, the maximum value in a' should be as small as possible.

Petya is good in theory, however, he needs your help to implement the algorithm.

Input

The first line of the input contains two integers n andm (, the number of rows and the number of columns of the table respectively.

Each of the following n rows contain m integers ai, j(1 ≤ ai, j ≤ 109) that are the values in the table.

Output

Output the compressed table in form of n lines each containingm integers.

If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.

Examples
Input
2 2
1 2
3 4
Output
1 2
2 3
Input
4 3
20 10 30
50 40 30
50 60 70
90 80 70
Output
2 1 3
5 4 3
5 6 7
9 8 7
Note

In the first sample test, despite the fact a1, 2 ≠ a21, they are not located in the same row or column so they may become equal after the compression.



 按原权值排序后贪心的从小的开始标号,最小的一定编为1,然后按顺序把剩下的数字依次重新编号,权值相同且可以通过行列间接直接连接在一起的数字在新表里的编号也

相同,可以通过并查集合并这些数字并保留其中最大的编号,写了一次就过了,然而代码依然写的又臭又长。


#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int tot,n,m,x,h[1000002],l[1000002],f[1000002],hv[1000002],lv[1000002],val[1000002];
struct point
{
	int x,y,v;
	int num()
	{
		return (x-1)*m+y;
	}
} a[1000002];
int find(int k)
{
	if(f[k] == k) return k;
	f[k]=find(f[k]);
	return f[k];
}
bool camp(point a,point b)
{
	return a.v<b.v;
}
void insert(int i,int j,int v)
{
	tot++;
	a[tot].x=i;
	a[tot].y=j;
	a[tot].v=v;
}
int main()
{
	cin.sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i <= n;i++)
	 for(int j=1;j <= m;j++)
	  {
	 	 cin>>x;
	 	 insert(i,j,x);
	  }
	sort(a+1,a+1+n*m,camp);
	for(int i=1;i <= n*m;i++) f[i]=i;
	int now=1;
	for(int i=1;i <= n*m+1;i++)
	{
		if(a[i].v != a[i-1].v)
		{
			int s=now,t=i-1;
			if(s <= t)
			{
				for(int j=s;j <= t;j++)
				{
					val[a[j].num()]=max(hv[a[j].x],lv[a[j].y])+1;
					if(h[a[j].x] == 0)
					{
						h[a[j].x]=a[j].num();
					}
					else 
					{
						if(val[find(h[a[j].x])] < val[find(a[j].num())])
						{
							val[f[h[a[j].x]]]=val[f[a[j].num()]];
						}
						f[f[a[j].num()]]=f[h[a[j].x]];
					}
					if(l[a[j].y] == 0)
					{
						l[a[j].y]=a[j].num();
					}
					else 
					{
						if(val[find(l[a[j].y])] < val[find(a[j].num())])
						{
							val[f[l[a[j].y]]]=val[f[a[j].num()]];
						}
						f[f[a[j].num()]]=f[l[a[j].y]];
					}
				}
				for(int j=s;j <= t;j++)
				{
					val[a[j].num()]=val[find(a[j].num())];
					hv[a[j].x]=lv[a[j].y]=val[a[j].num()];
					h[a[j].x]=l[a[j].y]=0;
				}
			}
			now=i;
		}
	}
	for(int i=1;i <= n*m;i++)
	{
		if(i % m == 0) 
		{
			cout<<val[i]<<endl;
		}
		else cout<<val[i]<<" ";
	}
 } 


你可能感兴趣的:(Codeforces Round #345 (Div. 1) C. Table Compression)