Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)
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;
}
												
											Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)的更多相关文章
- Codeforces Round #599 (Div. 2)D 边很多的只有0和1的MST
		
题:https://codeforces.com/contest/1243/problem/D 分析:找全部可以用边权为0的点连起来的全部块 然后这些块之间相连肯定得通过边权为1的边进行连接 所以答案 ...
 - Codeforces Round #599 (Div. 2)
		
久违的写篇博客吧 A. Maximum Square 题目链接:https://codeforces.com/contest/1243/problem/A 题意: 给定n个栅栏,对这n个栅栏进行任意排 ...
 - Codeforces Round #599 (Div. 2) E. Sum Balance
		
这题写起来真的有点麻烦,按照官方题解的写法 先建图,然后求强连通分量,然后判断掉不符合条件的换 最后做dp转移即可 虽然看起来复杂度很高,但是n只有15,所以问题不大 #include <ios ...
 - Codeforces Round #599 (Div. 2) A,B1,B2,C 【待补 D】
		
排序+暴力 #include<bits/stdc++.h> using namespace std; #define int long long #define N 1005000 int ...
 - Codeforces Round #599 (Div. 2)的简单题题解
		
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
 - Codeforces Round #599 (Div. 2) C. Tile Painting
		
Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to ...
 - Codeforces Round #599 (Div. 2)  B2. Character Swap (Hard Version)
		
This problem is different from the easy version. In this version Ujan makes at most 2n2n swaps. In a ...
 - Codeforces Round #599 (Div. 2) Tile Painting
		
题意:就是给你一个n,然后如果 n mod | i - j | == 0 并且 | i - j |>1 的话,那么i 和 j 就是同一种颜色,问你最大有多少种颜色? 思路: 比赛的时候,看到 ...
 - Codeforces Round #599 (Div. 1) C. Sum Balance 图论 dp
		
C. Sum Balance Ujan has a lot of numbers in his boxes. He likes order and balance, so he decided to ...
 
随机推荐
- 如何最快实现物流即使查询功能-物流轨迹查询API
			
上一篇文章我们介绍了一个物流服务提供商,推荐大家使用快递鸟接口,主要介绍了如何注册账号,获得密钥,找不到注册地址的,我在发一下: http://kdniao.com/reg 今天我们来聊如何利用快递鸟 ...
 - VUE报表开发
			
因为在项目中经常开发一些报表,并且业务.逻辑其实都有大部分的重复部分. 所以将这些常用的模块抽象出来.并且可视化操作.封装成一款报表开发工具. 先看一下项目的一些效果:数据单项绑定 可视化操作: 数据 ...
 - 2018icpc南京现场赛-G Pyramid(打标找规律+逆元)
			
题意: 求n行三角形中等边三角形个数,图二的三角形也算. n<=1e9 思路: 打表找下规律,打表方法:把所有点扔坐标系里n^3爆搜即可 打出来为 1,5,15,35,70,126,210.. ...
 - SpringBoot之ApplicationRunner接口和@Order注解
			
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了ApplicationRunner接口来帮助我们实现这种需求.该接 ...
 - asp.net core系列 WebAPI 作者:懒懒的程序员一枚
			
asp.net core系列 36 WebAPI 搭建详细示例一.概述1.1 创建web项目1.2 添加模型类1.3 添加数据库上下文1.4 注册上下文1.5 添加控制器1.6 添加Get方法1.7 ...
 - html标签学习入门   随笔
			
Html学习入门 随笔1: HTML 标题 HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 标题仅用于标题文本 不应该被使用在加粗字 ...
 - Thread类的interrupted方法和isInterrupted方法的区别
			
如下所示,interrupted()会改变线程的中断状态(清除),而isInterrupted()不影响线程的中断状态 /** * Tests whether the current thread ...
 - python使用turtle库绘制奥运五环
			
效果图: #奥运五环 import turtle turtle.setup(1.0,1.0) #设置窗口大小 turtle.title("奥运五环") #蓝圆 turtle.pen ...
 - ASP.NET MVC5+EF6+EasyUI 后台管理系统--网页版本代码生成器
			
1.单列表模式 2.树形列表模式 3.左右列表模式 4.左右树形和列表结合模式 一 简介 网页版代码生成器需要运行项目,非常有趣,可以用来研究,和自定义一些自己的代码习惯 按界面生成:可生成单个页面和 ...
 - 数据算法  --hadoop/spark数据处理技巧    --(15.查找、统计和列出大图中的所有三角形  16.k-mer计数)
			
十五.查找.统计和列出大图中的所有三角形 第一步骤的mr: 第二部mr: 找出三角形 第三部:去重 spark: 十六: k-mer计数 spark: