codeforces 650C (并查集)

C. Table Compression
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 gzbzzip 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.


题意:构造出一个最大数字最小的矩阵,使得矩阵里面每一行每一列的任意两个数之间的相对关系和原矩阵一样。


对于某一个数字,他所在的行列中和他相等的元素构成一个集合,这个集合的元素必然是同时修改成相同值的,所以

可以对于同行同列的相同值用一个并查集来维护,然后改变时所有的元素都修改成所有元素所在行列中最大值+1.


#include <bits/stdc++.h>
using namespace std;
#define maxn 1000005

int n, m;
int ans[maxn];
struct node {
    int x, y, num;
}p1[maxn], p2[maxn];
int mp[maxn];
int l[maxn], r[maxn];//行最大 列最大

int id (int x, int y) {
    return x*m+y;
}

bool cmp1 (const node &a, const node &b) {
    return a.num < b.num || (a.num == b.num && a.x < b.x) || (a.num == b.num && a.x == b.x && a.y < b.y);
}
bool cmp2 (const node &a, const node &b) {
    return a.num < b.num || (a.num == b.num && a.y < b.y) || (a.num == b.num && a.y == b.y && a.x < b.x);
}
int fa[maxn];
#define find Find
int find (int x) {
    return fa[x] == x ? fa[x] : fa[x] = find (fa[x]);
}

vector <int> gg[maxn];
void solve () {
    for (int i = 0; i < n*m; i++) {
        gg[i].clear ();
    }
    for (int i = 0; i < n*m; i++) {
        int fa = find (i);
        if (i != fa)
            gg[fa].push_back (i);
    }
    for (int i = 0; i < n*m; i++) {
        int u = id (p1[i].x, p1[i].y);
        int fa = find (u);
        if (u != fa) {
            continue;
        }
        else {
            int cur = max (l[u/m], r[u%m]);
            for (int j = 0; j < gg[u].size (); j++) {
                int pos = gg[u][j];
                cur = max (cur, max (l[pos/m], r[pos%m]));
            }
            cur++;
            ans[u] = cur;
            l[u/m] = max (l[u/m], cur);
            r[u%m] = max (r[u%m], cur);
            for (int j = 0; j < gg[u].size (); j++) {
                int pos = gg[u][j];
                ans[pos] = cur;
                l[pos/m] = max (l[pos/m], cur);
                r[pos%m] = max (r[pos%m], cur);
            }
        }
    }
    for (int i = 0; i < n*m; i++) {
        printf ("%d%c", ans[i], i%m == m-1 ? '\n' : ' ');
    }
}

int main () {
    //freopen ("in.txt", "r", stdin);
    scanf ("%d%d", &n, &m);
    for (int i = 0; i < n*m; i++) {
        scanf ("%d", &mp[i]);
        p1[i] = p2[i] = (node) {i/m, i%m, mp[i]};
    }
    memset (l, 0, sizeof l);
    memset (r, 0, sizeof r);
    for (int i = 0; i < n*m; i++) {
        fa[i] = i;
    }
    sort (p1, p1+n*m, cmp1);
    sort (p2, p2+n*m, cmp2);
    for (int i = 1; i < n*m; i++) { 
        if (p1[i].num == p1[i-1].num && p1[i].x == p1[i-1].x) {
            int f1 = find (id (p1[i].x, p1[i].y)), f2 = find (id (p1[i-1].x, p1[i-1].y));
            fa[f2] = f1;
        }
    }
    for (int i = 1; i < n*m; i++) { 
        if (p2[i].num == p2[i-1].num && p2[i].y == p2[i-1].y) {
            int f1 = find (id (p2[i].x, p2[i].y)), f2 = find (id (p2[i-1].x, p2[i-1].y));
            if (f1 == f2)
                continue;
            fa[f2] = f1;
        }
    }
    solve ();
    return 0;
}


你可能感兴趣的:(codeforces 650C (并查集))