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< ...
随机推荐
- Erlang:[笔记一,构建工具rebar之编译]
Rebar概述 Rebar是一款Erlang构建工具,使用它可以方便的编译,测试erlang程序和打包erlang发行版本.Rebar其实是一个独立的erlang脚本,默认情况下,Rebar会按照Er ...
- 新浪sae对storage的文档进行读写操作
有的人喜欢将一些数据写在服务器的文件里面,并不喜欢存在mysql里,但新浪sae却不支持对本地文件进行操作. 不过sae拓展了一个storage的服务,可以将一些静态文件放在上面.本文不介绍文件的上传 ...
- 用Python获取计算机网卡信息
目录 0. 前言 1. 测试环境及关键代码解释 1.1 测试环境 1.1.1 系统: 1.1.2 开发工具: 2. 模块介绍及演示 2.1 platform模块使用示例 2.2 netifaces模块 ...
- vmware 安装XP 32位Professional版本
VMware14 安装XP 专业版,总部报错Dicrectory Boot not found 下载了N多个XP的版本发现,原来是XP版本的问题,现将正确版本写在这里,以备后用 百度网盘共享位置 ...
- windows通过gcc编译代码
1.将gcc添加到环境变量 2.检查gcc是否安装成功 cmd下输入gcc –v 3.cd进入需要编译源文件的目录 4.dir查看当前目录下是否有需要编译的文件(linux下用ls) 5.编译文件(H ...
- python中检测某个变量是否有定义
目录 第一种方法使用内置函数locals() 第二种方法使用内置函数dir() 第三种方法使用内置函数vars() 第一种方法使用内置函数locals() 'testvar' in locals(). ...
- jacascript 数组
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 数组的增删 第一个数组元素的索引值为 0,第二个索引值为 1,始终递增. 数组可以在头部增加元素arr.u ...
- java 文件上传 下载 总结
首先引入2个jar  输出商品列表,用户输入序号,显示用户选中的商品. 页面显示 序号 + 商品名称,如: 1 手机 2 电脑 (2): 用户输入选择的商品序号,然后打印商品名称 (3):如果用户输入的 ...