题解: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 ...
随机推荐
- 异常处理,内置方法(__new__,__init__,__del__析构方法,单例模式,item系列)
__new__ 创建一个对象 class A: def __init__(self): print('in init') def __new__(cls): print('in new') self= ...
- DDD是软件工程的第一性原理?
本文书接上回<DDD建模后写代码的正确姿势>,关注公众号(老肖想当外语大佬)获取信息: 最新文章更新: DDD框架源码(.NET.Java双平台): 加群畅聊,建模分析.技术实现交流: 视 ...
- Java 读取 IP 地址
使用 InetAddress 类 可以利用 Java 自带的 InetAddress 类来检查一个字符串是否为有效的 IP 地址: import java.net.InetAddress; // 导入 ...
- 10 Python面向对象编程:类和对象以及和Java的对比
本篇是 Python 系列教程第 10 篇,更多内容敬请访问我的 Python 合集 这里只介绍类和对象,self.属性.方法.访问控制.类继承.方法重写在后面的文章里介绍 在Python中,类和对象 ...
- 几步轻松定制私人AI助手
这两年大模型的发展持续火热,以至于许多资本和学者认为AI出现了泡沫,根本原因还是因为大模型目前还没有出现能够结合行业切实落地的应用. 我才不关注泡沫不泡沫呢,我只关注大模型能给我带来哪些帮助即可.大模 ...
- 支付宝小程序swiper video的坑
在使用uniapp 开发多端应用的时候,踩了一个坑,有一个页面,是使用swiper作为滑动容器,然后每个swiper-item 嵌套不同的内容, 代码示意: <template> < ...
- MRO, mixin的解读
本文试着将python中类继承中方法解析顺序MRO(method resolution order)和mixin梳理清楚 先MARK 类的继承中,super()的使用还是蛮多讲究的,因为看pytorc ...
- C++ 数据输入cin (解决CLoin输入中文程序出错)
数据输入cin 语法:cin >> 变量 解决 CLoin 使用cin输入中文程序无法正常运行 按住Ctrl+alt+shift+/键 弹出对话框选择注册表 取消勾选run.process ...
- 全网最适合入门的面向对象编程教程:53 Python 字符串与序列化-字符串与字符编码
全网最适合入门的面向对象编程教程:53 Python 字符串与序列化-字符串与字符编码 摘要: 在 Python 中,字符串是文本的表示,默认使用 Unicode 编码,这允许你处理各种字符集,字符编 ...
- [python] 基于PyOD库实现数据异常检测
PyOD是一个全面且易于使用的Python库,专门用于检测多变量数据中的异常点或离群点.异常点是指那些与大多数数据点显著不同的数据,它们可能表示错误.噪声或潜在的有趣现象.无论是处理小规模项目还是大型 ...