time limit per test  4 seconds
memory limit per test  256 megabytes
input  standard input
output  standard output

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 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.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Solution:

这道题真心好,妙极。

Tutorial上给出的是图论向的解法,但我压根就没往那方向想过,never ever!

这里讨论一个正常人的解法。

将所有数从小到大排序,再往里填。我相信这是最自然的想法了。

紧接着而来的问题(trouble)就是按什么顺序填充那些相同的数。

我最开始的想法是很朴素也很naive的:

给排序加若干辅助的优先级。

这些辅助优先级是观察样例得来的猜想,当然不大可能靠得住。

-----------------------------------------------------------------------------------------

后来参考了某篇题解,解决了上面提到的trouble。

注意到题目中要求:such that the relative order of the elements in each row and each column remains the same.这句话其实是在表格元素上定义了一个等价关系

  a~b: a=b 且存在一条由a到b的曼哈顿路径且该路径上的所有点(元素)都属于集合{x | x=a}.

(这样描述看起来还是不够形式化 :D)

按这个等价关系可将表格(全集)分成若干等价类。

而题目正是要求压缩(映射)之后维持这个等价关系。

因此,实际上要在排序的基础上(对相同元素)进一步维护出等价类(equivelance classes)。

说到等价关系自然就想到并查集

---------------------------------------------------------------------------------------------------------------------------------------------

细节就不多说了,coding时自能体会(也许读者才不会像LZ,犯那样SB的错误呢)。

Implementation:

/*
In mathematics, an equivalence relation is a binary relation that is at the same time a
reflexive relation, a symmetric relation and a transitive relation. As a consequence of
these properties relation provides a partition of a set onto equivalence classes.
*/
#include <bits/stdc++.h>
using namespace std; const int N(1e6+); struct node{
int x, y, v;
bool operator <(const node &a)const{
return v<a.v;
}
}a[N], mx[N], my[N]; int par[N], ma[N], ans[N]; int m, n; int ID(node &a){
return a.x*m+a.y;
} int find(int x){
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x, int y){
x=find(x), y=find(y);
par[x]=y;
ma[y]=max(ma[y], ma[x]);
} int main(){
cin>>n>>m; for(int i=; i<n; i++){
for(int j=; j<=m; j++){
int x;
cin>>x;
a[i*m+j]={i, j, x};
}
} for(int i=; i<=m*n; i++)
par[i]=i; sort(a+, a+m*n+); //two-pointers
for(int i=, j; i<=m*n; ){
// cout<<a[i].v<<endl;
for(j=i; j<=m*n && a[j].v==a[i].v; j++){ ma[ID(a[j])]=max(ans[ID(mx[a[j].x])], ans[ID(my[a[j].y])]); //error-prone if(a[j].v==mx[a[j].x].v){
unite(ID(a[j]), ID(mx[a[j].x]));
}
else{
mx[a[j].x]=a[j];
} if(a[j].v==my[a[j].y].v){
unite(ID(a[j]), ID(my[a[j].y]));
}
else{
my[a[j].y]=a[j];
}
} for(; i!=j; i++){
int id=ID(a[i]);
// cout<<i<<' '<<find(ID(a[i]))<<endl;
// ma[ID(a[i])]=ma[find(ID(a[i]))]+1 //OMG!
ans[id]=ma[find(id)]+;
}
} for(int i=; i<n; i++){
for(int j=; j<=m; j++)
cout<<ans[i*m+j]<<' ';
cout<<'\n';
} return ;
}

---------------------------------------

其实还有许多可总结的,累了,坑留着往后再填吧。

Codeforces 650C Table Compression的更多相关文章

  1. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  2. Codeforces 651E Table Compression【并查集】

    题目链接: http://codeforces.com/problemset/problem/650/C 题意: 给定n*m的矩阵,要求用最小的数表示每个元素,其中各行各列的大小关系保持不变. 分析: ...

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

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

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

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

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

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  6. codeforces 651E E. Table Compression(贪心+并查集)

    题目链接: E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  7. 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 ...

  8. CF650C Table Compression

    CF650C Table Compression 给一个 \(n\times m\) 的非负整数矩阵 \(a\),让你求一个 \(n\times m\) 的非负整数矩阵 \(b\),满足以下条件 若 ...

  9. Code Forces 650 C Table Compression(并查集)

    C. Table Compression time limit per test4 seconds memory limit per test256 megabytes inputstandard i ...

随机推荐

  1. Android SQLite (一) 数据库简介

    大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...

  2. MVC ajaxSubmit上传图片

    注意事项: 1.提交form,必须引用jquery.form.min.js 2.不要使用mvc自带的Ajax.Form() 1.页面cshtml <form name="frmInpu ...

  3. 怎么用JS截取字符串中第一个和第二个字母间的部分?

    一.JS中用正则判断字符串是否有匹配正则的字符串部分,格式如下: /[a-zA-Z](.*?)[a-zA-Z]/.test('1a123d45678901a2') “.test”前面的部分是正则表达式 ...

  4. Chrome调试工具简单介绍

    作为前端开发者都知道,快捷键F12可以打开chrome调试工具.firefox可以打开firebug工具.“工欲善其事,必先利其器”,对调试工具的掌握,能大大提高我们调试代码的效率.因为我平常chro ...

  5. 一个背景图实现自定义spinner样式

    如下界面:由一个spinner两个EditText一个Button实现,为了保持界面的统一性,需要把默认的spinner样式改成类似下面的样式. xml文件布局如下图 这里用一个LinerLayout ...

  6. 关联规则算法(The Apriori algorithm)详解

    一.前言 在学习The Apriori algorithm算法时,参考了多篇博客和一篇论文,尽管这些都是很优秀的文章,但是并没有一篇文章详解了算法的整个流程,故整理多篇文章,并加入自己的一些注解,有了 ...

  7. RxJava简介

    RxJava简介 本文为前段时间学习RxJava时留下的历史遗留笔记,仅作纪念,科学的大神教学帖子在这里-> 给 Android 开发者的 RxJava 详解 通过链式调用序列实现基于事件流的异 ...

  8. javascript模块化详解

    模块化:每个模块只完成一个独立的功能,然后提供该功能的接口.模块间通过接口访问.模块中的(过程和数据)对于其它模块来说是私有的(不能访问修改) 原始人写法: function m1(){ //... ...

  9. Windows 2008如何绑定MAC防范ARP攻击!

    Windows 2008如何绑定MAC防范ARP攻击!   阅读(1974)暂无评论时间:2010-11-23 22:52:13   在Windows server 2003时代,通过arp 这命令即 ...

  10. linux中的帮助命令

    关键字 man --help help 1.man (1)查看命令 man 命令用来查看别的命令的信息和用法,如man ls表示查看ls的介绍以及用法: (2)查看配置文件的帮助文档 linux下的配 ...