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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces791 B. Bear and Friendship Condition

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  5. Codeforces 791B. Bear and Friendship Condition 联通快 完全图

    B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...

  6. 【CF771A】Bear and Friendship Condition

    题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...

  7. CodeForce-791B Bear and Friendship Condition(并查集)

    Bear Limak examines a social network. Its main functionality is that two members can become friends ...

  8. 【codeforces 791B】Bear and Friendship Condition

    [题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...

  9. CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)

    题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...

  10. Bear and Friendship Condition-HZUN寒假集训

    Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...

随机推荐

  1. cloud compare PCA插件开发详细步骤(二)附代码

    在上一节 https://blog.csdn.net/csy1021/article/details/141200135 我们已经完成了 具体开发前的准备工作,包括 各级 CMakelists.txt ...

  2. pom阿里加速镜像地址

    <repositories> <repository> <id>alimaven</id> <name>aliyun maven</n ...

  3. Python 项目及依赖管理工具技术选型

    Python 项目及依赖管理工具,类似于 Java 中的 Maven 与 Node 中的 npm + webpack,在开发和维护项目时起着重要的作用.使用适当的依赖管理工具可以显著提高开发效率,减少 ...

  4. 【YashanDB知识库】含有NUL字节的varchar字符串查询时出现截断

    [问题分类]功能使用 [关键字]NUL字符 [问题描述]数据库中插入一条含有NUL字符(\00)的字符串,使用yasql在终端进行查询,字符串从NUL处被截断,未能完整展示全部字符. [问题原因分析] ...

  5. 小tips:vue结合百度UEditor富文本编辑器实现vue-ueditor-wrap

    1.下载vue-ueditor-wrap cnpm i vue-ueditor-wrap -S 下载最新的 UEditor 资源文件放入你项目的静态资源目录中(比如 static 或者 public, ...

  6. QT硬件异构计算

    QT硬件异构计算 使用AI技术辅助生成 1 QT硬件异构计算概述 1.1 硬件异构计算概念 1.1.1 硬件异构计算概念 硬件异构计算概念 <QT硬件异构计算>正文 硬件异构计算概念 在进 ...

  7. 【赵渝强老师】MongoDB中的索引(下)

    (四)索引的类型三:复合索引(Compound Index) MongoDB支持复合索引,即将多个键组合到一起创建索引.该方式称为复合索引,或者也叫组合索引,该方式能够满足多键值匹配查询使用索引的情形 ...

  8. 五行八字在线排盘api接口免费版_json数据格式奥顺互联内部接口

    「八字在线排盘」谁都想知道自己一生中的事业.财运.婚姻.功名.健康.性格.流年运程将是怎样,通过八字排盘,四柱八字排盘会有你想知道的答案.一个人出生的年月时天干地支的排列组合(即八字)就是命.不过仅凭 ...

  9. WPF中的ListBox怎么添加删除按钮并删除所在行

    直接上代码: 第一步:创建测试类 public class BeautifulGirl { public string Name { get; set; } } 第二步:创建viewmodel和数据源 ...

  10. 分析ueventd Coldboot耗时问题

    安卓go平台启动时间发现如下ueventd耗时1.907s问题: 01-11 00:20:02.854 0 0 I init : Parsing file /odm/etc/init... 01-11 ...