题目链接: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? 广度搜索 双向链表 判断联通 大量重复节点的删除的更多相关文章

  1. [Codeforces 920E]Connected Components?

    Description 题库链接 给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小. \(1\leq n,m\leq 200000\) Solution 参考 ...

  2. Codeforces 920E Connected Components? 补图连通块个数

    题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...

  3. Codeforces E - Connected Components?

    E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...

  4. CodeForces 292D Connected Components (并查集+YY)

    很有意思的一道并查集  题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...

  5. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  7. 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 ...

  8. 八数码问题:C++广度搜索实现

    毕竟新手上路23333,有谬误还请指正. 课程设计遇到八数码问题(这也是一坨),也查过一些资料并不喜欢用类函数写感觉这样规模小些的问题没有必要,一开始用深度搜索却发现深搜会陷入无底洞,如果设定了深度限 ...

  9. 记录----第一次使用BFS(广度搜索)学习经验总结

    学习经验记录与分享—— 最近在学习中接触到了一种解决最短路径的实用方法----BFS(广度搜索),在这里总结并分享一下第一次学习的经验. 首先第一个要了解的是"queue"(队列函 ...

随机推荐

  1. 页面定制CSS代码初探(四):cnblogs使用Github引用样式

    前言 对于用惯了Github的人来说,眼里的引用应该是这样的 "Talk is cheap. Show me the code" -- Linus Torvalds 然而实际上cn ...

  2. Eclipse中使用GIT将已提交到本地的文件上传至远程仓库

    GIT将已提交到本地的文件上传至远程仓库: 1.  右击项目——Team——Push to Upstream,即可将已保存在本地的文件上传推至GIT远程仓库.

  3. gcp – 源于CP的高级命令行文件拷贝工具

    作者:linux 出处:http://linux.cn/thread/11868/1/1/ gcp – 源于CP的高级命令行文件拷贝工具 几周前,我们讨论了高级拷贝(修改于cp命令,让其可以显示复制进 ...

  4. js判断 nan null undefined的方法

    收集资料如下判断: 1.判断undefined: 复制代码代码如下: <span style="font-size: small;">var tmp = undefin ...

  5. ASP.NET-GUID扩展类使用

    在NUGET上有一个GUID的类,安装试用一下它的方法 将string转为guid对象 Guid ad = new Guid("{99009327-15D2-4A69-B015-BEAC11 ...

  6. jquery访问ashx文件示例

    转自原文jquery访问ashx文件示例 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件 ...

  7. 《JAVA程序设计》实训第二天——《猜猜看》游戏

    课程实训第二天,我在第一天的基础上去导入目录,第一天那时候一直改动都是改动不到,上网找了相关的知识.问了同学该怎么去导入显示图片. public class weiwei extends JFrame ...

  8. 37、ifconfig命令

    很多windows很熟悉ipconfig命令行工具.它被用来获取网络接口配置信息并对此进行改动.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config). 通常须 ...

  9. node08---EJS模版

    四.模板引擎 <a href="<%= url %>"><img src="<%= imageURL %>" alt= ...

  10. hdoj--1950--Bridging signals(二分查找+LIS)

    Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...