Description

题库链接

给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小。

\(1\leq n,m\leq 200000\)

Solution

参考了 ww140142 的做法。题解也转自该博客。

每次枚举一个未处理过的点,然后从它开始宽搜出它所在的连通块;

具体是枚举它的所有原图的边,标记起来,枚举边之后再枚举所有的点,将未标记的点加入该连通块,并加入队列继续宽搜;

为了节约无用的枚举,我们还需要对所有点构建链表,将已经在某个块内的点删除;

这个算法的复杂度是 \(O(n+m)\) 的;

原因是每一个点仅进行了一次宽搜的拓展;

并且在每次拓展中,枚举边表总复杂度是 \(O(m)\) ;

而之后的枚举剩下的点,我们将点分为两部分:已标记的点的复杂度计在 \(O(m)\) 之内,而未标记的点将会被加入队列,这个过程对每个点也仅有一次。

综上复杂度为 \(O(n+m)\) 。

Code

#include <bits/stdc++.h>
using namespace std;
const int N = 200000; int n, m, u, v;
vector<int>to[N+5];
queue<int>Q;
int lst[N+5], nxt[N+5], ans[N+5], cnt;
int vis[N+5], undo[N+5]; void delet(int x) {nxt[lst[x]] = nxt[x], lst[nxt[x]] = lst[x]; }
void work() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
scanf("%d%d", &u, &v), to[u].push_back(v), to[v].push_back(u);
for (int i = 1; i < n; i++) nxt[i] = i+1, lst[i+1] = i;
nxt[0] = 1;
for (int i = 1; i <= n; i++)
if (vis[i] == 0) {
ans[++cnt] = 1;
vis[i] = 1, Q.push(i); delet(i);
while (!Q.empty()) {
u = Q.front(); Q.pop();
for (int j = 0, sz = to[u].size(); j < sz; j++)
if (vis[to[u][j]] == 0) undo[to[u][j]] = 1;
for (int j = nxt[0]; j; j = nxt[j])
if (undo[j] == 0) {vis[j] = 1, ++ans[cnt]; delet(j); Q.push(j); }
else undo[j] = 0;
}
}
sort(ans+1, ans+cnt+1); printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
}
int main() {work(); return 0; }

[Codeforces 920E]Connected Components?的更多相关文章

  1. Codeforces 920E Connected Components? 补图连通块个数

    题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...

  2. Codeforces E - Connected Components?

    E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...

  3. CodeForces 292D Connected Components (并查集+YY)

    很有意思的一道并查集  题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...

  4. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  6. Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论

    E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...

  7. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  8. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  9. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

随机推荐

  1. JavaScript(第九天)【正则表达式】

    假设用户需要在HTML表单中填写姓名.地址.出生日期等.那么在将表单提交到服务器进一步处理前,JavaScript程序会检查表单以确认用户确实输入了信息并且这些信息是符合要求的.   一.什么是正则表 ...

  2. c语言——第0次作业

    1.你认为大学的学习生活.同学关系.师生应该是怎样?请一个个展开描写 大学生活:大学生活充满着挑战,首先当然必须先掌握自己所学的专业知识,然后就要学会独立,可以处理好人际关系,并且要有更强的自我约束能 ...

  3. Linux下ftp和ssh详解

    学习了几天Linux下ftp和ssh的搭建和使用,故记录一下.学习ftp和ssh的主要目的是为了连接远程主机,并且进行文件传输.废话不多说,直接开讲! ftp服务器 1. 环境搭建 本人的系统是Arc ...

  4. Beta冲刺Day2

    项目进展 李明皇 今天解决的进度 优化了信息详情页的布局:日期显示,添加举报按钮等 优化了程序的数据传递逻辑 明天安排 程序运行逻辑的完善 林翔 今天解决的进度 实现微信端消息发布的插入数据库 明天安 ...

  5. TOTP算法 基于时间的一次性密码

    /** Copyright (c) 2011 IETF Trust and the persons identified as authors of the code. All rights rese ...

  6. 【作业】HansBug的前三次OO作业分析与小结

    OO课程目前已经进行了三次的作业,容我在本文中做一点微小的工作. 第一次作业 第一次作业由于难度不大,所以笔者程序实际上写的也比较随意一些.(点击就送指导书~) 类图 程序的大致结构如下: 代码分析 ...

  7. js定时刷新页面.

    //页面定时刷新.2017.09.27 $(document).ready(function () { self.setInterval(function () { var d = new Date( ...

  8. js判断IE浏览器版本(IE8及以下)

    var DEFAULT_VERSION = 8.0; var ua = navigator.userAgent.toLowerCase(); var isIE = ua.indexOf("m ...

  9. Oracle 使用pl/sql将表中的数据读出到文件中

    (1)在服务器上创建文件路径及文件 [oracle@redhat errormsg]$ touch test01.txt (2)在数据库中创建路径及授权 (3)创建存储过程 CREATE OR REP ...

  10. python 单例模式的四种创建方式

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...