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. [学习笔记] 模拟退火 (Simulated Annealing)

    真没想到这东西真的在考场上用到了...顺便水篇blog以示诈尸好了(逃 模拟退火算法 模拟退火是一种随机化算法, 用于求函数的极值qwq 比如给出一个问题, 我们要求最优解的值, 但是可能的方案数量极 ...

  2. Mysql性能优化之覆盖索引

    因为我们大多数情况下使用的都是Innodb,所以这篇博客主要依据Innodb来讲 b+树(图片来自网络) b+树图来自网络 1.聚集索引与非聚集索引区别 聚集索引:叶子节点包含完整的数据(物理地址连续 ...

  3. (译文)学习ES6非常棒的特性——Async / Await函数

    try/catch 在使用Async/Await前,我们可能这样写: const main = (paramsA, paramsB, paramsC, done) => { funcA(para ...

  4. hibernate框架学习笔记9:多对多关系案例

    员工与角色案例: 一个员工可以是多种角色(总监,经理),一种角色可以是多个员工(保洁) 这里发现无法使用外键表达关系,多对多总是创建第三张表来维护关系 这张表至少两列,都是外键,分别引用两张表的主键 ...

  5. springboot elasticsearch 集成注意事项

    文章来源: http://www.cnblogs.com/guozp/p/8686904.html 一 elasticsearch基础 这里假设各位已经简单了解过elasticsearch,并不对es ...

  6. oracle数据库修改连接数

    最近在用weblogic部署项目,同时用的是oracle数据库,然后今天遇到一个问题:多个用户连接数据库连接不成功,有时提示被锁住,经检查发现一方面weblogic控制台中数据源的连接池配置没有配置足 ...

  7. 阿尔法冲刺——Postmortem会议

    设想与目标 1.我们软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这个问题,我们觉得我们的软件目标还是比较明确的,在SRS中也给出了典型用户和典型场景的清晰的描述. 2 ...

  8. mobiscroll2.5.4 日期组件

    <script type="text/javascript"> function setCss(o) { $('input:jqmData(role="dat ...

  9. 第二次作业:APP案例分析

    App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...

  10. raid5 / raid5e / raid5ee的性能对比及其数据恢复原理

    RAID 5 是一种存储性能.数据安全和存储成本兼顾的存储解决方案. RAID 5可以理解为是RAID 0和RAID 1的折中方案.RAID 5可以为系统提供数据安全保障,但保障程度要比Mirror低 ...