## 小坏蛋_千千

I was caught in a heavy rain!

# Codeforces 920 E. Connected Components? （bfs）

## Description

You are given an undirected graph consisting of n vertices and $\frac{n\left(n-1\right)}{2}-m$$\frac{n(n-1)}{2}-m$ 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, $0\le m\le min\left(\frac{n\left(n-1\right)}{2},200000\right)$$0≤m≤\min(\frac{n(n-1)}{2},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 5
1 2
3 4
3 2
4 2
2 5


## Example output

2
1 4


## AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
using namespace std;
const int maxn = 2e5+10;

int n,m;
unordered_map<int,bool> mp[maxn];
list<int> ret;

void init()
{
for(int i=1; i<=n; i++)
ret.push_back(i);
}

int bfs(int x)
{
int cnt = 0;
queue<int> sk;
sk.push(x);
while(!sk.empty())
{
int p = sk.front();
sk.pop();
++cnt;
for(auto s = ret.begin(); s!=ret.end();)
{
if(!mp[p].count(*s))
{
sk.push(*s);
ret.erase(s++);
}
else
s++;
}
}
return cnt;
}

void solve()
{
vector<int> ans;
while(!ret.empty())
{
int x = ret.front();
ret.pop_front();
ans.push_back(bfs(x));
}
sort(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(auto s:ans)
cout<<s<<" ";
cout<<endl;
}

int main()
{
IO;
cin>>n>>m;
init();
for(int i=0; i<m; i++)
{
int u,v;
cin>>u>>v;
mp[u][v] = true;
mp[v][u] = true;
}
solve();
return 0;
}

#### Codeforces - 920E - Connected Components?

2018-02-03 13:58:56

#### E. Connected Components （BFS ）

2018-05-10 09:34:47

#### [leetcode323]Number of Connected Components in an Undirected Graph

2016-03-01 21:57:58

#### Educational Codeforces Round 37 E. Connected Components?（bfs+思路）

2018-02-03 09:56:52

#### Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components?(连通分量的个数,STL)

2018-02-03 11:14:48

#### Codeforces #479 (Div. 3) 解题报告 E. Cyclic Components 无向图 DFS

2018-05-08 19:40:37

#### [leetcode] 323. Number of Connected Components in an Undirected Graph 解题报告

2016-03-13 06:48:19

#### Codeforces292D Connected Components 经典好题并查集

2016-07-26 01:28:51

#### Cyclic Components（并查集）

2018-05-08 20:52:34

#### codeforces 954 D. Fight Against Traffic【BFS + 最短路】

2018-03-22 20:41:19