zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    netcore跨域
    阿里云oss通过api上传图片后不能预览只能下载的解决方法
    阿里云oss对图片的处理:缩略、剪裁、锐化等
    通过字节值判断图片格式
    Linux 常见命令 用户管理命令(二)
    nohup命令
    selinux基础介绍
    LINUX中的limits.conf配置文件
    【ASP.NET】使用Jquery缓存数据
    .net 4.0以下版本实现web socket服务
  • 原文地址:https://www.cnblogs.com/qscqesze/p/11814113.html
Copyright © 2011-2022 走看看