bzoj4883 [Lydsy1705月赛] guards on board Minimum Spanning Tree Forest ring

Topic Portal

https://lydsy.com/JudgeOnline/problem.php?id=4883

answer

Each row and each column must be covered.

Consider for each row and each column to establish a point, and even between the row corresponding to one side is the right point coordinates.

In this way, each point must have a side to indicate that the row / column selection this side.

Each point must have a side of the base ring is a forest tree.

Therefore, with the smallest ring kruskal maintain direct forest trees.

The maintenance method is probably disjoint-set time and then maintains a communication information indicating that memory block is not present the ring. You can contribute an edge, the first time a communication is not communication in two blocks and two blocks do not have the communication ring; the second time is two communication endpoints in a block, and this block is not the communication loop.


Time complexity \ (O (nm \ log nm) \) .

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const int N = 100000 + 7;

int n, m, tot;
int fa[N], hs[N];

struct Edges { int x, y, w; } e[N];
inline bool operator < (const Edges &a, const Edges &b) { return a.w < b.w; }

inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

inline void work() {
    std::sort(e + 1, e + n * m + 1);
    ll ans = 0;
    for (int i = 1; i <= n + m; ++i) fa[i] = i;
    for (int i = 1; i <= n * m; ++i) {
        int x = find(e[i].x), y = find(e[i].y), z = e[i].w;
//      dbg("z = %d\n", z);
        if (x == y) {
            if (!hs[x]) hs[x] = 1, ans += z;//, dbg("****** %d %d %d\n", e[i].x, e[i].y, z);
            continue;
        }
        if (hs[x] && hs[y]) continue;
        ans += z, fa[y] = x, hs[x] |= hs[y];//, dbg("****** %d %d %d\n", e[i].x, e[i].y, z);
    }
    printf("%lld\n", ans);
}

inline void init() {
    read(n), read(m);
    int v;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            read(v);
            e[++tot] = (Edges){ i, j + n, v };
        }
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj4883.html