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"(队列函 ...
随机推荐
- ZBrush中Pinch捏挤笔刷介绍
随着版本的升级ZBrush®中给我们提供了越来越多的笔刷,对于这款软件来说,笔刷的使用是第一要素,也会一直伴随我们创作.虽然Zbrush中有那么多的笔刷,但是很多朋友会根据自己的习惯来使用,这个并不是 ...
- BZOJ2440: [中山市选2011]完全平方数 容斥原理_莫比乌斯函数
emmm....... 数学题都不友好QAQ...... Code: #include <cstdio> #include <algorithm> #include <c ...
- jq——动画
基本 1 show(可加时间)显示[在效果完成后可执行函数] 2 hide(可加时间)隐藏 3 toggle():切换效果 [在show和hide中切换] 有函数时 滑动动画 1 slideDown: ...
- socket&socketserver网络编程
1.套接字与套接模块 套接字是为特定网络协议(例如TCP/IP,ICMP/IP,UDP/IP等)套件对上的网络应用程序提供者提供当前可移植标准的对象.它们允许程序接受并进行连接,如发送和接受数据.为了 ...
- 使用yum方式安装mysql5.6
1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove ...
- Git学习总结(10)——git 常用命令汇总
1.git 基本概念: 工作区:改动(增删文件和内容) 暂存区:输入命令:git add 改动的文件名,此次改动就放到了'暂存区'(新增的文件) 本地仓库(简称:本地):输入命令:git commit ...
- Mysql学习总结(32)——MySQL分页技术详解
1.什么是数据分页:数据分页就是将很多条记录像书本一样分页,每页显示多少行记录: 2.为什么要数据分页:当我们进行sql语句查询时,假如数据有成千上万行记录,如果在同一个页面去显示,那这个页面得有多大 ...
- 一个简单搜索引擎的搭建过程(Solr+Nutch+Hadoop)
最近,因为未来工作的需要,我尝试安装部署了分布式爬虫系统Nutch,并配置了伪分布式的Hadoop来存储爬取的网页结果,用solr来对爬下来的网页进行搜索.我主要通过参考网上的相关资料进行安装部署的. ...
- jQuery调用WebService ( 同源调用)
转自原文 jQuery调用WebService 1.编写4种WebService方法 [WebService(Namespace = "http://tempuri.org/&quo ...
- 揭秘Product Hunt怎样运用邮件崛起
Product Hunt正被硅谷热议,而大家谈论Product Hunt时,经常涉及邮件营销. Product Hunt由Ryan Hoover 在2013年11月创立.鼓舞人们发现和分享新产品的站点 ...