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

题意:给你一个n*n的由数字组成的矩阵,让你尽量缩小这个矩阵中的值,使得缩小前后两个矩阵每一行每一列任意两个数对应的大小关系一致。

思路:先考虑所有的点对应的数都不同,那么我们只要对所有的数排个序,然后依次编号就行了,相当于一次离散化。那么现在给你的矩形是有相同元素的,可以观察到同一行或者同一列的数缩小后也是一样的,且它们最小能缩小到的数是符合它们各自所在行的之前算出来的最小数+1,我们可以用并查集把这些数都统一成一个数,然后就能算了。

<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1000050
struct node{
int x,y,num,idx;
}a[maxn];
bool cmp(node a,node b){
return a.num<b.num;
}
int X[maxn],Y[maxn],x[maxn],y[maxn]; int ans[maxn];
int pre[maxn]; int findset(int x)
{
int i=x,j,r=x;
while(r!=pre[r]){
r=pre[r];
}
while(i!=pre[i]){
j=pre[i];
pre[i]=r;
i=j;
}
return r; } int main()
{
int n,m,i,j,c,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
int tot=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&c);
tot++;
a[tot].x=i;a[tot].y=j;
a[tot].idx=tot;a[tot].num=c;
pre[tot]=tot;
}
}
sort(a+1,a+1+tot,cmp); for(i=1;i<=tot;){
for(j=i;a[i].num==a[j].num;j++);
int r,c;
for(k=i;k<j;k++){
r=a[k].x;c=a[k].y;
if(!x[r])x[r]=a[k].idx ;
else{
pre[findset(a[k].idx ) ]=findset(x[r]);
}
if(!y[c])y[c]=a[k].idx ;
else{
pre[findset(a[k].idx ) ]=findset(y[c]);
}
}
for(k=i;k<j;k++){
int q=findset(a[k].idx );
int answer=max(X[a[k].x ],Y[a[k].y ] )+1;
ans[q]=max(ans[q],answer );
}
for(k=i;k<j;k++){
X[a[k].x ]=Y[a[k].y ]=ans[a[k].idx ]=ans[findset(a[k].idx ) ];
x[a[k].x ]=y[a[k].y ]=0;
}
i=j;
}
int flag=1; for(i=1;i<=tot;i++){
if(flag){
flag=0;printf("%d",ans[i]);
}
else{
printf(" %d",ans[i]);
}
if(i%m==0){
flag=1;printf("\n");
}
}
}
return 0;
}

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

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

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

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

  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 Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

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

  5. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

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

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

  7. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  8. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  9. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

随机推荐

  1. Ts有限状态机

    ts版本的有限状态机 最近做小游戏要做切换人物状态,花点时间写了一个有限状态机,使用语言为Ts,也可改成自己的语言 按照目前的逻辑,这个可以继续横向扩展,某些做流程管理 先上预览图 Fsm:状态机类 ...

  2. Pandas数据分析练手题(十题)

    数据集下载地址:https://github.com/Rango-2017/Pandas_exercises --------------------------------------------- ...

  3. Java 安全之Weblogic 2018-2628&2018-2893分析

    Java 安全之Weblogic 2018-2628&2018-2893分析 0x00 前言 续上一个weblogic T3协议的反序列化漏洞接着分析该补丁的绕过方式,根据weblogic的补 ...

  4. 【Linux】rsync中sending incremental file list时间优化

    每次使用rsync的时候,前面出现sending incremental file list 这句之后要等待很长时间 查了很多帖子和官方文档后,发现是-c这个选项的问题, -v, --verbose ...

  5. leetcode 864. 获取所有钥匙的最短路径(BFS,状态压缩)

    题目链接 864. 获取所有钥匙的最短路径 题意 给定起点,要求在最短步骤内收集完所有钥匙,遇到每把锁之前只有 有对应的钥匙才能够打开 思路 BFS+状态压缩典型题目 先确定起点和总的钥匙数目,其次难 ...

  6. 【linux】系统编程-6-POSIX标准下的信号量与互斥锁

    目录 前言 8. POSIX信号量 8.1 概念 8.2 POSIX无名信号量 8.3 POSIX有名信号量 8.4 POPSIX信号量与system V信号量的区别 9. POSIX互斥锁 9.1 ...

  7. Kioptix Level 1

    1. 简介 Vulnhub是一个提供各种漏洞环境的靶场平台. 个人学习目的:1,方便学习更多类型漏洞.2,为OSCP做打基础. 下载链接 https://www.vulnhub.com/entry/k ...

  8. 基于FPGA的光口通信开发案例|基于Kintex-7 FPGA SFP+光口的10G UDP网络通信开发案例

    前言 自著名华人物理学家高锟先生提出"光传输理论",实用化的光纤传输产品始于1976年,经历了PDH→SDH→DWDM→ASON→MSTP的发展历程.本世纪初期,ASON/OADM ...

  9. Java 栈的使用

    讲栈之前,要先讲一下Deque双端队列 既可以添加到队尾,也可以添加到队首 既可以从队首获取又可以从队尾获取 public interface Deque<E> extends Queue ...

  10. 关于springboot2.X使用外部tomcat服务器进行部署的操作详细步骤

    1.修改pom.xml文件(4个地方) ①<packaging>war</packaging>将其中的jar该为war ②<dependency> <grou ...