Connected Components?

CodeForces - 920E

You are given an undirected graph consisting of n vertices and edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.

You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.

Input

The first line contains two integers n and m (1 ≤ n ≤ 200000, ).

Then m lines follow, each containing a pair of integers x and y (1 ≤ x, y ≤ n, x ≠ y) denoting that there is no edge between x and y. Each pair is listed at most once; (x, y) and (y, x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.

Output

Firstly print k — the number of connected components in this graph.

Then print k integers — the sizes of components. You should output these integers in non-descending order.

Example

Input

5 51 23 43 24 22 5

Output

21 4

思路:

bfs,同时用一个set记录哪些点还未确定是哪个联通块,再开一个set记录能走到哪些点。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 200000 + 10;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
set<int> st;
set<int> temp;
std::vector<int> v[maxn];
int ans[maxn];
int tot = 0;
int n, m;
bool vis[maxn];
queue<int> q;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout); du2(n, m);
repd(i, 1, m) {
int x, y;
du2(x, y);
v[x].push_back(y);
v[y].push_back(x);
}
repd(i,1,n)
{
st.insert(i);
}
repd(i, 1, n) {
if (!vis[i]) {
q.push(i);
tot++;
while (!q.empty()) {
int x = q.front();
q.pop();
if (vis[x]) {
continue;
}
ans[tot]++;
vis[x] = 1;
for (auto y : v[x]) {
if (!vis[y]) {
temp.insert(y);
}
}
for (auto y : temp) {
st.erase(y);
}
for (auto y : st) {
if (!vis[y]) {
q.push(y);
}
}
swap(temp, st);
temp.clear();
}
}
}
printf("%d\n", tot );
sort(ans + 1, ans + 1 + tot);
repd(i, 1, tot) {
printf("%d%c", ans[i], i == tot ? '\n' : ' ');
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Codeforces 920E-Connected Components? (set,补图,连通块)的更多相关文章

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

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

  2. [Codeforces 920E]Connected Components?

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

  3. 【BZOJ 1098】办公楼(补图连通块个数,Bfs)

    补图连通块个数这大概是一个套路吧,我之前没有见到过,想了好久都没有想出来QaQ 事实上这个做法本身就是一个朴素算法,但进行巧妙的实现,就可以分析出它的上界不会超过 $O(n + m)$. 接下来介绍一 ...

  4. Codeforces E - Connected Components?

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

  5. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  6. CodeForces 690D1 The Wall (easy) (判断连通块的数量)

    题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...

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

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

  8. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

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

随机推荐

  1. python-Web-flask-蓝图和单元测试

    4 蓝图和单元测试: 能够使用代码实现蓝图对项目进行模块化 admin=Blueprint('admin',__name__) # 创建一个蓝图对象 @admin.route('/') def adm ...

  2. 剑指offer 67. 字符串转换为整数(Leetcode 8. String to Integer (atoi))

    题目:剑指offer 67题 需要考虑的情况:空指针.nullptr.空字符串"".正负号.数值溢出.在写代码的时候对这些特殊的输入都定义好合理的输出.可以定义一个全局布尔型变量g ...

  3. axios二次封装的几种方法

    一.用Class方法 import axios from "axios"; declare var Promise: any; export class Request { sta ...

  4. elasticsearch基本概念理解+elasticsearch 的shards unassigned处理方法 -- 最佳运维实践 - 集群规划

    1.es与MySQL的概念对比 2.概念理解 2.1 Index : 一个索引即是文档的集合 2.2 Document : 一个文档即是一个可被索引的基础单元信息,一条记录: 2.3 Replicas ...

  5. mysql根据时间统计数据语句

    select FROM_UNIXTIME(`createtime`, '%Y年%m月%d日')as retm,count(*) as num  from `user` GROUP BY retm se ...

  6. navicat破解版的下载与激活

    原文链接:http://www.cnblogs.com/djwhome/p/9289295.html 以前一直使用的老版的破解版的navicat,但是最近老是报错 而且连接还特别慢,今天终于不忙了额, ...

  7. HTML的列表表格表单知识点

    无序列表格式                                                                                              ...

  8. [Agc029C]Lexicographic constraints_进制_二分答案_贪心

    Lexicographic constraints 题目链接:https://atcoder.jp/contests/agc029/tasks/agc029_c 数据范围:略. 题解: 二分是显然的, ...

  9. [转帖]万能数据库的使用【DbVisualizer软件,连接不同类型的数据库】

    万能数据库的使用[DbVisualizer软件,连接不同类型的数据库] https://www.cnblogs.com/FanSunny/p/4874572.html 自己就是用这个工具进行处理的. ...

  10. java知识随笔整理-标量函数和表值函数

    以sql server为例: 1.表值函数 用户定义表值函数返回 table 数据类型,表是单个 SELECT 语句的结果集. 示例代码CREATE FUNCTION Test_GetEmployee ...