CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
题目链接:https://cn.vjudge.net/problem/CodeForces-920E
题意
给一个补图,问各个联通块有几个元素,升序排列
注意maxn=2e5, maxm=2e10
思路
数据量超大,这本来是并查集专题的一道题
如果用并查集的话,向上维护一个元素个数,但首先离线建图是个问题O(n^2)
这样考虑的话,bfs O(n)就是更好的选择
提交上去TLE,当时写题没仔细算复杂度,set查边+数组判重,加起来貌似O(nlogn+n),至于为什么用set查边,因为数组查边肯定空间太大
最后查了查题解,判重是链表删除元素,相当于真正的删除了,循环次数大大降低了
好么,厉害
提交过程
| CE | 头文件 |
| TLE1 | set<pair<int, int>>存边 |
| TLE2 | 改成set存边 |
| WA1-3 | 忘了为啥WA了... |
| AC |
代码
#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=2e5+20;
int n, m, cnt, size[maxn], next[maxn], prev[maxn];
bool check[maxn];
set<long long> vis;
void del(int x){
next[prev[x]]=next[x];
prev[next[x]]=prev[x];
}
int bfs(int x){
int ans=1;
queue<int> que; que.push(x);
check[x]=true; del(x);
while (que.size()){
int node=que.front(); que.pop();
for (int i=next[0]; i<=n; i=next[i]) if (!check[i] && !vis.count((long long)i*maxn+node)){
que.push(i);
check[i]=true; del(i);
ans++;
}
}return ans;
}
int main(void){
int to, from;
scanf("%d%d", &n, &m);
for (int i=0; i<m; i++){
scanf("%d%d", &to, &from);
vis.insert((long long)to*maxn+from);
vis.insert((long long)from*maxn+to);
}
for (int i=1; i<=n; i++) next[i]=i+1, prev[i]=i-1;
next[0]=1; prev[n+1]=n;
int cnt=0;
for (int i=1; i<=n; i++) if (!check[i])
size[cnt++]=bfs(i);
sort(size, size+cnt);
printf("%d\n", cnt);
for (int i=0; i<cnt; i++) printf("%d%c", size[i], " \n"[i==cnt-1]);
return 0;
}
| Time | Memory | Length | Lang | Submitted |
|---|---|---|---|---|
| 343ms | 16012kB | 1067 | GNU G++ 5.1.0 | 2018-07-23 17:51:00 |
CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除的更多相关文章
- [Codeforces 920E]Connected Components?
Description 题库链接 给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小. \(1\leq n,m\leq 200000\) Solution 参考 ...
- 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 ...
- 八数码问题:C++广度搜索实现
毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...
- 记录----第一次使用BFS(广度搜索)学习经验总结
学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...
随机推荐
- day06-2 基本运算符(解压缩)
目录 运算符 算数运算符 比较运算符 赋值运算符 逻辑运算符 运算规则 成员运算符 身份运算符 Python运算符优先级 链式赋值(必考) 交叉赋值(必考) 解压缩(必考) 运算符 算数运算符 进行算 ...
- 03 Winform基础
补充: MD5加密 static void Main(string[] args) { string s = GetMD5("123"); Console.WriteLine(s) ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- 查看系统进程:ps、top
1.ps命令:提供最近进程的快照.显示当前活跃进程的简要信息. 常见使用: (1)与grep命令配合查找是否有相应进程存活 ps -ef | grep ksmd ps -Af | grep ksmd ...
- Spring配置文件中指定init-method属性的作用
bean 配置文件属性 init-method 用于在bean初始化时指定执行方法,用来替代继承 InitializingBean接口.相关链接:https://www.cnblogs.com/Joe ...
- 记一次BootStrap的使用
效果图如下: 一.简介: 什么是Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架. 什么是响应式布局? 引用一句Bootstrap的标题语 “Boots ...
- MyBatis学习总结(1)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- TCP的可靠传输(依赖流量控制、拥塞控制、连续ARQ)
TCP可靠性表现在它向应用层提供的数据是无差错,有序,无丢失,即递交的和发送的数据是一样的. 可靠性依赖于流量控制.拥塞控制.连续ARQ等技术 <TCP/IP详解>中的“分组”是不是就是报 ...
- 2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations
ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)
树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...