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+;
set<int>s,g[M];
int vis[M];
void bfs(int x){
queue<int>que;
que.push(x);
s.erase(x);
while(!que.empty()){
int u=que.front();
que.pop();
if(vis[u])
continue;
vis[u]=;
set<int>::iterator it;
for(it=s.begin();it!=s.end();){
int v=*it;
it++;
if(g[u].find(v)==g[u].end()){///如果这一次找不到的话,就说明这个块中的点有流向它的权值为1的边
que.push(v);
s.erase(v); }
}
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
s.insert(i);
for(int x,y,i=;i<=m;i++){
scanf("%d%d",&x,&y);
g[x].insert(y);
g[y].insert(x);
}
int ans=;
for(int i=;i<=n;i++){
if(!vis[i]){
ans++;
bfs(i);
}
}
printf("%d\n",ans-);
}
Codeforces Round #599 (Div. 2)D 边很多的只有0和1的MST的更多相关文章
- 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 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- 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. 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 ...
- Codeforces Round #599 (Div. 1) B. 0-1 MST 图论
D. 0-1 MST Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math ...
- Codeforces Round #599 (Div. 1) A. Tile Painting 数论
C. Tile Painting Ujan has been lazy lately, but now has decided to bring his yard to good shape. Fir ...
- Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version) 构造
B2. Character Swap (Hard Version) This problem is different from the easy version. In this version U ...
- Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version) 水题
B1. Character Swap (Easy Version) This problem is different from the hard version. In this version U ...
随机推荐
- ADO多线程数据库查询
{ADO查询多线程单元} unit ADOThread; interface uses Classes,StdCtrls,ADODB; type TADOThread = class(TThread) ...
- request对象和response对象的作用和相关方法
response对象(响应) 响应行 状态码 :setStatus(int a) 设置状态码 302重定向 304控制缓存 响应头 setHeader() 一个key对应一个value addHead ...
- HTML5 可缩放矢量图形(1)—SVG基础
参考文档1 SVG基础 SVG介绍 概念:SVG 是使用 XML 来描述二维图形和绘图程序的语言.(理解就是一个在网页上使用笔画图的过程) 什么是SVG SVG 指可伸缩矢量图形 (Scalable ...
- 01Java-方法
一:动手动脑 1.编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数 package reserve; import java.util.Scanner; import java.ut ...
- Activity的Launch mode详解 :standard(默认), singleTop, singleTask和 singleInstance
本文参考了此文http://hi.baidu.com/amauri3389/blog/item/a54475c2a4b2f040b219a86a.html 另附 android task与back s ...
- Paper Review: Epigenetic Landscape, Cell Differentiation 01
Today, I'll share a review papers about Epigenetic Landscape, the Epigenetic Landscape is related to ...
- python thrift demo
简介Thrift最初由Facebook研发,主要用于各个服务之间的RPC通信,支持跨语言,常用的语言比如C++, Java, Python, PHP, Ruby, Erlang, Perl, Hask ...
- java读取本地json数组并解析
1.本地json位置 2,json数据 {"garbages":[{"id":"/m/011k07","ename":& ...
- 关于maven的使用总结
maven介绍 项目构建过程 eclipse只是开发工具,虽然提供了创建.编码.编译.测试.运行等功能,但并不是项目构建工具. 项目构建主要过程如下: 实际的项目构建过程要复杂繁琐的多.如果是一个独立 ...
- MySql索引机制
第一部分 MySQL数据库索引的数据结构及算法理论 第二部分 MySQL索引实现机制 第三部分 MySQL中高性能使用索引的策略 数据结构及算法 MySQL官方对索引的定义为:索引(Index)是帮助 ...