并查集+巧妙分块,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;
}
相关推荐
52Hz11838 分钟前
二叉树理论、力扣94.二叉树的中序遍历、104.二叉树的最大深度、226.反转二叉树、101.对称二叉树
python·算法·leetcode
王德博客40 分钟前
【C++继承】笔试易错题目
开发语言·c++·继承
Shirley~~44 分钟前
leetcode移除元素
javascript·数据结构·算法
普贤莲花1 小时前
新生2026年1月20日---星期二(大寒)
程序人生·算法·leetcode
高洁011 小时前
产品数字孪生体与数字样机及数字化交付的应用
人工智能·深度学习·算法·数据挖掘·transformer
2501_941507941 小时前
通信基站天线设备检测与分类YOLO11-LSCD-LQE算法实现与优化
算法·分类·数据挖掘
wen__xvn1 小时前
基础数据结构第08天:栈(实战篇)
数据结构·c++·算法
玄鱼殇1 小时前
前端排序算法
算法·排序算法
tqs_123451 小时前
倒排索引数据结构
java·前端·算法
a程序小傲1 小时前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先