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. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x,?y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices.

You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices X such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to X violates this rule.

Input

The first line contains two integers n and m (1?≤?n?≤?200000, ).

Then m lines follow, each containing a pair of integers x and y (1?≤?x,?y?≤?n, x?≠?y) denoting that there is no edge between x and y. Each pair is listed at most once; (x,?y) and (y,?x) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices.

Output

Firstly print k — the number of connected components in this graph.

Then print k integers — the sizes of components. You should output these integers in non-descending order.

Example

input
5 5
1 2
3 4
3 2
4 2
2 5
output
2
1 4

题意

给你n个点的完全图,告诉你有m条边是不可连的。问你里面一共有多少个联通块,输出每个块的大小。

题解

https://www.cnblogs.com/qscqesze/p/11813351.html 一摸一样

经验get

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int n,m;
set<int>S[maxn];
set<int>vis;
int v[maxn];
int dfs(int x){
    int now = 1;
    vector<int> ret;
    for(int v:vis){
        if(!S[x].count(v))
            ret.push_back(v);
    }
    for(int i=0;i<ret.size();i++){
        vis.erase(ret[i]);
    }
    for(int i=0;i<ret.size();i++){
        v[ret[i]]=1;
        now+=dfs(ret[i]);
    }
    return now;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int x,y;cin>>x>>y;
        x--,y--;
        S[x].insert(y);
        S[y].insert(x);
    }
    vector<int> ans;
    for(int i=0;i<n;i++){
        vis.insert(i);
    }
    for(int i=0;i<n;i++){
        if(!v[i]){
            ans.push_back(dfs(i));
        }
    }
    cout<<ans.size()<<endl;
    sort(ans.begin(),ans.end());
    for(int i=0;i<ans.size();i++){
        cout<<ans[i]-1<<" ";
    }
    cout<<endl;
}

原文地址:https://www.cnblogs.com/qscqesze/p/11814113.html

时间: 2024-07-30 18:58:35

Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components? 图论的相关文章

Educational Codeforces Round 37 (Rated for Div. 2)A,B,C,F

A Water The Garden 数据不大,暴力模拟下直至把每个花床都遍历过的过程即可 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define PI acos(-1.0) 5 #define INF 1e18 6 #define inf 0x3f3f3f3f 7 #define FAST_IO ios::sync_with_stdio(false) 8 9 const int N=456; 10 typedef long

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

Water The Garden #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #inclu

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ? 给你一个有向图,给用最少的颜色给每条边染色,要保证不存在一个环中的所有边都是同一个颜色. [Solution] ? 用拓扑排序判断图中是否存在环,若图中不存在环,则所有边都是同一种颜色.否则,同一个环中,只要用两种颜色就可以满足题目条件,所以总的颜色数就是两种,对于一个环,一定会存在两种边:1.节点号小

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w