[Codeforces 1242B]0-1 MST

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-08-05 18:56:32

[Codeforces 1242B]0-1 MST的相关文章

Codeforces Round #535 (Div. 3) 题解

Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉了. A. Two distinct points 题意: 给出两个区间的左右边界,输出两个数,满足两个数分别在两个区间内且这两个数不相等. 题解: 直接输出左端点然后判断一下就行了. 代码如下: #include <bits/stdc++.h> using namespace std; type

poj1789 MST 读题生涯永不停歇

题目: 链接在此 1.图论刷刷乐#1的第一题,无奈看了好长时间题目还是看不懂= =,明知是最水的题目 2.搜懂题目后,比较裸的MST,但还是决定写个题解,虽然没什么可说的,只是来警戒自己,还是要努力读题,YY大法,这也是水平的一个体现! #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include &

Codeforces Round #372 (Div. 1) B. Complete The Graph

题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S 到 T 的最短路恰好等于 L. 若没有输出 "NO",否则输出 "YES",同时输出新图中的所有边权值. 题目思路:二分+最短路(spfa or dijkstra) 闲谈:先%一发杜教,思路来源于看他的代码.然后蒟蒻spfa 982ms,杜教 dijkstra 93m

【codeforces #283(div 1)】ABC题解

A. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an n?×?m rectangular table consisting of lower case English letters. In one operation you can completely r

图论 --- MST

<传送门> [题目大意] SARS病毒蔓延,编号为0的人已经感染了SARS病毒,现在给你一些分组,只要和0接触过的都是可能感染的嫌疑人,问你最多可能有多少人感染了该种病毒. [题目分析] 简单的并查集,只要将所有一组的人都合并,最后来看一下和0一样的编号,统计一下输出就可. #include<iostream> using namespace std; int father[30001]; int find_father(int x) { if(father[x]!=x) retu

STP 4 - MST 和 PVST 对比 (侧重于MST)

802.1w = Rapid STP (uplinkFast & backbonefast 的功能被嵌入了,相当于自动生效) 802.1s = PVST/PVST+ (per VLAN spanning-tree) 两者都用VLAN来区分不同生成树进程 PVST中每个VLAN是一个进程 MST自定义进程 MST路径选择与PVST方法相同 ==== 相关命令与执行 更改MST根桥选择 手动更改BID优先级 - spanning-tree mst [instance id]priority  (lo

poj 1679 The Unique MST【次小生成树】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24034   Accepted: 8535 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

HUTACM2016 MST练习&#183;解题报告

专题链接 A - 还是畅通工程 题解: n个村,m条路,要用最少的钱把所有村连接起来,MST的模板题,提供两种算法模板. //使用Kruskal算法 #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; const int N = 105; int seed[N]; //构建并查集 int find_root(int x){ return seed[x] < 0?

【POJ1679】The Unique MST

1 //ver1: Kruskal+暴力枚举删除边 2 #include<iostream> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int t,n,m,first; 7 int a,ok,minx,p[101]; 8 struct edge{ 9 int v1,v2,d,v; //v标记MST中的每一条边 10 }e[10001]; 11 int findp(int x){ 1