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. Android中的layout_gravity和gravity的区别

    在Android的布局中,除了padding和margin容易弄混之外,还有layout_gravity和gravity.按照字面意思来说,layout_gravity就是相对于layout来设置的. ...

  2. MyGod_alpha版本测试报告

    买尬-Alpha版本测试报告 @(二手市场APP)[MyGod团队|团队项目|版本测试] 项目名称:武汉大学校园二手市场APP--买尬 软件版本:1.0.0 开发团队:MyGod 开发代表:程环宇 张 ...

  3. android 广播,manifest.xml注册,代码编写

    1.种 private void downloadBr(File file) {   // 广播出去,由广播接收器来处理下载完成的文件   Intent sendIntent = new Intent ...

  4. 小草手把手教你 LabVIEW 串口仪器控制——初识VISA串口

    有些人,学习一样东西时候,喜欢现成的例子.很多人学习一门技术,都喜欢现成的例子开始,比如学单片机的啊,最开始都是修改的例子吧,学语言的也是.最开始都是模仿.这个年头看书上的理论知识太浪费时间了.所以啊 ...

  5. HAOI 2012 高速公路

    https://www.luogu.org/problem/show?pid=2221 题目描述 Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这 ...

  6. 1-51单片机WIFI学习(开发板介绍)

    源码链接都在后面 前面的都是介绍单独的WIFI,没有和单片机结合起来,因为做项目很少会只用WIFI模块.大多数都是WIFI模块作为中转数据的桥梁,单片机负责 数据采集,控制等等,所以自己准备出一套51 ...

  7. MQTT和paho(一)

    参考链接:http://blog.csdn.net/yangzl2008/article/details/8861069 一.mqtt 1.简单介绍 http://mqtt.org/software ...

  8. 阿里云API网关(15)监控预警

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  9. 我做的python常用的小技巧

    在python编码过程中,总会遇到各种各样的小问题,我想着记录下来,以备查用,总结过去,是为了更好的思考与进步. 一. 去除变量中(标题中)多余的字符 数据处理过程中,遇到这样的情况: y=['月份' ...

  10. SourceTree 01 - git 客户端介绍

    SourceTree - git客户端介绍 SourceTree系列第1篇 --->> SourceTree 01 - git 客户端介绍(http://www.cnblogs.com/g ...