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;
}

原文地址:https://www.cnblogs.com/hh13579/p/11809744.html

时间: 2024-07-31 05:35:39

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的边进行连接 所以答案就是这些块的总数-1: #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back const int M=1e5+5; set<int>s,g[M]; int vis[

Codeforces Round #599 (Div. 2)

A - Maximum Square 题意:给 \(n\) 块宽度为 \(1\) 长度为 \(a_i\) 的木板,把这些木板拼在一起,求最大形成的正方形的边长. 题解:贪心,从大到小排序,然后找第一个满足 \(a_i<i\) 的位置break掉. #include<bits/stdc++.h> using namespace std; typedef long long ll; int n, a[1005]; int main() { #ifdef KisekiPurin freopen

Codeforces Round #599 (Div. 2) Tile Painting

题意:就是给你一个n,然后如果  n mod | i - j | == 0  并且 | i - j |>1 的话,那么i 和 j 就是同一种颜色,问你最大有多少种颜色? 思路: 比赛的时候,看到直接手推,发现有点东西,直接打表找出了规律 —— 如果 n的质因子只有一个,那么总数就是 那个 质因子.其它都为 1. 今天上课的时候无聊,还是试着推了一下原理. 1.如果一个数只有一个质因子 x ,那么  n-x .n-2x.n-3x ……等等全为一种颜色,也就是说每隔 x个就是同种颜色,这样的话就是有

C. Tile Painting (定理:任意一个合数都能够写成两个质数的乘积) 《Codeforces Round #599 (Div. 2) 》

Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of nn consecutive tiles, numbered from 11 to nn. Ujan will paint each tile in some color

Codeforces Round #599 (Div. 2) A. Maximum Square

Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai). Now, Ujan wants to make a square roof. He will first choos

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 paint the path from his house to the gate. The path consists of nn consecutive tiles, numbered from 11 to nn. Ujan will paint each tile in some color

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 addition, k≤1000,n≤50k≤1000,n≤50 and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previou

Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems. After struggling and failing many times, Ujan decided to try to clean up his house again. He

Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造

链接:https://www.luogu.com.cn/problem/CF1243B2 题意:给你长度为n的两个字符串s和t,你可以最多进行2*n次操作,每次操作选择i和j,然后交换s[i]和t[j],问你能否使得两个字符串相同 构造方法:假如(0~i)部分s和t已经相等,在i位置时首先在(i+1~t.size()-1)里找有没有和t[i]相同的字符,如果找到,则交换s[i]和t[j],如果找不到,则在s中找,找到之后,先将s[j]与t[t.size()-1],交换,再将s[i]与t[t.si