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 ...
随机推荐
- EntityFramework 实践 Overview
使用EntityFramework,是微软出的一个轻量级ORM框架,对于做一些小型的项目非常方便,几乎是零配置,以及对linq的支持,所以非常的易于使用,虽然已经使用EntityFramework很久 ...
- 算法&数据结构系列 -- 堆(优先队列)
前言 话说新开的博客十分好用... 所以,我打算开一个坑,名曰[算法系列]. 什么意思--从名字泥应该就猜得出来... 废话不多说,进入正文~~ 正文 原理 首先,堆是一颗棵二叉树.. 其次,堆是一棵 ...
- 应用负载均衡之LVS(二):VS_TUN和VS_DR的arp问题
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- win7本地连接消失
可能原因一 驱动程序错误: 右键"计算机"图标 - "属性" - 设备管理器现在在设备管理器下面找到"网络适配器",在下面的网卡驱动中发现有 ...
- JavaScript对象创建的几种方式
1 工厂模式 1.1 创建 function createFruit(name,colors) { var o = new Object(); o.name = name; o.colors = co ...
- Tomcat输出保存JVM GC日志文件
当系统出现问题时,分析java虚拟机GC日志可以帮助我们定位问题,一般来说, 我们可以通过制定JVM参数使tomcat保存GC日志文件,具体实现如下: Windows下: 找到tomcat的解压目录, ...
- java 23种设计模式 深入理解
以下是学习过程中查询的资料,别人总结的资料,比较容易理解(站在各位巨人的肩膀上,望博主勿究) 创建型抽象工厂模式 http://www.cnblogs.com/java-my-life/archive ...
- 15-谜问题(深拷贝、LC检索、面向对象编程)
问题描述: 在一个分成16格的方形棋盘上,放有15块编了号码的牌.对这些牌给定一种初始排列,要求通过一系列的合法移动将这一初始排列转换成目标排列. 这个问题解决时用到了L-C检索.在检索的过程中计算估 ...
- iOS——系统提供的dispatch方法
// 后台执行: dispatch_async(dispatch_get_global_queue(0,0), ^{ // something }); // 主线程执行: dispatch_async ...
- Qt msvc Modules
3D ActiveQt container ActiveQt server Bluetooth Concurrent Core Enginio Declarative Gui Help Locatio ...