Codeforces 920 E. Connected Components? (bfs)

Description

You are given an undirected graph consisting of n vertices and $\frac{n(n-1)}{2}-m$ 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, $0≤m≤\min(\frac{n(n-1)}{2},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

 

Example output

2
1 4

 

题意

求给定图补图的联通块个数以及每个联通块的大小。

 

思路

分析得出,对于 $n$ 部图来说,边数 $m$ 最大 200000 很小很小,也就是说,图中的联通块个数不会很多。

于是我们考虑枚举,初始时用链表存储剩余没有确定的点,然后选取一个点进行 bfs,将所有可以访问到的点全部从链表中删掉。

对剩余的部分也进行同样的操作。

 

AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
const int maxn = 2e5+10;

int n,m;
unordered_map<int,bool> mp[maxn];
list<int> ret;

void init()
{
    for(int i=1; i<=n; i++)
        ret.push_back(i);
}

int bfs(int x)
{
    int cnt = 0;
    queue<int> sk;
    sk.push(x);
    while(!sk.empty())
    {
        int p = sk.front();
        sk.pop();
        ++cnt;
        for(auto s = ret.begin(); s!=ret.end();)
        {
            if(!mp[p].count(*s))
            {
                sk.push(*s);
                ret.erase(s++);
            }
            else
                s++;
        }
    }
    return cnt;
}

void solve()
{
    vector<int> ans;
    while(!ret.empty())
    {
        int x = ret.front();
        ret.pop_front();
        ans.push_back(bfs(x));
    }
    sort(ans.begin(),ans.end());
    cout<<ans.size()<<endl;
    for(auto s:ans)
        cout<<s<<" ";
    cout<<endl;
}

int main()
{
    IO;
    cin>>n>>m;
    init();
    for(int i=0; i<m; i++)
    {
        int u,v;
        cin>>u>>v;
        mp[u][v] = true;
        mp[v][u] = true;
    }
    solve();
    return 0;
}

我想对千千说~