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.
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 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.
2 2 1 2 3 4
1 2 2 3
4 3 20 10 30 50 40 30 50 60 70 90 80 70
2 1 3 5 4 3 5 6 7 9 8 7
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.
题意:给你n*m的矩阵,叫你转化成为另外一个矩阵,使得矩阵中的数字之和最小,且同一行,同一列的两个数的关系仍和原来的矩阵相同。
思路:因为同一行同一列的两个数仍要满足原来的等价关系,所以就相当于暗含了一个拓扑序,
那么同一行和同一列的数相同的数要怎么办呢?利用一个并查集把他们捆绑起来。(先对原来的序列进行一个排序)
#include<bits/stdc++.h> using namespace std; const int maxn=1001000; int f[maxn],rx[maxn],ry[maxn],ans[maxn],pre_rx[maxn],pre_ry[maxn]; struct node{ int num,x,y; }mp[maxn]; int find(int x){ while(x!=f[x]) x=f[x]=f[f[x]]; return f[x]; } void merge(int x,int y){ int xx=find(x),yy=find(y); if(xx!=yy) f[yy]=xx; } bool cmp(node x,node y){ return x.num<y.num; } int main(){ int n,m; scanf("%d%d",&n,&m); int cnt=1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%d",&mp[(i-1)*m+j]); mp[cnt].x=i,mp[cnt++].y=j;f[(i-1)*m+j]=(i-1)*m+j; } sort(mp+1,mp+n*m+1,cmp); int k; for(int i=1;i<=n*m;i=k){ for(k=i+1;k<=n*m&&mp[k].num==mp[i].num;k++); for(int j=i;j<k;j++){ int x=mp[j].x,y=mp[j].y; if(!rx[x]) rx[x]=(x-1)*m+y; else merge(rx[x],(x-1)*m+y); if(!ry[y]) ry[y]=(x-1)*m+y; else merge(ry[y],(x-1)*m+y); } for(int j=i;j<k;j++){ int x=mp[j].x,y=mp[j].y,r=find((x-1)*m+y); ans[r]=max(ans[r],max(pre_rx[x],pre_ry[y])+1); } for(int j=i;j<k;j++){ int x=mp[j].x,y=mp[j].y,r=find((x-1)*m+y); ans[(x-1)*m+y]=ans[r]; pre_rx[x]=pre_ry[y]=ans[r]; rx[x]=0,ry[y]=0; } } for(int i=1;i<=n*m;i++){ if(i%m==0) printf("%d\n",ans[i]); else printf("%d ",ans[i]); } return 0; }