Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?
题
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?的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...
- 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 ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- time模块/datetime模块/calendar模块
time模块时间的表示形式时间戳:以整型或浮点型表示⼀个时间,该时间以秒为单位,这个时间是以1970年1⽉1⽇0时0分0秒开始计算的. 导入time import time 1.返回当前的时间戳 no ...
- Jfinal初次尝试及Jetty使用相关问题
Jetty介绍 Jetty官网:http://www.eclipse.org/jetty/ 参考:Jetty使用教程(一)--开始使用Jetty 1. 使用 参考上面文档,但是有些更新: echo $ ...
- 怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数
1. 向上取整使用: Math.ceil() Math.ceil(0.1); Math.ceil(1.9); 2. 向下取整使用: Math.floor() Math.floor(0.1); Math ...
- (十一)web服务与javaweb结合(2)
一.解决问题及解决方法 解决问题:上章节用监听器的方式是有缺陷的:web服务的端口和web工程的端口不能一致. 解决方案:将webService绑定到web工程中,使得共用一个端口. 二.案例 2.1 ...
- kafka之基本介绍
什么是kafka? Kakfa起初是由LinkedIn公司开发的一个分布式的消息系统,后成为Apache的一部分,它使用Scala编写,以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理 ...
- Django admin 外键关联默认显示用户的username
使用默认User表.默认显示用户username,转换成get_full_name() /home/labsmith/venv_labsmit/lib/python3.6/site-packages/ ...
- Go part 4 数据容器(数组,slice,string,map,syncMap,list)
数组 数组是值类型,因此改变副本的值,不会影响到本身 数组的定义:var 变量名 [元素数量] T 变量名(符合标识符要求即可) 元素数量(整型,可以是const中的值) T(可以是任意基本类型,包括 ...
- Redis 简单使用 and 连接池(python)
Redis 简介 NoSQL(not only sql):非关系型数据库 支持 key-value, list, set, zset, hash 等数据结构的存储:支持主从数据备份,集群:支持 ...
- JS 知识图
- stm32 printf重定向
printf函数调用fputc int fputc(int ch, FILE *p) { USART_SendData(USART1, ch); //重定向到串口 while(USART_GetFla ...