Connected Components? CodeForces - 920E (bfs)
大意:给定无向图, 求补图的连通块数
bfs模拟即可, 用了map存图, set维护未划分的点集, 复杂度$O(nlog^2n)$, 用链表的话可以$O(n)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define pb push_back using namespace std; const int N = 4e5+, INF = 0x3f3f3f3f;
int n, m;
map<int,int> a[N];
set<int> s;
int sz[N]; int main() {
scanf("%d%d", &n, &m);
REP(i,,m) {
int u, v;
scanf("%d%d", &u, &v);
a[u][v]=a[v][u]=;
}
REP(i,,n) s.insert(i);
REP(i,,n) if (s.count(i)) {
queue<int> q;
q.push(i);
int cnt = ;
while (!q.empty()) {
int i=q.front();q.pop();
vector<int> tmp;
for (int j:s) if (!a[i][j]) {
++cnt, q.push(j), tmp.pb(j);
}
for (int j:tmp) s.erase(j);
}
sz[++*sz] = cnt;
}
printf("%d\n", *sz);
sort(sz+,sz++*sz);
REP(i,,*sz) printf("%d ",sz[i]);
puts("");
}
Connected Components? CodeForces - 920E (bfs)的更多相关文章
- Connected Components? Codeforces - 920E || 洛谷 P3452 &&bzoj1098 [POI2007]BIU-Offices
https://codeforces.com/contest/920/problem/E https://www.luogu.org/problemnew/show/P3452 https://www ...
- 线段树 C - Connected Components? CodeForces - 920E
这个题目居然可以用线段树写,好震惊,如果不是在线段树专题肯定想不到,但是就算在线段树的专题里面,我也不太会怎么写. 这个题目大意是,给你n m n代表n个点,m代表m条边,然后就是m行,每行两个数字, ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- 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 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Cyclic Components CodeForces - 977E(DFS)
Cyclic Components CodeForces - 977E You are given an undirected graph consisting of nn vertices and ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
随机推荐
- 数据仓库基础(二)ETL
本文转载自:http://www.cnblogs.com/evencao/archive/2013/06/14/3135529.html ETL在数据仓库中具有以下的几个特点: 数据流动具有周期性: ...
- 数据仓库原理<4>:联机分析处理(OLAP)
本文转载自:http://www.cnblogs.com/hbsygfz/p/4762085.html 1. 引言 本篇主要介绍数据仓库中的一项重要分析技术——联系分析处理(OLAP). 在第一篇笔者 ...
- 自动化持续集成Jenkins
自动化持续集成Jenkins 使用Jenkins配置自动化构建http://blog.csdn.net/littlechang/article/details/8642149 Jenkins入门总结h ...
- javascript中的console.log有什么作用?
javascript中的console.log有什么作用? 主要是方便你调式javascript用的.你可以看到你在页面中输出的内容. 相比alert他的优点是:他能看到结构话的东西,如果是alert ...
- 分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace(转)
源:分享三个USB抓包软件---Bus Hound,USBlyzer 和-USBTrace Bus Hound官方下载地址:http://perisoft.net/bushound/Bus Hound ...
- 基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理
<基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...
- Python3基础 str partition 以参数字符串切分字符串,只切分为三部分
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Java8新特性:Function接口和Lambda表达式参考
Lambda基本:https://blog.csdn.net/wargon/article/details/80656575 https://www.cnblogs.com/hyyq/p/742566 ...
- vue.js组件(component)
简介: 组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面 ...
- C#学习笔记(十二):构造函数、属性和静态类
面向对象 简写重载的方法:重载中如果逻辑重复的情况下,用参数少的调用参数多 参数空缺,可以用null填补 using System; using System.Collections.Generic; ...