0-1 MST

0-1 MST

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 mm 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≤10^5, 0≤m≤min(n*(n−1)/2,10^5)), 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.

思路

这道题很明显的意思,最小生成树嘛。关键在于未连接的边可以算作权值为0的边,导致这道题变得复杂起来了,这里给出两种解法:
1.kruskal算法,按照该算法的步骤,我们进行缩点,每次选出一个没有加入的点,dfs找到所有没有与改点加入的点,缩成一个点并标记,继续寻找,最后剩下ans个点,那么长度就为ans-1;
2.我们反过来思考,将所有点加入到优先队列,按照入度排序,对于入度最大的点,如果它的入度为剩下所有的点,那么没有一条路径边权为0,我们就ans++,并将改点从图中删除,所有与改点相连的点的入度-1,最后剩下一个点的时候跳出即可。
总的来说,两种贪心的策划不一样,第一种是比较常见的kruskal算法, 进行缩点操作,第二种则是另一个角度的贪心,优先判断可选边权为0数量少的点开始。

具体代码如下:

第一种
代码如下:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=2e5+10;
set<int>s[mx];//记录边,用set可以直接count判断是否在边内,复杂度logn,不用n查找
set<int>f;//记录还有哪些点没有加入 
int vis[mx];//标记每个点是否加入 
int n,m,ans;
 
void dfs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值