题解: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 ...
随机推荐
- 卷积神经网络CNN实战:MINST手写数字识别——网络定义
本文基于python-pytorch框架,编写CNN网络,并采用CNN手写数字数据集训练.测试网络. 网络的构建 以LeNet-5 网络为例 类定义 首先先了解一下网络的最基本框架 一般而言,首先创建 ...
- 【Python自动化】之利用JS操作页面滚动条(新)
如何操作页面中的滚动条,有以下几个方法: 一.查找可见元素进行滚动页面(通用) 方法一: ①移动到元素element对象的"顶端",与当前窗口的"顶部"对齐(默 ...
- Docker的数据卷与数据卷容器操作
一.数据卷 数据卷(Data Volumes)是一个可供容器使用的特殊目录,它将主机操作系统目录直接映射进容器,类似于Linux中的mount动作. 数据卷可以提供很多有用的特性: 数据卷可以在容器之 ...
- PlugIR:开源还不用微调,首尔大学提出即插即用的多轮对话图文检索 | ACL 2024
即插即用的PlugIR通过LLM提问者和用户之间的对话逐步改进文本查询以进行图像检索,然后利用LLM将对话转换为检索模型更易理解的格式(一句话).首先,通过重新构造对话形式上下文消除了在现有视觉对话数 ...
- WebShell流量特征检测_蚁剑篇
80后用菜刀,90后用蚁剑,95后用冰蝎和哥斯拉,以phpshell连接为例,本文主要是对这四款经典的webshell管理工具进行流量分析和检测. 什么是一句话木马? 1.定义 顾名思义就是执行恶意指 ...
- lxml官方入门教程(The lxml.etree Tutorial)翻译
lxml官方入门教程(The lxml.etree Tutorial)翻译 说明: 首次发表日期:2024-09-05 官方教程链接: https://lxml.de/tutorial.html 使用 ...
- c++ push_back()和emplace_back()区别
c++ push_back()和emplace_back()区别 References C++中push_back和emplace_back的区别 push_back v.s. emplace_bac ...
- Webpack4-使用expose-loader将变量注册到全局
首先贴一下 package.json中的插件版本,不同的版本也许会有很大的差异: // package.json{ "name": "WEBPACK-DEV-1" ...
- 中文关键字检索分析-导出到csv或者excel-多文件或文件夹-使用python和asyncio和pandas的dataframe
1.02版本 把原来的tab一个个拼接成文件输出,改成pandas的dataframe 使用asyncio库来使用协程,但是测试下来速度好像是差不多的.可能速度太快了,没能很好的测出来差异. 原来的最 ...
- Angular 18+ 高级教程 – Component 组件 の Angular Component vs Custom Elements
前言 在上一篇 Angular Component vs Web Component 中,我们整体对比了 Angular Component 和 Web Component 的区别. 这一篇我们将针对 ...