Description
给你一张 \(n\) 个点的完全图,其中有 \(m\) 条边长度为 \(1\),其余全为 \(0\)。问你这张图的最小生成树为多少。
\(1\leq n\leq 100000,0 \leq m \leq \min\left(\frac{n(n-1)}{2},10^5\right)\)
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\n", ans-1);
return 0;
}
原文地址:https://www.cnblogs.com/NaVi-Awson/p/11823608.html
时间: 2024-10-12 17:59:54