一、题目

  

二、题目链接

  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. Android静态变量的生命周期

    Android是用Java开发,其静态变量的生命周期遵守Java的设计.我们知道静态变量是在类被load的时候分配内存的,并且存在于方法区.当类 被卸载的时候,静态变量被销毁.在PC机的客户端程序中, ...

  2. 何时使用MQ ?

    何时使用MQmq作为一种基础中间件在互联网项目中有着大量的使用. 一种技术的产生自然是为了解决某种需求,通常来说是以下场景: 需要跨进程通信:B系统需要A系统的输出作为输入参数.当A系统的输出能力远远 ...

  3. Linux编写一个C程序HelloWorld

    环境 需要文本编辑器和编译器,文本编辑器用linux(我用的centos7)自带的vi,编译器用gcc(GNU C Compiler/GNU Compiler Collection) 安装gcc,查看 ...

  4. 第105天:Ajax 客户端与服务器基本知识

    一.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.服务器类型 - 按服务类型可分为:文件服务器.数据库服务器.邮件服务器.Web服务器等 - 按操作系统可分为:Linux服务 ...

  5. bzoj2330: [SCOI2011]糖果 差分约束系统

    幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖果的时候 ...

  6. 剑指 offer面试题20 顺时针打印矩阵

    [题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

  7. bzoj1067

    题意: 给你下雨量,让你判断每一句话是否正确 题解: 线段树 用来维护判断 代码: #include <cstdio> #include <cstring> #include ...

  8. Date、DateFormat、SimpleDateFormat、Calendar

    package com.Date; import java.util.Date; /* * Date 表示特定的瞬间,精确到毫秒 * JDK1.0开始 * 构造方法(常用的方法,过时的不管): * D ...

  9. SqlServer缓存依赖 示例

    ------------------------------------------------------------c#代码----------------------using System; ...

  10. alsa-lib、alsa-utils移植

    /************************************************************************** * alsa-lib.alsa-utils移植 ...