OvO http://codeforces.com/contest/920/problem/E

  模拟一遍……

  1.首先把所有数放到一个集合 s 中,并创建一个队列 que

  2.然后每次随便取一个数,并且从集合中删除这个数,将这个数放入 que

  3.取 que 首元素,记为 now,然后枚举集合 s,每次找到 s 中和 now 相连的元素 x,从 s 中删除元素 x,并且把 x 放入 que 中。

  4.如果 s 不为空,回到步骤2

  可见就是一个模拟,至于复杂度,计算如下。

  由于每个数字只会从集合 s 中删除一次。步骤3中枚举集合 s 元素的操作中,记成功从集合 s 中删除元素的为有效操作,反之为无效操作,则每一次有效操作必然会使 s 中元素数量减,所以有效操作复杂度为O(n)。而每次无效操作则必然会使用到一对题目所给的 (x,y),其中 x 与 y 不相连,可见无效操作复杂度为 O(m)。

  其他复杂度计算显然。加起来不会超时。

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue> using namespace std; const int M=2e5+;
const int N=2e5; vector<int> mp[M];
queue<int> que;
set<int> s;
int n,m;
int lans,ans[M]; void init()
{
for(int i=;i<=N;i++)
mp[i].clear();
s.clear();
lans=;
} bool isConnected(int a,int b)
{
vector<int>::iterator it=lower_bound(mp[a].begin(),mp[a].end(),b);
if(it==mp[a].end() || *it!=b) return true;
return false;
} void solve()
{
queue<int> dlt;
int now,tmp;
set<int>::iterator it,tmp_it;
for(int i=;i<=n;i++)
s.insert(i);
while(s.size()>)
{
while(!que.empty()) que.pop();
while(!dlt.empty()) dlt.pop();
lans++; ans[lans]=;
it=s.begin(); now=*it;
// cout<<now<<' ';
s.erase(it); ans[lans]++;
que.push(now);
while(!que.empty())
{
now=que.front(); que.pop();
for(it=s.begin();it!=s.end();it++)
if(isConnected(now,*it))
{
// cout<<*it<<' ';
que.push(*it),ans[lans]++,dlt.push(*it);
}
while(!dlt.empty())
{
tmp=dlt.front(); dlt.pop();
s.erase(tmp);
}
}
// cout<<endl;
// cout<<s.size()<<endl;
}
} int main()
{
init();
int a,b;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
mp[a].push_back(b),mp[b].push_back(a);
}
for(int i=;i<=n;i++)
sort(mp[i].begin(),mp[i].end());
solve();
sort(ans+,ans+lans+);
printf("%d\n",lans);
for(int i=;i<=lans;i++)
printf("%d ",ans[i]);
puts("");
return ;
}

Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?的更多相关文章

  1. Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements (思维,前缀和)

    Educational Codeforces Round 37 (Rated for Div. 2)C. Swap Adjacent Elements time limit per test 1 se ...

  2. Educational Codeforces Round 37 (Rated for Div. 2)

    我的代码应该不会被hack,立个flag A. Water The Garden time limit per test 1 second memory limit per test 256 mega ...

  3. Educational Codeforces Round 37 (Rated for Div. 2) G

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  4. [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

    Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...

  5. 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 ...

  6. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  9. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

随机推荐

  1. go 相关资源

    网站guide 官方文档 国内镜像 包下载 Golang标准库文档 Release History Getting Start 安装 1.下载binrary包(zip 解压后需要配置gopath, m ...

  2. 《Redis - 穿透/击穿/雪崩/集中失效》

    一:什么是缓存穿透? - 定义 - 正常情况下,我们在理想的条件下去查询缓存数据都是存在的. - 那么请求去查询一条数据库中不存在的数据,也就是缓存和数据库都查询不到这条数据. - 所以请求每次都会打 ...

  3. 笔试2019-GRANDSTREAM

    程序阅读 随机,因为局部变量在栈中,值随机. 上面那个题在x86上都是12因为x86是大端模式 但是在ARM架构的处理器,因为它们是小端模式,则输出x078 判断回文 int hui(const ch ...

  4. 使用不同代理IP刷票的脚本---requests

    投票功能限制刷票是通过限制单个IP的投票次数实现的,所以写了个脚本用于测试此功能. #-*- coding=utf-8 -*- ''' 功能:此脚本用于用不同的IP刷票 作者:Elle 最后修改日期: ...

  5. python学习-46 时间模块

    时间模块 ····时间戳 print(time.time()) 运行结果: 1564294158.0389376 Process finished with exit code 0 ·····结构化时 ...

  6. JS 08表单操作_表单域

    一.表单的获取方式 document.getElementById() document.forms[index]; document.forms[form_name] document.form_n ...

  7. (三)Redis之数据结构概念以及数据结构之字符串

    一.数据结构 五种数据类型: 字符串(String) 字符串列表(list) 有序字符串集合(sorted set) 哈希(hash) 字符串集合(set) 二.数据结构之字符串 二进制安全的,存入和 ...

  8. Linux每隔1秒kill掉cpu大于50%的进程

    1.新建/test/killcpu.sh shell脚本 并授予权限0755#!/bin/bashps axf -o "pid %cpu" | awk '{if($2>=50 ...

  9. Asp.Net Core 轻松学系列-5利用 Swagger 自动生成接口文档

    目录 前言 结语 源码下载 前言     目前市场上主流的开发模式,几乎清一色的前后端分离方式,作为服务端开发人员,我们有义务提供给各个客户端良好的开发文档,以方便对接,减少沟通时间,提高开发效率:对 ...

  10. Python实现FTP文件定时自动下载

    之前遇到技术问题总能在技术博客上得到启发,十分感谢各位的无私分享.而自己却很少发文,固然是水平有限,但也限制了知识积累和总结.今后多总结分享,回馈博客的同时也希望大家多多批评. 一.需求: 某数据公司 ...