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. 用CSS3实现上下左右箭头

    225deg 向上箭头 135deg向下箭头45deg向右箭头 -45deg向左箭头

  2. python将文件写成csv文件保存到本地

    举个例子: import csv import os path='/tmp/' file='test.csv' def generate_csv(path,file): if not os.path. ...

  3. PHP 魔术引号

    1.魔术引号的作用是什么? ​ 魔术引号设计的初衷是为了让从数据库或文件中读取数据和从请求中接收参数时,对单引号.双引号.反斜线.NULL加上一个一个反斜线进行转义,这个的作用跟addslashes( ...

  4. 课程3——程序结构关键字

    声明:本系列随笔主要用于记录c语言的常备知识点,不能保证所有知识正确性,欢迎大家阅读.学习.批评.指正!!你们的鼓励是我前进的动力.严禁用于私人目的.转载请注明出处:http://www.cnblog ...

  5. 复习做UWP时涉及到的几种加密签名相关

    本人菜鸟一枚,大学里凭兴趣学了一点WP的皮毛,后来又幸运(或者不幸)的进了一家专注于Windows生态的公司做了一段时间的UWP.在博客园写点自己遇到的东西,作为分享,也作为自己的备忘,如果有错误的地 ...

  6. raid知识

    1,raid形象理解(饮水机模型) http://dingyichao.blog.51cto.com/442449/698762     2,raid利用率 3,raid详细理解 raid0 raid ...

  7. Navicat for mysql 显示中文乱码问题

    使用navicat for mysql 打开数据库时,使用Console插入和查询数据显示乱码 处理过程 1.查看数据库编码为" utf8 -- UTF-8 Unicode",也就 ...

  8. LeetCode-Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  9. android的adb详解(多设备时adb调用)

    在多设备(模拟器)时,想要直接用logcat查看其中一台的状态,或者直接把应用安装到目标设备上时,需要指定设备号.adb devices这个指令可以得到当前设备的序列号(serialNumber).比 ...

  10. js判断页面点击事件

    <input type="submit" name="sb1" id="sb1" onclick="queryclick() ...