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)。

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

  

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9.  
  10. using namespace std;
  11.  
  12. const int M=2e5+;
  13. const int N=2e5;
  14.  
  15. vector<int> mp[M];
  16. queue<int> que;
  17. set<int> s;
  18. int n,m;
  19. int lans,ans[M];
  20.  
  21. void init()
  22. {
  23. for(int i=;i<=N;i++)
  24. mp[i].clear();
  25. s.clear();
  26. lans=;
  27. }
  28.  
  29. bool isConnected(int a,int b)
  30. {
  31. vector<int>::iterator it=lower_bound(mp[a].begin(),mp[a].end(),b);
  32. if(it==mp[a].end() || *it!=b) return true;
  33. return false;
  34. }
  35.  
  36. void solve()
  37. {
  38. queue<int> dlt;
  39. int now,tmp;
  40. set<int>::iterator it,tmp_it;
  41. for(int i=;i<=n;i++)
  42. s.insert(i);
  43. while(s.size()>)
  44. {
  45. while(!que.empty()) que.pop();
  46. while(!dlt.empty()) dlt.pop();
  47. lans++; ans[lans]=;
  48. it=s.begin(); now=*it;
  49. // cout<<now<<' ';
  50. s.erase(it); ans[lans]++;
  51. que.push(now);
  52. while(!que.empty())
  53. {
  54. now=que.front(); que.pop();
  55. for(it=s.begin();it!=s.end();it++)
  56. if(isConnected(now,*it))
  57. {
  58. // cout<<*it<<' ';
  59. que.push(*it),ans[lans]++,dlt.push(*it);
  60. }
  61. while(!dlt.empty())
  62. {
  63. tmp=dlt.front(); dlt.pop();
  64. s.erase(tmp);
  65. }
  66. }
  67. // cout<<endl;
  68. // cout<<s.size()<<endl;
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. init();
  75. int a,b;
  76. scanf("%d%d",&n,&m);
  77. for(int i=;i<=m;i++)
  78. {
  79. scanf("%d%d",&a,&b);
  80. mp[a].push_back(b),mp[b].push_back(a);
  81. }
  82. for(int i=;i<=n;i++)
  83. sort(mp[i].begin(),mp[i].end());
  84. solve();
  85. sort(ans+,ans+lans+);
  86. printf("%d\n",lans);
  87. for(int i=;i<=lans;i++)
  88. printf("%d ",ans[i]);
  89. puts("");
  90. return ;
  91. }

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. Erlang:[笔记一,构建工具rebar之编译]

    Rebar概述 Rebar是一款Erlang构建工具,使用它可以方便的编译,测试erlang程序和打包erlang发行版本.Rebar其实是一个独立的erlang脚本,默认情况下,Rebar会按照Er ...

  2. 新浪sae对storage的文档进行读写操作

    有的人喜欢将一些数据写在服务器的文件里面,并不喜欢存在mysql里,但新浪sae却不支持对本地文件进行操作. 不过sae拓展了一个storage的服务,可以将一些静态文件放在上面.本文不介绍文件的上传 ...

  3. 用Python获取计算机网卡信息

    目录 0. 前言 1. 测试环境及关键代码解释 1.1 测试环境 1.1.1 系统: 1.1.2 开发工具: 2. 模块介绍及演示 2.1 platform模块使用示例 2.2 netifaces模块 ...

  4. vmware 安装XP 32位Professional版本

    VMware14 安装XP 专业版,总部报错Dicrectory Boot not found   下载了N多个XP的版本发现,原来是XP版本的问题,现将正确版本写在这里,以备后用 百度网盘共享位置 ...

  5. windows通过gcc编译代码

    1.将gcc添加到环境变量 2.检查gcc是否安装成功 cmd下输入gcc –v 3.cd进入需要编译源文件的目录 4.dir查看当前目录下是否有需要编译的文件(linux下用ls) 5.编译文件(H ...

  6. python中检测某个变量是否有定义

    目录 第一种方法使用内置函数locals() 第二种方法使用内置函数dir() 第三种方法使用内置函数vars() 第一种方法使用内置函数locals() 'testvar' in locals(). ...

  7. jacascript 数组

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 数组的增删 第一个数组元素的索引值为 0,第二个索引值为 1,始终递增. 数组可以在头部增加元素arr.u ...

  8. java 文件上传 下载 总结

    首先引入2个jar ![](http://images2017.cnblogs.com/blog/1128666/201711/1128666-20171101145630498-2084371020 ...

  9. Java 18套JAVA企业级大型项目实战分布式架构高并发高可用微服务电商项目实战架构

    Java 开发环境:idea https://www.jianshu.com/p/7a824fea1ce7 从无到有构建大型电商微服务架构三个阶段SpringBoot+SpringCloud+Solr ...

  10. Python练习_购物车_day6

    第一次代码 (1) 输出商品列表,用户输入序号,显示用户选中的商品. 页面显示 序号 + 商品名称,如: 1 手机 2 电脑 (2): 用户输入选择的商品序号,然后打印商品名称 (3):如果用户输入的 ...