Codeforces 920 E Connected Components?
Discription
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
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4 我们发现对于N个点的完全图来说,删去M条边的影响是有限的(因为N和M最大都是2*10^5)。
所以可以猜测的是不会有很多联通块。那么就可以暴力BFS一下,用一个链表来维护当前还没有被放入联通块的节点。
当我们从一个节点扩展的时候,就在链表里找它所能到达的点,然后把它们从链表中删去,假如BFS的队列中。 我们在链表中扫到一个元素还不删除的次数之和最多是2*M,因为只有和当前点有连边的链表中的点才不会被删除。
这样就保证了时间复杂度,也就是每个点最多进一次队列,链表中的所有点被扫到的次数之和<=2*M+N。
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<map>
#define ll long long
#define maxn 200005
using namespace std;
map<int,int> mmp[maxn];
int ans[maxn],tot=0,n,m;
int hd,ne[maxn]; inline void solve(int tmp){
queue<int> q;
ans[tmp]=0;
int x,now,pre; q.push(hd),hd=ne[hd];
while(!q.empty()){
x=q.front(),q.pop(),ans[tmp]++; for(now=hd,pre=0;now;now=ne[now])
if(!mmp[x][now]){
q.push(now);
if(hd==now) hd=ne[hd];
ne[pre]=ne[now];
}
else pre=now;
}
} int main(){
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&uu,&vv);
mmp[uu][vv]=mmp[vv][uu]=1;
} for(int i=1;i<=n;i++) ne[i]=hd,hd=i; while(hd) solve(++tot); printf("%d\n",tot);
sort(ans+1,ans+tot+1);
for(int i=1;i<=tot;i++) printf("%d ",ans[i]);
puts(""); return 0;
}
Codeforces 920 E Connected Components?的更多相关文章
- Educational Codeforces Round 37-E.Connected Components?题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组 ...
- Educational Codeforces Round37 E - Connected Components?
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论
E. Connected Components? You are given an undirected graph consisting of n vertices and edges. Inste ...
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [Redux] Using withRouter() to Inject the Params into Connected Components
We will learn how to use withRouter() to inject params provided by React Router into connected compo ...
随机推荐
- Ironic-Python-Agent
Ironic-Python-Agent 在PXE部署环境中,deploy模块是通过打开一个iSCSI设备,ironic-conductro将OS的镜像文件写到iSCSI的设备,所以deploy_ram ...
- 一个画ROC曲线的封装包
Draw_ROC_Curves This is a python file which is used for drawing ROC curves -f : assign file name -t ...
- 重现ssd遇到的问题
首先是create_list.sh和create_data.sh中的data_dir的路径得修改. 然后是在create_data.sh文件调用$caffe_root下的scripts目录中的crea ...
- 团队项目-任务分解[Alpha0]
团队项目-任务分解[Alpha0] 标签(空格分隔): 团队博客 适用范围: 本文档 适用对象 团队全体成员 适用时间 alpha阶段第一周计划 10.24-10.28 适用内容 目标.分工.时长估计 ...
- Access连接字符串
Access2007没有密码连接: <connectionStrings> <add name="myconn" connectionString="P ...
- (转)详解JavaScript模块化开发
https://segmentfault.com/a/1190000000733959 什么是模块化开发? 前端开发中,起初只要在script标签中嵌入几十上百行代码就能实现一些基本的交互效果,后来j ...
- qemu中的网络设置
https://www.cnblogs.com/hukey/p/6436211.html 这个链接里教你怎么操作kvm的各种网络模式,实际操作成
- gcc 编译器常用的命令行参数一览
这些常用的 gcc/g++ 命令行参数,你都知道么? 1. gcc -E source_file.c -E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c -S, ...
- Python之面向对象:闭包和装饰器
一.闭包 1. 如果一个函数定义在另一个函数的作用域内,并且引用了外层函数的变量,则该函数称为闭包. def outter(): name='python' def inner(): print na ...
- Oracle 根据逗号分隔字符串 同时记录一波坑
报表需要过滤掉不需要的数据,由于报表是根据零件编号来统计,需要过滤掉不合格品,只能根据关联的物料编码(零件编号)来过滤,只能通过not in来过滤,但是天真的我却用下面代码来当子查询: b.part_ ...