并查集+巧妙分块,Codeforces1424B. 0-1 MST

目录

一、题目

1、题目描述

2、输入输出

2.1输入

2.2输出

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解


一、题目

1、题目描述

Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph.

It is an undirected weighted graph on n vertices. It is a complete graph: each pair of vertices is connected by an edge. The weight of each edge is either 0 or 1; exactly mm edges have weight 1, and all others have weight 0.

Since Ujan doesn't really want to organize his notes, he decided to find the weight of the minimum spanning tree of the graph. (The weight of a spanning tree is the sum of all its edges.) Can you find the answer for Ujan so he stops procrastinating?

2、输入输出

2.1输入

The first line of the input contains two integers n and m (1≤n≤10^5, 0≤m≤min(n*(n−1)/2,10^5)), the number of vertices and the number of edges of weight 1 in the graph.

The i-th of the next m lines contains two integers ai and bi (1≤ai,bi≤n, ai≠bi), the endpoints of the i-th edge of weight 1.

It is guaranteed that no edge appears twice in the input.

2.2输出

Output a single integer, the weight of the minimum spanning tree of the graph.

3、原题链接

Problem - 1242B - Codeforces (Unofficial mirror by Menci)


二、解题报告

1、思路分析

思来想去只能想到O(n^2)解法,看到大佬一句话点破梦中人了属于是orz。

朴素思想:直接跑生成树------MLE

进一步:无权边连通块数目-1即为答案------如果只能想出O(n^2)解法会TLE

一个特别妙的思路:因为一共有m条带权边,那么只考虑带权边的情况下所有节点的出度入度之和为2*m,那么假如最小度为dmin,那么度为dmin的点不会超过2*m/n!!!(这个不难想

精彩的来了:我们先拿到一个最小度的点,O(n)求出它所在的无权边连通块,那么剩下的点中的无权边连通块的数目即为答案

那么如何求剩下点的无权连通块呢?

剩下的点不超过2*m/n个,我们对于每个点都枚举1~n的所有点,如果两个点之间没有边就合并

这一步骤时间复杂度为O(2*m/n * n) = O(m)!!!

所以我们在O(N+M)的时间内就解决了问题

最坏情况下,如果最小度的点特别多,我们效率仍然是线性的(可以结合n,m的数据范围想一下

2、复杂度

时间复杂度:O(n+m) 空间复杂度:O(m)

3、代码详解

复制代码
cpp 复制代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100005;
const int M = 505;
int n, m, cnt;
bool g[M][N];
struct edge
{
    int u, v;
} edges[N];
int deg[N], minid = 0;
int p[N];
int seq[N], tot = 0, pos[N];
int findp(int x)
{
    return p[x] < 0 ? x : p[x] = findp(p[x]);
}
bool Union(int x, int y)
{
    int px = findp(x), py = findp(y);
    if (px == py)
        return false;
    if (p[px] > p[py])
        swap(px, py);
    p[px] += p[py], p[py] = px;
    return true;
}
bool vis[N];
int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    // freopen("in.txt", "r", stdin);
    cin >> n >> m, memset(p, -1, sizeof(p));
    for (int i = 1, u, v; i <= m; ++i)
        cin >> u >> v, edges[i] = {u, v}, ++deg[u], ++deg[v];

    minid = 1;
    for (int i = 2; i <= n; ++i)
        if (deg[i] < deg[minid])
            minid = i;

    for (int i = 1; i <= m; ++i)
    {
        if (edges[i].u == minid)
            vis[edges[i].v] = true;
        if (edges[i].v == minid)
            vis[edges[i].u] = true;
    }
    for (int i = 1; i <= n; ++i)
        if (vis[i])
            seq[++tot] = i, pos[i] = tot;
        else
            Union(i, minid);

    for (int i = 1; i <= m; ++i)
    {
        if (vis[edges[i].u])
            g[pos[edges[i].u]][edges[i].v] = true;
        if (vis[edges[i].v])
            g[pos[edges[i].v]][edges[i].u] = true;
    }
    for (int i = 1; i <= tot; ++i)
        for (int j = 1, u = seq[i]; j <= n; ++j)
            if (g[i][j])
                continue;
            else
                cnt += Union(u, j);

    cout << tot - cnt;
    return 0;
}
相关推荐
Mercury_Lc11 分钟前
【贪心 或 DFS - 面试题】小于n最大数
数据结构·c++·算法
凤年徐14 分钟前
【数据结构】LeetCode160.相交链表 138.随即链表复制 牛客——链表回文问题
c语言·数据结构·c++·算法·leetcode·链表
qq_124987075323 分钟前
基于改进蜂群优化算法的高频金融波动率预测系统 (源码+论文+部署+安装)
python·算法·金融·毕业设计·蜂群优化算法
艾莉丝努力练剑30 分钟前
【C语言16天强化训练】从基础入门到进阶:Day 14
java·c语言·学习·算法
羑悻的小杀马特32 分钟前
【C++高并发内存池篇】ThreadCache 极速引擎:C++ 高并发内存池的纳秒级无锁革命!
开发语言·c++·多线程·高性能内存池
元亓亓亓1 小时前
LeetCode热题100--98. 验证二叉搜索树--中等
算法·leetcode·职场和发展
程序员小富1 小时前
令牌桶VS漏桶:谁才是流量控制的“最优解”?
java·后端·算法
指针刺客2 小时前
嵌入式筑基之设计模式
开发语言·c++·设计模式
fsnine2 小时前
机器学习回顾(二)——KNN算法
人工智能·算法·机器学习
aiwery2 小时前
算法题——找到字符串中所有字母异位词
javascript·算法