Codeforces 920 E. Connected Components? (bfs)

网友投稿 613 2022-08-26

Codeforces 920 E. Connected Components? (bfs)

Codeforces 920 E. Connected Components? (bfs)

Description

You are given an undirected graph consisting of n vertices and n(n−1)2−m n ( n − 1 ) 2 − m 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≤m≤min(n(n−1)2,200000) 0 ≤ m ≤ min ( 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 51 23 43 24 22 5

Example output

21 4

题意

求给定图补图的联通块个数以及每个联通块的大小。

思路

分析得出,对于 n n 部图来说,边数 mm

于是我们考虑枚举,初始时用链表存储剩余没有确定的点,然后选取一个点进行 bfs,将所有可以访问到的点全部从链表中删掉。

对剩余的部分也进行同样的操作。

AC 代码

#include#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 mp[maxn];list ret;void init(){ for(int i=1; i<=n; i++) ret.push_back(i);}int bfs(int x){ int cnt = 0; queue 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 ans; while(!ret.empty()) { int x = ret.front(); ret.pop_front(); ans.push_back(bfs(x)); } sort(ans.begin(),ans.end()); cout<>n>>m; init(); for(int i=0; i>u>>v; mp[u][v] = true; mp[v][u] = true; } solve(); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:“Hello world”不简单
下一篇:Codeforces 845 D. Driving Test (模拟)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~