一、题目

  

二、题目链接

  http://codeforces.com/contest/920/problem/E

三、题意

  给定一个$N$和$M$。$N$表示有$N$个点,$M$表示,在一个$N$个点组成的无向完全图中,接下来的$M$条无向边不存在。

  问你在这个图中有多少个连通分量,并且从小到大输出每个连通分量的顶点个数。

四、思路

  求无向图的连通分量个数,只需要跑一边bfs就OK了。其实dfs也可以,只是太多的“函数压栈现场保护”浪费一丢丢时间而已,慢一点点,影响其实并不大。

  要注意的是,无论跑bfs还是dfs,不能枚举边,因为这题的边数太多了,会超时。只能枚举点,枚举所有未访问的点。

  然后,在跑bfs的过程中,统计这次bfs访问的点的个数,返回就OK了。枚举每一个点,如果是未访问的,就跑一遍bfs,记录这次bfs访问的点的个数,问题就解决了。

五、源代码

  

#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
;

template <class T> inline void read(T &x) {
    int t;
    bool flag = false;
    ')) ;
    ';
     + t - ';
    if(flag) x = -x;
}

int n, m;
unordered_set<int> g[MAXN], invis;
vector<int> ans, tmp;
queue<int> que;

int bfs(int s) {
    ;
    while(!que.empty())que.pop();
    que.push(s);
    invis.erase(s);
    while(!que.empty()) {
        int t = que.front();
        que.pop();
        tmp.clear();
        for(auto x : invis) {//注意不要边迭代、边移除。
            if(g[t].count(x))continue;
            else {
                que.push(x);
                tmp.push_back(x);
                res++;
            }
        }
        for(auto x : tmp)invis.erase(x);
    }
    return res;
}

void init() {
    invis.clear();
    ; i <= n; ++i)invis.insert(i);
    ; i < MAXN; ++i)g[i].clear();
    ans.clear();
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("Einput.txt", "r", stdin);
#endif // ONLINE_JUDGE
    int a, b;
    scanf("%d%d", &n, &m);
    init();
    ; i < m; ++i) {
        read(a), read(b);
        g[a].insert(b);
        g[b].insert(a);
    }
    ; i <= n && invis.size() > ; ++i) {
        if(invis.count(i))ans.push_back(bfs(i));
    }
    sort(ans.begin(), ans.end());
    printf("%d\n", ans.size());
    for(auto x : ans)printf("%d ", x);
    ;
}

Educational Codeforces Round 37-E.Connected Components?题解的更多相关文章

  1. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  3. 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 ...

  4. 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 ...

  5. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Educational Codeforces Round 37 A B C D E F

    A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...

  7. 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 ...

  8. [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)

    Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h ...

  9. 【Educational Codeforces Round 37 E】Connected Components?

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs. 用一个链表来记录哪些点已经确定在某一个联通快里了. 一开始每个点都能用. 然后从第一个点开始进行bfs. 然后对于它的所有 ...

  10. Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?

    题 OvO http://codeforces.com/contest/920/problem/E 解 模拟一遍…… 1.首先把所有数放到一个集合 s 中,并创建一个队列 que 2.然后每次随便取一 ...

随机推荐

  1. Maven简单的配置Junit测试及使用简单的mock

    1.maven依赖配置如下 <dependency> <groupId>org.mockito</groupId> <artifactId>mockit ...

  2. HDU-3853-期望/dp/坑

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  3. hdu 1385 floyd字典序

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. C#金额千分位分隔

    "234234".ToString("C") 显示:¥234,234

  5. epoint:TreeView

    Epoint.Web.UI.WebControls2X.EpointTreeNode 思路:就是使用递归 RootNodeText 根节点名称RootNodeUrl 根节点路径ShowRootNode ...

  6. Winform开发常用控件之ComboBox、ListBox

    ComboBox就是我们常见的下拉框,对于此类控件,我们最关心的当然是数据的绑定和选择值得获取. 首先介绍个属性DropDownStyle,如果不允许ComboBox输入值,只能选择,就选DropDo ...

  7. 如何优化tomcat配置优化

    tomcat默认参数是为开发环境制定,而非适合生产环境,尤其是内存和线程的配置,默认都很低,容易成为性能瓶颈. tomcat内存优化 linux修改TOMCAT_HOME/bin/catalina.s ...

  8. Linux运维学习笔记-通配符及正则表达式知识总结

    通配符: * 代表所有   ? 任意一个字符   : 两个命令的分隔符   # 注释   | 管道,将|前命令的执行结果作为|后命令的输入   ~ 用户的家目录   - 上一次的目录   $ 变量前面 ...

  9. UVA11732 "strcmp()" Anyone?【左儿子右兄弟Trie】

    LINK1 LINK2 题目大意 给你一些字符串,并定义了一个函数(具体见题面) 问你把任意两个字符串放到函数里面得到的值的和是多少 思路 该怎么统计答案呢? 每次考虑当前插入的串和所有已经插入过的串 ...

  10. Windows7 SP1 64bit配置IIS7.5和ASP.NET4

    一.安装前的环境 1. Windows7 SP1 64bit: 2. 在安装IIS7.5之前,安装了Visual Studio 2010或.NET Framework4: 二.安装IIS7.5 1.  ...