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';
}
}
}

最新文章

  1. 深入理解java中的ArrayList和LinkedList
  2. 生产环境常见的HTTP状态码列表
  3. 【转】The import javax.servlet cannot be resolved
  4. AseBulkCopy 若干问题的解决方法
  5. WebService之CXF框架
  6. prepareStatement和Statement的区别
  7. Labview实现脉波调制( PAM )
  8. 关于自动刷新CSS
  9. java学习笔记 --- 条件,循环语句
  10. [ext4]08 磁盘布局 - CheckSums
  11. Android 不规则图像填充 小玩着色游戏
  12. docker学习笔记(3)
  13. Linux&#160;操作系统主机名变成bogon怎么解决?
  14. Ubuntu下安装hbase
  15. linux下关于PCL(point cloud library)库的安装,三行命令错误的问题
  16. 黑马程序员_java基础笔记(15)...银行业务调度系统_编码思路及代码
  17. iOS内存管理 -讲的不错,角度独特
  18. 【转】Scheme 编程环境的设置
  19. 什么是webpack?
  20. [转]理解Linux的处理器负载均值

热门文章

  1. GitHub快速搭建个人博客
  2. 最新 美图java校招面经 (含整理过的面试题大全)
  3. 轻松搞定Vue 使用SignalR与Asp.net Core通讯
  4. Cent7.2单用户模式
  5. [Agc028A]Two Abbreviations_数学
  6. SQLite进阶-13.Autoincrement关键字
  7. 【AtCoder】AGC011
  8. dfs/bfs专项训练
  9. Java学习总结一 数据类型
  10. 禅道工具的下载和使用(原地址:https://www.cnblogs.com/ydnice/p/5800256.html)