C. Table Compression

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

Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' 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 row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'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 and m (, 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 containing m 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
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.

题意:个数不超过1e6个数的二维数列;按照行与列数的相对大小尽可能的缩小为正整数,但不在同一行或同一列的数的缩放前后的大小没有关系;

输出缩放后的数列;

思路:排序后每次处理都是处理值相等的一串数据,并且是看成没没有填入到新数组中,这样使用并查集就可以得到“十”字形相等的根节点的最大值,即所有这棵并查集下的节点的值;x[i],y[i]来模拟并查集,X[],Y[]表示行列上一个值填到的数值,所以之后直接得到根节点所要填入的值;

#include<bits/stdc++.h>
using namespace std;
int i,j,k,n,m,T,tot;
const int N = ;
struct data{
int r,c,v,id;
}p[N];
bool cmp(const data &a,const data &b){return a.v < b.v;}
int f[N],X[N],Y[N],ans[N],x[N],y[N],tmp[N];
int Find(int a){return a==f[a]?f[a]:f[a]=Find(f[a]);}
int main()
{
scanf("%d%d",&n,&m);
for(i = ;i <= n;i++)
for(j = ;j <= m;j++){
scanf("%d",&p[++tot].v);
p[tot].r = i,p[tot].c = j;
p[tot].id = tot;
f[tot] = tot;
}
sort(p+,p++tot,cmp);
for(i = ;i <= tot;i = j){
for(j = i;p[i].v == p[j].v;++j);
for(k = i;k < j;k++){
int r = p[k].r, c = p[k].c;
if(!x[r]) x[r] = k;// 行并查
else f[Find(k)] = Find(x[r]);
if(!y[c]) y[c] = k;
else f[Find(k)] = Find(y[c]);//f[k]会因为十字型交叉而出错;
}
for(k = i;k < j;k++){//只是在之前的值的基础上得到,不是模拟填入值
int q = Find(k);
tmp[q] = max(tmp[q],max(X[p[k].r],Y[p[k].c])+);
}
for(k = i;k < j;k++){//根节点得到的是全体的值
x[p[k].r] = y[p[k].c] = ;
X[p[k].r] = Y[p[k].c] = ans[p[k].id] = tmp[Find(k)];
}
}
for(i = ;i <= tot;i++){
printf("%d ",ans[i]);
if(i%m == ) puts("");
}
}

codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集的更多相关文章

  1. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  2. Codeforces Round #345 (Div. 2) E. Table Compression(并查集)

    传送门 首先先从小到大排序,如果没有重复的元素,直接一个一个往上填即可,每一个数就等于当前行和列的最大值 + 1 如果某一行或列上有重复的元素,就用并查集把他们连起来,很(不)显然,处于同一行或列的相 ...

  3. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...

  6. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  7. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  8. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  9. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

随机推荐

  1. 应聘.net开发工程师常见的面试题(一)(转载)

    1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...

  2. redis的hash操作在集中式session中的应用

    在集群部署时,为了高可用性的目的,往往把session进行共享,共享分为两种:session复制和集中式管理. redis在session集中式管理中可以起到比较大的作用. 制约session集中式共 ...

  3. jQuery的自定义事件——滚轮

    这个案例类似于在地图上滚动滚轮,能缩小或者放大地图,分别用zoomIn和zoomOut来命名. 代码如下: //JS部分:<script src="jquery-1.10.2.min. ...

  4. Mybatis 动态获取字段值(不需要创建javabean)

    最近遇到一个这样的需求,如下: 用户可以通过自定义模板选择需要查询显示的字段,需要动态查询显示. 前提:数据库有一张表,里面有400多个字段. 要求:用户在前台可以自定义模板,一个模板可能对应x个字段 ...

  5. Callable、Future和FutureTask使用说明

    普通的创建线程,一种是直接继承Thread,另外一种就是实现Runnable接口.但是这两种都无法在执行完任务之后获取执行结果,Callable.Future就提供了这样的便利.   Future的方 ...

  6. Makefile的规则

    在讲述这个Makefile之前,还是让我们先来粗略地看一看Makefile的规则:最基本的编写规则的方法是从最终的源程序文件一个一个的查看源码文件.把它们要生成的目标文件作为目标,而C语言源码文件和源 ...

  7. jQuery 判断div是否shown

    // Checks for display:[none|block], ignores visible:[true|false] $(element).is(":visible") ...

  8. HTML5规范的本地存储

    在HTML5 中定义了两种本地存储的,Web Storage 和本地数据库 SQL Database . 用来检查判断浏览器是否支持 Web Storage if(window.localStorag ...

  9. 2015年校园招聘12家IT公司面试体验

    背景 2015年注定是一个不平凡的年头,作为一个应届毕业生,我也算是经历了工作上的大起大落.下面我先简单讲述一下自己的遭遇,然后根据自己亲身的面试经历,从一个学生的角度去谈谈自己对面试过的公司的一些看 ...

  10. Android Activity常用生命周期函数

    在Activity中主要有7个常用的周期函数,他们分别是: (一)onCreate 在Activity对象被第一次创建时调用 注: 从另一个Activity返回到前一个Activity时,不会调用该函 ...