Codeforces 920E-Connected Components? (set,补图,连通块)
Connected Components?
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,补图,连通块)的更多相关文章
- Codeforces 920E Connected Components? 补图连通块个数
题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...
- [Codeforces 920E]Connected Components?
Description 题库链接 给你一个 \(n\) 个点 \(m\) 条边的无向图,求其补图的连通块个数及各个连通块大小. \(1\leq n,m\leq 200000\) Solution 参考 ...
- 【BZOJ 1098】办公楼(补图连通块个数,Bfs)
补图连通块个数这大概是一个套路吧,我之前没有见到过,想了好久都没有想出来QaQ 事实上这个做法本身就是一个朴素算法,但进行巧妙的实现,就可以分析出它的上界不会超过 $O(n + m)$. 接下来介绍一 ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...
- CodeForces 690D1 The Wall (easy) (判断连通块的数量)
题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...
- CodeForces 292D Connected Components (并查集+YY)
很有意思的一道并查集 题意:给你n个点(<=500个),m条边(<=10000),q(<=20000)个询问.对每个询问的两个值xi yi,表示在从m条边内删除[xi,yi]的边后 ...
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- 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 ...
随机推荐
- jq append添加的元素click获取不到的解决方法
移动端手机ios网页,apppend添加html class名用 on()方法没效果 解决方案:在html里面给需要点击的元素加一个onclick="javascript:void(0)&q ...
- 源码搭建zabbix服务
1) 部署LNMP 1.1) cd /root tar -xf lnmp_soft.tar.gz cd lnmp_soft/ tar -xf nginx-1.10.3.tar.gz cd nginx- ...
- 【转载】DOS系统的安装
<电脑爱好者>报转载第一辑第一篇之DOS系统的安装 DOS系统的安装 一.DOS的历史 DOS是Diskette Operating System的缩写,意思是磁盘操作系统,主要有MS-D ...
- Python初始
一,Python介绍 1.Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间, ...
- BIO和NIO实现文件复制
普通文件复制 public void copyFile() throws Exception{ FileInputStream fis=new FileInputStream("C:\\Us ...
- EMR-LDAP配置
usersync是负责在配置policy的时候可选用户有ldap里的用户,admin是负责登录webui的 https://cwiki.apache.org/confluence/display/RA ...
- String、StringBuilder、StringBuffer的爱恨情仇
第三阶段 JAVA常见对象的学习 StringBuffer和StringBuilder类 (一) StringBuffer类的概述 (1) 基本概述 下文以StringBuffer为例 前面我们用字符 ...
- Oracle的查询-分组查询
--查询出每个部门的平均工资 select e.deptno,avg(e.sal) from emp e group by e.deptno; 分组查询中,出现在 group by 后面的原始列,才能 ...
- 使用pycharm开发web——django2.1.5(一)入坑尝试第一步,基本搭建
首先,接触python的人应该都会用pip 来安装需要的包吧(------>>>>)默认 在运行中使用python -m django --version来检查自己的djang ...
- Idea中Smart Tomcat插件启动报NullPointerException问题
如果你跟我一样用的是Idea Community社区版的话,也一定会遇到用Smart Tomcat插件启动报错的问题: 这个问题网上搜了一圈,大家好像也都遇到过了,不过也都没有找到原因和给出解决方案. ...