一、题目

  

二、题目链接

  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. C++中的动态绑定

    C++中基类和派生类遵循类型兼容原则:即可用派生类的对象去初始化基类的对象,可用派生类的对象去初始化基类的引用,可用派生类对象的地址去初始化基类对象指针. C++中动态绑定条件发生需要满足2个条件: ...

  2. window.frames && iframe 跨页面通信

    1.定义 frames[]是窗口中所有命名的框架组成的数组.这个数组的每个元素都是一个Window对象,对应于窗口中的一个框架. 2.用法 假设iframe 是一个以存在的 iframe 的 ID 和 ...

  3. 关于浏览器的eventflow(capture and bubble up)

    因为,没有全面的学习javascript,及其事件原理: 全占的课程:4-5 浏览器 Bubble Up 事件模型中 不是很理解它所讲的.网上查找相关知识点.记录中在博客中: 理解了JS的加载 htt ...

  4. 远程使用tomcat8的首页的管理工具

    1.在%Tomcat_Home%/conf/Catalina/localhost中新建manager.xml,内容如下 <Context privileged="true" ...

  5. 偶数求1/2+1/4+...+1/n奇数1/1+1/3+...+1/n

    题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n Scanner scanner = new Scanner(Sy ...

  6. LeetCode 46

    // 又是可以用回溯法做的一道题.class Solution { public: vector<vector<int>> permute(vector<int>& ...

  7. mysqli使用记录

    1.批量插入数据insert 方式一:insert into tableName (id,name,age) values ('1','张三','12'),('2','李四','16'),('3',' ...

  8. Deploying Qt or how to avoid “could not find or load the Qt platform plugin”

    (转自:http://www.tripleboot.org/?p=138) Once you’ve built your first Qt program, have you tried it on ...

  9. EPANET头文件解读系列5——TYPES.H

    /************************************************************************                            ...

  10. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...