[Codeforces 1242B]0-1 MST

Posted navi-awson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces 1242B]0-1 MST相关的知识,希望对你有一定的参考价值。

Description

题库链接

给你一张 (n) 个点的完全图,其中有 (m) 条边长度为 (1),其余全为 (0)。问你这张图的最小生成树为多少。

(1leq nleq 100000,0 leq m leq minleft(frac{n(n-1)}{2},10^5 ight))

Solution

容易发现,答案就是补图连通块个数 (-1)。喜闻乐见的抄板子了...

解析详见[Codeforces 920E]Connected Components?

Code

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 100000+5;

int n, m, u, v, vis[N], undo[N], ans, lst[N], nxt[N];
vector<int> to[N];
queue<int> Q;

void delet(int x) {nxt[lst[x]] = nxt[x], lst[nxt[x]] = lst[x]; }
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &u, &v), to[u].pb(v), to[v].pb(u);
    nxt[0] = 1;
    for (int i = 1; i < n; i++) lst[i+1] = i, nxt[i] = i+1;
    for (int i = 1; i <= n; i++)
        if (!vis[i]) {
            ++ans; Q.push(i); vis[i] = 1; delet(i);
            while (!Q.empty()) {
                int u = Q.front(); Q.pop();
                for (auto v : to[u])
                    if (!vis[v]) undo[v] = 1;
                for (int j = nxt[0]; j; j = nxt[j])
                    if (undo[j] == 0) Q.push(j), vis[j] = 1, delet(j);
                    else undo[j] = 0;
            }
        }
    printf("%d
", ans-1);
    return 0;   
}

以上是关于[Codeforces 1242B]0-1 MST的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

codeforces 19/11/06 div2D. 0-1 MST

Codeforces 1108F MST Unification MST + LCA

Codeforces Round #535 F-MST Unification

Codeforces.888G.Xor-MST(Bor?vka算法求MST 贪心 Trie)

Codeforces 160 D. Edges in MST