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 ...
随机推荐
- 牛客——Rabbit的字符串
题目: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 Rabbit得到了一个字符串,她的好朋 ...
- mysql查询用if控制显示列
1: select *,if(sex=1,”男”,”女”) as sex from user2: select CASE sex WHEN 1 THEN ‘男’ ELSE ‘女’ END as sex ...
- POJ 1185 状态DP
这个题目是个挺难表示的状态DP,因为不但要考虑上下还要考虑左右,在DP里面就没有什么下了咯,但也至少除了考虑左右还要考虑上 所以先枚举出在同一行满足条件的状态 即 某状态 若 s&(s< ...
- PAT-树-DFS-BFS相关问题解决方案整理
如何建树? 二叉树-建树-方式一 dfs使用root左右指针建立树节点关系,返回根节点root 二叉树-建树-方式二 dfs使用二维数组,int nds[n][2],如:nds[i][0]表示i节点的 ...
- 基于JWT的token认证机制
1. 一个JWT实际上就是一个字符串,由三部分组成 头部,载荷,签名 头部:事描述类型,签名,算法等 可以被表示成一个JSON对象 载荷:存放有效信息的地方 包含三个部分 (1)标准注册中的声明-建议 ...
- 微信获得access_token
<?php //获取access_token $appid = 'wx47a6fc3c1187e60d'; //测试账号appid $appsecret = '525f76d57c7bd7200 ...
- DAO层使用mybatis框架有关实体类的有趣细节
1.根据个人习惯,将储存那些数据库查询结果集有映射关系的实体类的Package包名有如下格式: cn.bjut.domain cn.bjut.pojo cn.bjut.model cn.bjut.en ...
- malloc 底层实现及原理
摘要:偶尔看到面试题会问到 malloc 的底层原理,今天就来记录一下,毕竟学习要“知其所以然”,这样才会胸有成竹. 注:下面分析均是基于 linux 环境下的 malloc 实现.步骤是:先总结结论 ...
- LeetCode Input Initial Code
说明 LeetCode提供的样本输入,显示上是数组Array,而后台的实际测试用例则是树TreeNode,链表ListNode等. 如果你是在页面手撸代码直接提交的,那没什么影响. 如果你是在本地ID ...
- Unity中的事件监听
Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值.如下图所示: 先以PointerClick为例.这个是用于某点点击事件.其他事件都可 ...