题解:CF771A Bear and Friendship Condition
CF771A Bear and Friendship Condition 题解
算法
并查集,图的基本性质
分析
题目意思是,一旦有一些点联通,那么这些点必须两两直接相连。换句话讲,就是图中的每个联通块都是完全图。
所谓完全图,就是图中的每个点都两两相连,假设一个完全图有 \(n\) 个点,那么我们可以通过乘法原理算出这个完全图的边数为 \(\frac{n\times(n-1)}{2}\)。那么我们现在只需要用并查集统计一下联通块个数,顺便统计一下每个联通块的点数,算出来所有完全图的边数之和与 \(m\) 比较即可。
注意要开 long long。
示例代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace Ryan
{
const int N=150005;
int n,m;
int fa[N],sum[N],vis[N];
int find(int x)
{
return (x==fa[x]?x:fa[x]=find(fa[x]));
}
void merge(int x,int y)
{
int ff=find(x);
int fy=find(y);
fa[ff]=fy;
sum[fy]+=sum[ff];
return;
}
signed work()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
fa[i]=i,sum[i]=1;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
if(find(x)!=find(y))
merge(x,y);
}
int k=0,ans=0;
for(int i=1;i<=n;i++)
{
int x=find(i);
if(vis[x])continue;
vis[x]=1;
k+=sum[x]*(sum[x]-1)/2;
ans++;
}
if(k==m)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
return Ryan::work();
}
题解:CF771A Bear and Friendship Condition的更多相关文章
- codeforces round #405 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...
- Codeforces 791B Bear and Friendship Condition(DFS,有向图)
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- Codeforces791 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces 791B. Bear and Friendship Condition 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
- Bear and Friendship Condition-HZUN寒假集训
Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...
随机推荐
- 虚拟文件系统VFS-片段一
文件系统类型 基于磁盘的文件系统 如FAT.EXT4 虚拟文件系统 如proc 网络文件系统 顾名思义,网络文件系统还将网络通信封装起来,这意味可以直接通过通信访问另一台设备的文件系统. man fs ...
- 搭建QT开发环境
下载 Qt官网,Qt下载网址 安装前要登录账号,其他的该咋就咋样,路径不能有中文. 组件自己选 我的是MinGW.Android.虚拟键盘.Qt脚本.Qt Creator 然后创个项目,能跑起来就是安 ...
- c++学习笔记(三):函数++
函数PLUS 函数默认参数 在c++中,函数的形参列表中的形参是可以有默认值的.调用函数时,如果未传递参数的值(传入参数为空),则会使用默认值,如果指定了值,则会忽略默认值,使用传递的值. 语法:返回 ...
- RuleLinKClient - 再也不担心表达引擎宕机了
原来有这么多时间 六月的那么一天,天气比以往时候都更凉爽,媳妇边收拾桌子,边漫不经心的对我说:你最近好像都没怎么阅读了. 正刷着新闻我,如同被一记响亮的晴空霹雳击中一般,不知所措.是了,最近几月诸事凑 ...
- 小tips:...运算符(展开运算符、剩余操作符)
如下例子: 1. var set = new Set([1, 2, 3, 4, 4,4,4,4,2,2,2]) set=[...set] 2. let [head, ...tail] = [1, 2, ...
- ubuntu22.04安装DBeaver
要在 Ubuntu22.04 上安装 DBeaver,可以选择使用 Ubuntu 软件中心的图形界面方法或使用命令行方法通过官方 DBeaver 仓库或 Snap 包安装. 方法一:从官方仓库安装 D ...
- [TK] 理想的正方形
题目描述 有一个整数组成的矩阵,现请你从中找出一个指定边长的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 题目分析 其实这道题和滑动窗口很像,而滑动窗口使用优先队列解决. 我们都知道优先队 ...
- CSP2024-S 游记
9-21 今天考完了初赛,明显感觉数学门槛变高了一些,有高中数学知识才能保证看得懂题意,只是苦了小学和初中同学,看数据参加人数还涨了50%,权当拉低分数线了吧.用小图灵估分70.应该是稳过.
- Python 潮流周刊#71:PyPI 应该摆脱掉它的赞助依赖(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- 浏览器中生成 OSS 令牌 | Web Crypto API
笔者写文章的时候,都会把图片通过自己搭建的一个简单站点 https://imgbed.sugarat.top/ 把图片上传到各种云的对象存储服务(OSS)上. 然后通过CDN访问,保证图片有可靠的访问 ...