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 ...
随机推荐
- dubbo refrence bean(服务引用)
在xml上写一个dubbo标签就可以把远程的服务引用到本地使用: <dubbo:reference id="buyFoodService" interface="c ...
- JDK,JRE,JVM的区别与联系
JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...
- 在控制台进行依赖注入(DI in Console)
首先我们准备两个服务接口 public interface IServiceA { void showConsole(); int GetValue(int val); } public interf ...
- 关于JavaScript的事件处理一些知识
<JS事件处理> Event对象详细信息:http://www.w3school.com.cn/jsref/dom_obj_event.asp JS原生支持3中绑定事件方式: 1.以标签属 ...
- ansible实践3-playbook条件判断
--- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: t ...
- Java面试题库及答案解析
1.面向对象编程(OOP)有哪些优点? 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码的可理解性. 2.面向对象编程有哪些特性? 封装.继承.多态.抽象 封装 封装 ...
- mysql中的范式与范式——读<<高性能mysql>>笔记一
对于任何给定的数据库通常都有很多表示方法,从完全的范式化到完全的反范式化,以及两者的折中.在范式化的数据库中,每个事实数据会出现并且只出现一次.相反,在反范式化的数据库中,可能会存储在多个地方. 那什 ...
- 0基础学python3心得体会 - python3学习笔记 - python3基础
基础预热 print()会依次打印每个字符串,遇到逗号","会输出一个空格,可以打印整数,或者计算 结果 Python提供了一个input(),,可以让用户输入字符串,并存放到一个 ...
- Java SocketChannel 读取ByteBuffer字节的处理模型
在JAVA中的流分为字节流或字符流,一般来说采用字符流处理起来更加方便.字节流处理起来相对麻烦,SocketChannel中将数据读取到ByteBuffer中,如何取出完整的一行数据(使用CRLF分隔 ...
- [翻译]编写高性能 .NET 代码 第二章:垃圾回收
返回目录 第二章:垃圾回收 垃圾回收是你开发工作中要了解的最重要的事情.它是造成性能问题里最显著的原因,但只要你保持持续的关注(代码审查,监控数据)就可以很快修复这些问题.我这里说的"显著的 ...