[codeforces920E]Connected Components?

time limit per test : 2 seconds
memory limit per test : 256 megabytes

分数:2100

You are given an undirected graph consisting of n n n vertices and n ( n − 1 ) 2 − m {n(n-1)\over2} -m 2n(n1)m edges. Instead of giving you the edges that exist in the graph, we give you m m m unordered pairs ( x ,   y ) (x, y) (x,y) such that there is no edge between x x x and y y 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 n n and m ( 1   ≤   n   ≤   200000 , 0 &lt; = m &lt; = m i n ( n ( n − 1 ) 2 , 200000 ) m (1 ≤ n ≤ 200000,0&lt;=m&lt;=min( {{n(n-1)}\over{2}},200000) m(1n200000,0<=m<=min(2n(n1),200000)).

Then m lines follow, each containing a pair of integers x x x and y ( 1   ≤   x ,   y   ≤   n , x   ≠   y ) y (1 ≤ x, y ≤ n, x ≠ y) y(1x,yn,x̸=y) denoting that there is no edge between x x x and y y y. Each pair is listed at most once; ( x ,   y ) (x, y) (x,y) and ( y ,   x ) (y, x) (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 k k — the number of connected components in this graph.
Then print k k 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,有一个n个点的图,给定m条边,表示这个图中不存在这条边(没有给出的边都存在)
要求求出这个图有几个联通块和联通块的大小。

题解:
用一个set存当前还未被访问过的点即可,每个点再开一个set存这个点连出去的不存在的边。然后直接正常dfs求联通块大小即可(记得在dfs之前要先从set中删与当前点直接相连的点)。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,sz;
set<int>tr,a[200004];
void dfs(int x){
    sz++;
    set<int>temp;temp.clear();
    for(int now:tr){
        if(!a[x].count(now)){
            temp.insert(now);
        }
    }
    for(int now:temp)tr.erase(now);
    for(int now:temp)dfs(now);
}
vector<int>ans;

int main(){
    ans.clear();
    tr.clear();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        tr.insert(i);
        a[i].clear();
    }
    for(int i=1;i<=m;i++){
        int u,v;scanf("%d%d",&u,&v);
        a[u].insert(v);
        a[v].insert(u);
    }
    for(int i=1;i<=n;i++){
        if(tr.count(i)){
            tr.erase(i);
            sz=0;
            dfs(i);
            ans.push_back(sz);
        }
    }
    sort(ans.begin(),ans.end());
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)printf("%d ",ans[i]);
    puts("");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值