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 ...
随机推荐
- 组件state
一.设计合适的state 1.1 定义: state代表一个组件UI呈现的完整状态 stae代表一个组件UI呈现的最小状态集[所有状态都用于组件UI的变化,没有任何多余的状态] 1.2 state和p ...
- SpringMVC:拦截器
SpringMVC:拦截器 概述 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.开发者可以自己定义一些拦截器来实现特定的功能. 过 ...
- part8 详情页面动态路由以及banner布局
1.<router-link>标签 a标签就会把里面文字的颜色变掉 那我们可以换一种写法 <router-link tag='li'> //这样vue就会把这个标签渲染成li标 ...
- redis(六)---- 简单延迟队列
延迟队列的应用场景也很常见,例如:session的超时过期.自动取消未付款订单等等.redis中有一种数据结构叫做zset,即有序集合.元素类型为String类型,且元素具有唯一性不能重复,每个元素可 ...
- POJ 1035:Spell checker
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22574 Accepted: 8231 De ...
- [C/C++]'fopen': This function or variable may be unsafe
这个错误也算比较常见吧,当然这个与代码无关,是编译器的问题,主要是VS中出现的,因为微软方面认为fopen函数是不安全的,于是自己搞了一套fopen_s的函数来代替,用前面一个的话编译器是会出错的,所 ...
- .NET技术-4.0. NETCORE跨域
.NET技术-4.0. NETCORE跨域 1.安装程序CORS程序包,一般默认都带了此程序包的 Install-Package Microsoft.AspNetCore.Mvc.Cors 2.配置C ...
- POJ 3013 SPFA算法,邻接表的使用
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19029 Accepted: 4 ...
- Centos7下yum安装软件报错解决办法
Traceback (most recent call last): File "/usr/bin/yum", line 29, in yummain.user_main(sys. ...
- P3370 【模板】字符串哈希 题解
地址:https://www.luogu.org/problem/P3370 求不同字符串的数量 这题用set也可以过,但是hash更高大上嘛. 哈希其实就是将一个字符串映射成一个值,并且要让这些值不 ...