0-1MST

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 nn vertices. It is a complete graph: each pair of vertices is connected by an edge. The weight of each edge is either 00 or 11; exactly mm edges have weight 11, and all others have weight 00.

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 nn and mm (1≤n≤1051≤n≤105, 0≤m≤min(n(n−1)2,105)0≤m≤min(n(n−1)2,105)), the number of vertices and the number of edges of weight 11 in the graph.

The ii-th of the next mm lines contains two integers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n, ai≠biai≠bi), the endpoints of the ii-th edge of weight 11.

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 00, other edges have weight 11. One of the minimum spanning trees is highlighted in orange and has total weight 22.

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

【思路】

要求最小生成树,如果考虑有0 边相连的点作为一个集合的话,那么就是求集合的个数。

求补图的连通块数减一,n个连通块需要n-1条边连接。

用set维护所有点,先set存所有点。

每次循环,如果当前的这个点没有被染色,那么就从这个点开始bfs,找到所有当前点不能到的点,这些点是肯定能用长度为0的边连接的,可以划分为一块。然后删除这些块中的点。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m,cnt,vis[N];
set<int>st,g[N];
void bfs(int s,int col)
{
	queue<int> q;
	q.push(s);
	st.erase(s);
	while(q.size())
    {
		int u=q.front();
		q.pop();
		vector<int>v;
		vis[u]=col;
		for(set<int>::iterator it=st.begin();it!=st.end();it++)
        {
			if(g[u].find(*it)==g[u].end())
			{
			    v.push_back(*it);
			    q.push(*it);
			}
		}
		for(int i=0;i<v.size();i++)
		{
		    st.erase(v[i]);
		    vis[v[i]]=col;
		}
	}
}
int main()
{
    ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1,a,b;i<=m;i++)
    {
        cin>>a>>b;
        g[a].insert(b);
        g[b].insert(a);
    }
	for(int i=1;i<=n;i++)
	st.insert(i);
	for(int i=1;i<=n;i++)
	if(!vis[i])
	bfs(i,++cnt);
	cout<<cnt-1<<endl;
	return 0;
}

代码是copydalao 的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值