[Codeforces 920E]Connected Components?
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?的更多相关文章
- Codeforces 920E Connected Components? 补图连通块个数
题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- CodeForces 292D Connected Components (并查集+YY)
很有意思的一道并查集 题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- 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 ...
- [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), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
随机推荐
- [日常] AtCoder Beginner Contest 075 翻车实录
别问我为啥要写一篇ABC的游记... 周日打算CF开黑于是就打算先打打ABC找回手速... 进场秒掉 $A$ 和 $B$ , 小暴力一脸偷税 然后开 $C$ ...woc求桥? 怎么办啊我好像突然忘了 ...
- IT & ME
第一部分:结缘计算机 填报志愿的那天晚上,老爸老妈和我一起在房间里讨论专业选择的事情.因为我性格比较内敛,家人建议我去学医.而我又对学医一点也不感冒,再加上自己高中时期一直喜欢玩游戏,于是最后就填报了 ...
- 获取android项目的数据库地址或者数据库名
你不需要知道该路径.只是使用数据库,你可以将它们删除的列表. for (String databaseName : context.databaseList()) { context.deleteDa ...
- 项目Beta冲刺Day6
项目进展 李明皇 今天解决的进度 进行前后端联动调试 明天安排 完善程序运行逻辑 林翔 今天解决的进度 服务器端发布消息,删除消息,检索消息,个人发布的action 明天安排 图片功能遇到问题,微信小 ...
- logging日志
import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...
- 算法第四版 coursera公开课 普林斯顿算法 ⅠⅡ部分 Robert Sedgewick主讲《Algorithms》
这是我在网上找到的资源,下载之后上传到我的百度网盘了. 包含两部分:1:算法视频的种子 2:字幕 下载之后,请用迅雷播放器打开,因为迅雷可以直接在线搜索字幕. 如果以下链接失效,请在下边留言,我再更新 ...
- 在linux中关闭防火墙
1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...
- 【基础知识】Flex-弹性布局原来如此简单!!
简言 布局的传统解决方案是基于盒状模型,依赖 display + position + float 方式来实现,灵活性较差.2009年,W3C提出了一种新的方案-Flex,Flex是Flexible ...
- python hashlib、hmac模块
一.hashlib模块 import hashlib m = hashlib.md5() m.update(b"Hello") print(m.hexdigest()) m.upd ...
- 阿里云API网关(11)API的三种安全认证方式
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...