AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 其它
    • 题解
    • 分享
    • 商店
    • 问答
  • 吐槽
  • 登录/注册

力扣1632,CF650C(拓扑排序 + 并查集)

作者: 作者的头像   冰冷酒 ,  2023-01-25 01:43:55 ,  所有人可见 ,  阅读 44


1


#include <bits/stdc++.h>

using namespace std;

const int N = 2000010;

int e[N], ne[N], h[N], idx;
int p[N], du[N];

struct node {
    int x, t;
    bool operator < (const node &w) const {
        if (x != w.x) return x < w.x;
        return t < w.t;
    }
};

vector<node> tmp[N];
unordered_map<int, int> mp;
int t, n, m;

void init() {
    idx = t = 0;
    memset(h, -1, sizeof(h));
    for (int i = 0; i < N; i ++) p[i] = i, du[i] = 0;
    for (int i = 0; i < N; i ++) tmp[i].clear();
    mp.clear();
}

int find(int x) {
    if (x != p[x]) p[x] = find(p[x]);
    return p[x];
}

void join(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx != fy) {
        p[fx] = fy;
    }
}

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void top_sort() {
    queue<int> q;
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            if (!du[find(tmp[i][j].t)] && find(tmp[i][j].t) == tmp[i][j].t) {
                q.push(find(tmp[i][j].t));
                mp[tmp[i][j].t] = 1;
            }
        }
    }

    while (!q.empty()) {
        int t = q.front();
        q.pop();

        for (int i = h[t]; i != -1; i = ne[i]) {
            int j = e[i];
            du[j] --;
            mp[j] = max(mp[j], mp[t] + 1);
            if (!du[j]) {
                q.push(j);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    init();        

    cin >> n >> m;

    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            int x;
            cin >> x;
            tmp[i].push_back({x, ++ t});
        }
    }

    for (int i = 0; i < n; i ++) {
        vector<node> res;
        for (int j = 0; j < m; j ++) res.push_back(tmp[i][j]);
        sort(res.begin(), res.end());
        for (int j = 1; j < m; j ++) {
            if (res[j].x == res[j - 1].x) {
                join(res[j].t, res[j - 1].t);
            } 
        }
    }

    for (int i = 0; i < m; i ++) {
        vector<node> res;
        for (int j = 0; j < n; j ++) res.push_back(tmp[j][i]);
        sort(res.begin(), res.end());
        for (int j = 1; j < n; j ++) {
            if (res[j].x == res[j - 1].x) {
                join(res[j].t, res[j - 1].t);
            } 
        }
    }

    for (int i = 0; i < n; i ++) {
        vector<node> res;
        for (int j = 0; j < m; j ++) res.push_back(tmp[i][j]);
        sort(res.begin(), res.end());
        for (int j = 1; j < m; j ++) {
            if (res[j].x != res[j - 1].x) {
                add(find(res[j - 1].t), find(res[j].t));
                du[find(res[j].t)] ++;
            }
        }
    }

    for (int i = 0; i < m; i ++) {
        vector<node> res;
        for (int j = 0; j < n; j ++) res.push_back(tmp[j][i]);
        sort(res.begin(), res.end());
        for (int j = 1; j < n; j ++) {
            if (res[j].x != res[j - 1].x) {
                add(find(res[j - 1].t), find(res[j].t));
                du[find(res[j].t)] ++;
            }
        }
    }

    top_sort();

    t = 0;
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            cout << mp[find(++ t)] << ' ';
        }
        cout << endl;
    }


    return 0;
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息