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< ...
随机推荐
- SQL,NoSQL和NewSQL
一:概念 SQL(Structured Query Language):数据库,指关系型数据库.主要代表:SQL Server.Oracle.MySQL.PostgreSQL. NoSQL(Not O ...
- 在VMware Workstation10下CentOS7虚拟机中创建与主机共享文件夹的详细步骤
一.前言 在使用虚拟机时,常常需要与宿主计算机(以下简称为主机)操作系统交换文件,为此需要在虚拟机与主机之间建立共享文件夹. 二. 安装VMTools 要使用共享文件机制,必须首先安装VMTools. ...
- GPIO输入—按键检测(开关控制小灯)
本次的代码全是在上次代码之上添加的. 1.user下新建文件夹key,新建bsp_key.h bsp_key.c文件. 2.keil项目添加bsp_key.c,魔术棒C/C++中include pat ...
- JS 08表单操作_表单域
一.表单的获取方式 document.getElementById() document.forms[index]; document.forms[form_name] document.form_n ...
- Django 中 app_name (应用命名空间) 和 namespace (实例命名空间) 的区别
转自:https://www.jianshu.com/p/404500a0408a 补充理解: 先把官网上对应用命名空间(app_name)和实例命名空间(namespace)的解释贴上: app_n ...
- Tomcat安装及其目录结构介绍
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选. Tomcat的安装版本有绿色解压 ...
- C#获取客户端Ip工具类
string pcname = Dns.GetHostName(); string ip = Dns.GetHostAddresses(pcname).First().ToString(); usin ...
- (二十二)SpringBoot之使用mybatis generator自动生成bean、mapper、mapper xml
一.下载mybatis generator插件 二.生成generatorConfig.xml new一个generatorConfig.xml 三.修改generatorConfig.xml 里面的 ...
- Windows群集之NLB【转】
本文转自:http://www.talkwithtrend.com/Article/31746 网络负载平衡群集(Network Load balancing) 在Internet快速发展的今天,为了 ...
- MVC的12种ActionResult介绍以及应用示例【转】
一.介绍 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分视图结果,与ViewR ...