Educational Codeforces Round37 E - Connected Components?
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
const int N = 2e5 + 5;
vector<int> result;
std::set<pair<int, int> > unExistedMap;
int nextPoint[N];
int prePoint[N];
class List {
private:
int headPoint;
void initialize(int n) {
headPoint = 1;
for (int i = 1; i <= n; ++i) {
nextPoint[i] = i + 1;
prePoint[i] = i - 1;
}
nextPoint[n] = 0;
prePoint[1] = 0;
}
void deletePoint(int pos) {
int prePos = prePoint[pos];
int nexPos = nextPoint[pos];
nextPoint[prePos] = nexPos;
prePoint[nexPos] = prePos;
if (prePos == 0)
headPoint = nexPos;
}
public:
List(int n) { initialize(n); }
void Solve() {
while (1) {
if (headPoint == 0)
break;
std::queue<int> bfsQueue;
bfsQueue.push(headPoint);
deletePoint(headPoint);
int totalMapSeg = 0;
while (!bfsQueue.empty()) {
totalMapSeg++;
int nowPoint = bfsQueue.front();
bfsQueue.pop();
for (int i = headPoint; i; i = nextPoint[i]) {
int from = nowPoint;
int to = i;
if (from > to)
swap(from, to);
if (unExistedMap.find(std::make_pair(from, to)) == unExistedMap.end()) {
bfsQueue.push(i);
deletePoint(i);
}
}
}
result.push_back(totalMapSeg);
}
}
};
int main() {
int n, m;
while (~scanf("%d %d", &n, &m)) {
unExistedMap.clear();
result.clear();
for (int i = 0; i < m; ++i) {
int from, to;
scanf("%d %d", &from, &to);
if (from > to)
swap(from, to);
unExistedMap.insert(std::make_pair(from, to));
}
List Basa = List(n);
Basa.Solve();
sort(result.begin(), result.end());
printf("%d\n", (int)result.size());
for (int i = 0; i < result.size(); ++i) {
printf("%d ", result[i]);
}
printf("\n");
}
return 0;
}
Educational Codeforces Round37 E - Connected Components?的更多相关文章
- Educational Codeforces Round 37-E.Connected Components?题解
一.题目 二.题目链接 http://codeforces.com/contest/920/problem/E 三.题意 给定一个$N$和$M$.$N$表示有$N$个点,$M$表示,在一个$N$个点组 ...
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- 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 ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- [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 ...
随机推荐
- LongAdder基础
LongAdder是JDK8中并发包中的一个新类,和AtomicLong都使用CAS,但是性能比AtomicLong更好. LongAdder在AtomicLong的基础上进行了热点分离,热点分离类似 ...
- Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录
打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...
- WdatePicker-限制日期选择
场景: 1. 开始时间,和结束时间最大选择今天. 2. 开始时间和结束时间的最大时间间隔为30天. jsp代码: <!-- 时间段 --> <form> <!-- 开始时 ...
- ES6 学习笔记之二 块作用域与闭包
"闭包是函数和声明该函数的词法环境的组合." 这是MDN上对闭包的定义. <JavaScript高级程序设计>中则是这样定义的:闭包是指有权访问另一个函数作用域中的变量 ...
- [Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0
更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 ''''''''''''''''''''''''''' ...
- [Python Study Notes]磁盘信息和IO性能
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- CENTOS6.6下zabbix2.4.7搭建
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 安装依赖 安装Perl .apr / apr-util yum -y ...
- Python自动化--语言基础3--字典、函数、全局/局部变量
字典 dict1 = {'name':'han','age':18,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values ...
- C语言_简单的阶乘函数
include <stdio.h> long jc (int num); long jc2 (int num); int main() { long n; n = jc(); printf ...
- Java文件复制与读写
函数介绍 public String readLine():每次读取文件的一行,当文件读取完毕时,返回null public int read(byte[] b):将文件内容读取到字节数组b ...