codeforces 650C (并查集)

本文探讨了表格压缩算法的设计与实现,通过并查集维护元素间的相对位置关系,确保压缩后的表格保持原始数据的相对顺序不变。算法目标是使压缩后的最大值尽可能小,通过迭代调整每个元素为所在行列中最大值加一的方式实现。实例演示了如何通过输入的表格数据生成符合要求的压缩矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值