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. linux破解navicat for mysql

    第一次执行start_navicat时,会在用户主目录下生成一个名为.navicat64的隐藏文件夹. cd ~/.navicat64 此文件夹下有一个system.reg文件 rm system.r ...

  2. centos7 通过yum安装mysql

    但是CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql的repo源. 1.下载mysql的repo源 $ wget http://repo.mysql.co ...

  3. Oracle数据库——查询所有用户

    查询数据库所有用户(ALL_USERS)的用户名,用户编号,创建日期 默认应该有36个用户 SELECT * FROM ALL_USERS; 查看ALL_USERS的结构 DESC ALL_USERS ...

  4. mysql存储emoji表情报错的处理方法【更改编码为utf8mb4】

    utf-8编码可能2个字节.3个字节.4个字节的字符,但是MySQL的utf8编码只支持3字节的数据,而移动端的表情数据是4个字节的字符.如果直接往采用utf-8编码的数据库中插入表情数据,Java程 ...

  5. Linux 数据库MySql 安装配置教程!

    本文价绍Linux 相关mysql 安装和配置以及基本连接测试 1官网下载安装mysql-server # wget http://dev.mysql.com/get/mysql-community- ...

  6. 使用的jQuery加载源的优势【问题】

    [问题]使用的jQuery加载源的优势? [答案]许多用户在访问其他站点时,已经从谷歌或微软加载过 jQuery.所有结果是,当他们访问您的站点时,会从缓存中加载 jQuery,这样可以减少加载时间. ...

  7. Marketing Cloud contact主数据的csv导入

    使用这个mock数据生成器网站https://www.mockaroo.com/b6790790,创建一个基于Marketing Cloud contact schema的csv文件. 如果偷懒的话, ...

  8. 【大数据】Clickhouse基础知识

    第1章 ClickHouse概述 1.1 什么是ClickHouse ClickHouse 是俄罗斯的Yandex于2016年开源的列式存储数据库(DBMS),主要用于在线分析处理查询(OLAP),能 ...

  9. JAVA 泛型 通配符? extends super限定,实例区分extends super限定的作用用法

    java泛型中的关键字 ? 表示通配符类型 <? extends T> 既然是extends,就是表示泛型参数类型的上界,说明参数的类型应该是T或者T的子类. <? super T& ...

  10. EF方式增加数据

             单条记录添加 第一种方式: public void AddRegion() { using(Northwind db = new Northwind()) { Region regi ...