Codeforces Round #599 (Div. 2)

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≤105, 0≤m≤min(n(n−1)2,105)), 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.

input1

6 11

1 3

1 4

1 5

1 6

2 3

2 4

2 5

2 6

3 4

3 5

3 6

output1

2

题意:

给你一个n个点的完全图,其中m条边权值是1,其他边的权值是0。求出权值最小的生成树权值的大小。

题解

我们把n个点放到set容器中和建一个set容器的图,然后通过bfs暴力,求出连通块的个数,答案就是连通块的个数-1。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
set<int>G[N],s;
int vis[N];
void bfs(int x)
{
queue<int>q;
q.push(x);
s.erase(x);
while(q.size()>0)
{
int y=q.front();
q.pop();
if(vis[y])
continue;
vis[y]=1; for(auto it=s.begin();it!=s.end();)
{
int v=*it;
++it;
if(G[y].find(v)==G[y].end())
{
q.push(v);//cout<<"-";
s.erase(v);
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
s.insert(i);
}
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
G[x].insert(y);
G[y].insert(x);
}
int ans=0; for(int i=1;i<=n;i++)
{
if(!vis[i])
{
bfs(i);
ans++;
}
}
cout<<ans-1<<"\n";
return 0;
}

最新文章

  1. ALS
  2. Android 基于Android的手机邮件收发(JavaMail)之二( Welcome.java 和 ReceiveAndSend.java )
  3. 3.Git的诞生和其分布式的优点
  4. [摘录]quarts:Quartz Quick Start Guide
  5. Java线程(二):线程同步synchronized和volatile
  6. 关于byte[]字节传输的大端和小端小议
  7. iOS - 数组(NSArray)
  8. 一步步学习NHibernate(8)&mdash;&mdash;HQL查询(2)
  9. driver.startActivity 启动app出现 An unknown server-side error occurred while processing the command
  10. 学习使用GitHub(一)--之入门
  11. struts2对action中的方法进行输入校验---xml配置方式(3)
  12. 安卓巴士android源码、博文精选1
  13. final、finally、finalize
  14. django——用户认证组件
  15. Fescar: Fast &amp; Easy Commit And Rollback
  16. [转]BTC手续费计算,如何设置手续费
  17. 第8章 用SQL语句操作数据
  18. 【题解】玲珑杯河南专场17B
  19. js对用户信息加密传输 java后端解密
  20. 编写高质量代码改善C#程序的157个建议——建议130:以复数命名枚举类型,以单数命名枚举元素

热门文章

  1. 死磕java(7)
  2. C# 多态和接口
  3. 最大流-前置push-relabel算法实现
  4. git命令清单 摘自 阮老师
  5. Source Code Structure - Python 源码目录结构
  6. 07-SpringMVC01
  7. Ikuai路由安装及简单配置 v1.0
  8. openssl 自签名证书SHA1加密算法
  9. Vue路由(vue-router)
  10. FastDFS 配置文件 storage.conf