题解: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 ...
随机推荐
- 到底什么是Cortex、ARMv8、arm架构、ARM指令集、soc?一文帮你梳理基础概念【科普】
前言 有粉丝问我到底什么是ARM,搞不清楚Cortex.arm内核.arm架构.ARM指令集.soc这些概念都是什么关系,下面一口君给大家整理一下关于ARM相关的一些概念. 1.ARM既可以认为是一个 ...
- Go 进程在容器中无 coredump 产生问题分析
Go 进程在容器中无 coredump 产生问题分析 0x01 起因 coredump 作为一种非常重要的高度手段,在日常开发中经常用到,切换到容器环境后一直没关注.最近测试了下,发现出不了 core ...
- Ubuntu 16.04 安装 python3.8
Ubuntu 16.04 amd64 (64bit)(纯净版) 自带python2.7和python3.5 执行"whereis python"查看当前安装的python [ro ...
- Ubuntu 安装 libwebkitgtk-1.0-0
在 Ubuntu 上安装完 PDI 后启动 spoon.sh 时提示安装 libwebkitgtk-1.0-0.由于 apt 官方源中不包含此软件包,因此要添加该软件包的源,以及源对应的 gpg 公钥 ...
- 各种好用的免费快递物流API 接口分享
全国快递物流查询:1.提供包括申通.顺丰.圆通.韵达.中通.汇通等 600+快递公司在内的快递物流单号查询.2.与官网实时同步更新.3.自动识别快递公司. 全国快递物流地图轨迹查询:[H5 物流轨迹. ...
- 淘宝订单信息获取接口API,淘宝打单发货接口
从事电商软件开发的小伙伴,在日常开发任务中,经常会遇到一个需求,就是将淘宝店铺的订单,同步到自己的内部订单管理系统OMS中,进行淘宝打单发货操作.我介绍下如何将订单同步下来,供各位参考.(注意:所有电 ...
- 安装Kubernetes(k8s)保姆级教程---无坑版
一.安装环境说明 硬件要求 内存:2GB或更多RAM CPU: 2核CPU或更多CPU 硬盘: 30GB或更多 本次环境说明: 操作系统:CentOS 7.9 内核版本:3.10.0-1160 mas ...
- ansible rpm包下载
Ansible2.9.18版本下载链接:https://pan.baidu.com/s/1dKlwtLWSOKoMkanW900n9Q 提取码:ansi 将软件上传至系统并解压安装: # tar -z ...
- Angular 18+ 高级教程 – Getting Started
前言 这篇主要是教大家如何快速搭建一个 Angular 项目,纯用于学习. Before Starting 开始前,我们需要知道几个小知识. 1. Angular Compilation 游览器支持的 ...
- 关于BarchNorm的一些学习
<Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift> ...