【Codeforces 1242B] 0-1 MST

説明

試験のリンク

あなたを与える\(N- \)完全グラフの点、\(M \)の長さの辺\(1 \) 残り(0 \)\私はあなたに、スパニングツリーをどのくらいの最低限のこの絵をお願いします。

\(1 \当量のn \当量100000,0 \当量M \当量\分\左(\ FRAC {N(N-1)} {2} ^ 5 10 \右)\)

解決

簡単に見つけ、答えは図の通信のブロック数を作ることです\( - 1 \) コピーボードは愛さ...

参照の解析[Codeforces 920E]連結成分?

コード

#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\n", ans-1);
    return 0;   
}

おすすめ

転載: www.cnblogs.com/NaVi-Awson/p/11823608.html