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"(队列函 ...
随机推荐
- Generic programming-泛型编程
Generic programming is a style of computer programming in which algorithms are written in terms of t ...
- 路飞学城Python-Day32
36-进程池线程池 开多线程实现并发的效率是高的,当用户没有那么多的时候,服务器是可以承受压力的 但是一定要以某种方式来设置并发数,让服务器能够实现稳定的运行,控制服务器的线程数 设置池,往里面放池的 ...
- Windows Vista 安装和使用指导 - 停止支持后的几条建议
简介 曾经被广大网民吐槽的Windows Vista现在已经淡出了人们的视线,但仍有一些朋友想要体验一下这个操作系统.Windows Vista是Windows发展路线上的里程碑,相比之前的Windo ...
- ubuntu 12.04下安装Qt出现cannot execute binary file的解决方案
最近在ubuntu 12.04下安装QT的过程中,遇到一个问题. ./qt-opensource-linux-x64-5.7.0.run出现了bash: ./qt-opensource-linux-x ...
- css所有属性(table,行列组)总结
概述: CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: CSS声明总是以分号(;)结束,声明组以大括号({})括起来: 一.注释: CSS注释以 "/*" 开始, ...
- 【hdu 6319】Ascending Rating
[链接] 我是链接,点我呀:) [题意] 给你一个长为n的数组a 让你对于每个长度为m的窗口. 算出其中的最大值以及从左往右遍历的时候这个最大值更新的次数. [题解] 单调队列. 从后往前滑动窗口. ...
- 高级聚合函数rollup(),cube(),grouping sets()
rollup(),cube(),grouping sets() 上面这几个函数,是对group by分组功能做的功能扩展. a.rollup() 功能:在原结果基础上追加一行总合计记录 ...
- C#-基础部分思维导图
C#-基础部分思维导图 链接:http://pan.baidu.com/s/1jHNgS78 密码:3i74 如果有错我,请告诉我,传播错误的知识是违法的.
- AIX查看某个端口被哪个进程占用
AIX查看某个端口被哪个进程占用 学习了:https://zhidao.baidu.com/question/1928716757722021467.html 1. netstat -Aan|grep ...
- webstorm卡顿问题处理
webstorm卡顿问题处理 学习了:http://blog.csdn.net/qq673318522/article/details/50583831 找到WebStorm.exe.vmoption ...