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.
题目还算好懂
M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小。
当时看题,大小相等关系,一眼想到差分约束
于是建边,spfa tle on 15......
改用 dijkstra+heap tle on 66...真是66666
查看数据好像是网格图
每一层更新了好多好多次。。。简直丧心病狂!
比赛后看题解居然并查集。。当时完全没有想到
并查集的做法就是
合并每一行、每一列相等的元素
所有原值从小到大排序后,填空
填的时候 找到并查集中和他一个集合的所有元素
统计这些元素对应每一行每一列的最大值
然后填最大值+1就可以啦
其实还是就结了好长时间,因为感觉查询的时候总是n^2的复杂度
睡醒突然想到,不需要啊
只需要把他们都合并到一个根上,合并时把最大值更新在root上
这样只需要访问root就知道了集合对应的最大值
其中hx hy就是保存当前行列最大值对应的x,y
这样就可以按行列直接合并了
合并时把孩子的最大值统计到根上
查询时直接查询根的最大值+1就可以了
#include<bits/stdc++.h>
using namespace std;
const int lim=1e6+100;
struct self
{
int x,y,w;
self(){}
self(int xx,int yy,int ww)
{
x=xx;
y=yy;
w=ww;
}
}s[lim];
int cmp(self a,self b)
{
return a.w<b.w;
}
int f[lim];
int t[lim];
int find(int i)
{
return i==f[i]?i:f[i]=find(f[i]);
}
int ans[lim];
int maxx[lim],maxy[lim];
self hx[lim],hy[lim];
int m,n;
int h(int x,int y)
{
return (x-1)*n+y;
}
int tot=0;
int x,y,w;
int main()
{
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&w);
tot++;
s[tot]=self(i,j,w);
}
sort(s+1,s+tot+1,cmp);
for(int i=1;i<=tot;i++)
f[i] = i;
int ll = 1;
int rr;
for(int k = 2; k <= tot+1; k++)
{
if(s[k].w != s[k-1].w || k == tot+1)
{
rr = k-1;
for(int i = ll; i <= rr; i++)
{
int x = s[i].x;
int y = s[i].y;
int w = s[i].w;
int r = find(h(x, y));
if(hx[x].w != w)
hx[x] = s[i];
else
{
//cout<<" add "<<x<<","<<y<<" to "<<hx[x].x<<","<<hx[x].y<<endl;
int l = find(h(hx[x].x, hx[x].y));
if(l != r)
{
f[l] = r;
t[r] = max(t[r], t[l]);
}
}
if(hy[y].w != w)
hy[y] = s[i];
else
{
//cout<<" add "<<x<<","<<y<<" to "<<hy[y].x<<","<<hy[y].y<<endl;
int l = find(h(hy[y].x, hy[y].y));
if(l!=r)
{
f[l] = r;
t[r] = max(t[r], t[l]);
}
}
t[r] = max(t[r], maxx[x]);
t[r] = max(t[r], maxy[y]);
//cout<<"num "<<x<<","<<y<<" = "<<t[r]<<endl;
}
for(int i = ll; i <= rr; i++)
{
int num = h(s[i].x, s[i].y);
int root = find(num);
ans[num] = t[root]+1;
maxx[s[i].x] = ans[num];
maxy[s[i].y] = ans[num];
}
ll = k;
}
}
for(int i = 1; i <= tot; i++)
{
printf("%d",ans[i]);
if(i%n == 0)
printf("\n");
else
printf(" ");
}
return 0;
}