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. phpmyadmin后台拿shell方法总结

    方法一: CREATE TABLE `mysql`.`xiaoma` (`xiaoma1` TEXT NOT NULL ); INSERT INTO `mysql`.`xiaoma` (`xiaoma ...

  2. ASP.NET 里的 JSON操作

    最近项目中需要用到 JSON操作,google了一下 找到了几个比较好的操作方法.... 一 .使用 mircosoft 提供的 .NET Framework 自带的 json操作方法 1. 使用Ja ...

  3. 20Spring_JdbcTemplatem模板工具类

    JdbcTemplate 是Spring提供简化Jdbc开发模板工具类.为了更好的了解整个JdbcTemplate配置数据库连接池的过程,这篇文章不采用配置文件的方式,而是采用最基本的代码 的方式来写 ...

  4. 对兼容ie浏览器所遇到的问题及总结

    1,若直接给一个元素设置absolute定位.在浏览器缩放的时候.位置会错位.解决的方法是给外层的元素设置为relative定位. 2,低版本ie浏览器不支持placeholder属性 3,盒模型上规 ...

  5. php基础04:字符串函数

    <?php //1.strlen(),strlen() 函数返回字符串的长度,以字符计. echo strlen("hello world"); echo "< ...

  6. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  7. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  8. unitty导出工程嵌入iOS原生工程中出现黑屏,但是模型还是可以扫。

    一般上出现这个问题,其实就是因为两个注意点没有搞清楚.我们分析一下,如果我们的模型或者视屏能够出来但是屏幕还是黑屏的.说明我们的unity的组件其实已经加载出来了.但是供我们交互的那个Layer类并没 ...

  9. 『方案』《女友十年精华》 ORC 图片 文字识别 详解

    目的需求: 2008年,遇到一本电子书 <女友十年精华> 觉得很美,想 私藏 这些文章: >网络搜索文章 —— 没有找到: >反编译程序 —— 所有文字 都是图片格式(部分文章 ...

  10. OSX 上安装 Scrapy 的那些坑

    Scrapy 这个爬网框架真心不错,但在OSX上安装总是会出现各种的问题,在这里就作一个收集汇总.我的系统环境是 OS X El Capitan (10.11.1) 首先要保证 pip , virtu ...