Link: D. 0-1 MST

D. 0-1 MST
Description: Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph.

It is an undirected weighted graph on n vertices. It is a complete graph: each pair of vertices is connected by an edge. The weight of each edge is either 0 or 1; exactly m edges have weight 1, and all others have weight 0.

Since Ujan doesn’t really want to organize his notes, he decided to find the weight of the minimum spanning tree of the graph. (The weight of a spanning tree is the sum of all its edges.) Can you find the answer for Ujan so he stops procrastinating?

Input
The first line of the input contains two integers n and m (1≤n≤1e5, 0≤m≤min(n*(n−1)/2,1e5)), the number of vertices and the number of edges of weight 1 in the graph.

The i-th of the next m lines contains two integers ai and bi (1≤ai,bi≤n, ai≠bi), the endpoints of the i-th edge of weight 1.

It is guaranteed that no edge appears twice in the input.

Output
Output a single integer, the weight of the minimum spanning tree of the graph.

Examples
input
6 11
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
output
2
input
3 0
output
0
Note
The graph from the first sample is shown below. Dashed edges have weight 0, other edges have weight 1. One of the minimum spanning trees is highlighted in orange and has total weight 2.

In the second sample, all edges have weight 0 so any spanning tree has total weight 0.

Problem solving:
这道题的意思就是给你一个图,然后告诉你一些边,这些边的权值为1,其他的未给出的边的权值为0。然后问你这个图的最小生成树的权值是多少。

因为边很多,存的话会超时(1e10的复杂度)。所以我们换一种思路想一下。有一个名词叫补图的连通块。在这个题里面的意思就是找出被权值为0的边连起来的连通块个数,再减一就行了,跟hdu的畅通工程有点像。

求补图的连通块个数。我这里使用了bfs写的,还可以用并查集什么的写。(当成板子记住吧)

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
set<int> V[maxn],s;
int m,n,x,y,ans,vis[maxn];
queue<int> q;
void bfs(int x)
{
    q.push(x);
    vis[x]=1,s.erase(x);
    while(!q.empty())
    {
        int mid=q.front();q.pop();
        for(auto i=s.begin();i!=s.end();)
        {
            int mi=*i++; //这里很重要,因为我们要删除,所以需要先加上而不是在for循环里面直接i++
            if(V[mi].count(mid)==0)
            {
                s.erase(mi);
                q.push(mi);
                vis[mi]=1;
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)   s.insert(i);
    while(m--)
    {
        cin>>x>>y;
        V[x].insert(y),V[y].insert(x);
    }
    for(int i=1;i<=n;i++)   if(!vis[i]) bfs(i),ans++;
    cout<<ans-1<<endl;
    return 0;
}