题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边。问这张图是否满足要求。

题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系,那么 A 和 C 也要有关系,因此可以采用并查集加以维护,维护关系的同时顺便维护各个联通块的大小,若符合题目要求,则同一个联通块中的点必须均有关系。因此,最后计算一下每个联通块的应有关系数和最初所给的关系数比较,相等则符合,反之,不符合。

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn=150010; inline int read(){
int x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
} int n,m,f[maxn],size[maxn];
long long tot;
bool vis[maxn]; int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
void merge(int x,int y){
x=find(x),y=find(y);
if(x==y)return;
if(size[x]>size[y])swap(x,y);
f[x]=y,size[y]+=size[x];
} void read_and_parse(){
n=read(),m=read();
for(int i=1;i<=n;i++)f[i]=i,size[i]=1;
} void solve(){
int T=m;
while(T--){
int x=read(),y=read();
merge(x,y);
}
for(int i=1;i<=n;i++)if(!vis[find(i)]){
vis[find(i)]=1;
tot+=(long long)size[find(i)]*(size[find(i)]-1)/2;
}
puts(tot==m?"YES":"NO");
} int main(){
read_and_parse();
solve();
return 0;
}

【CF771A】Bear and Friendship Condition的更多相关文章

  1. 【codeforces 791B】Bear and Friendship Condition

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

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

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

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

  6. 【CF573D】Bear and Cavalry 线段树

    [CF573D]Bear and Cavalry 题意:有n个人和n匹马,第i个人对应第i匹马.第i个人能力值ai,第i匹马能力值bi,第i个人骑第j匹马的总能力值为ai*bj,整个军队的总能力值为$ ...

  7. 【CF679D】Bear and Chase 最短路+乱搞

    [CF679D]Bear and Chase 题意:近日,鼠国的头号通缉犯,神出鬼没的怪盗——Joker正于摩登市出没!对于名侦探Jack来说,这正是将其捉拿归案的大号时机.形式化地,摩登市可以看成一 ...

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

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

  9. 【CF573E】Bear and Bowling

    [CF573E]Bear and Bowling 题面 洛谷 题解 首先有一个贪心的结论: 我们一次加入每个数,对于\(\forall i\),位置\(i\)的贡献为\(V_i = k_i\times ...

随机推荐

  1. 20155229《网络对抗技术》Exp6:信息收集与漏洞扫描

    实验内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务的查点 (4)漏洞扫描:会扫,会看报告,会查漏洞说明,会修补 ...

  2. [原创]STM32 BOOT模式配置以及作用

    一.三种BOOT模式介绍 所谓启动,一般来说就是指我们下好程序后,重启芯片时,SYSCLK的第4个上升沿,BOOT引脚的值将被锁存.用户可以通过设置BOOT1和BOOT0引脚的状态,来选择在复位后的启 ...

  3. LeetCode Letter Combinations of a Phone Number (DFS)

    题意 Given a digit string, return all possible letter combinations that the number could represent. A ...

  4. CodeFirst从零搭建ASP.NETCore2.0

    没时间介绍了,废话不说先上车 以下所有扯淡都是建立在.NETCore2.0环境已经搭建好 右键解决方案>新建项目> 选择Web>ASP.NETCoreWeb应用程序(.NET Cor ...

  5. Python对Selenium调用浏览器进行封装包括启用无头浏览器,及对应的浏览器配置文件

    """ 获取浏览器 打开本地浏览器 打开远程浏览器 关闭浏览器 打开网址 最大化 最小化 标题 url 刷新 Python对Selenium封装浏览器调用 ------b ...

  6. Ubuntu16.4下QT配置opencv3.1+FFmpeg

    安装依赖环境 sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config ...

  7. 11.5 Daily Scrum

    请把现在当成11月5日······   Today's tasks  Tomorrow's tasks 丁辛 餐厅列表数据结构设计 餐厅列表UI设计             李承晗           ...

  8. ios UnitTest 学习笔记

    一.运行第一个单元测试: 1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestC ...

  9. 第二个Sprint冲刺第三天(燃尽图)

  10. 基于RYU的拓扑发现

    基于RYU的拓扑发现 前言 本次实验是一个基于RYU的拓扑发现功能.参考了呈神的实现方式,并加了一些自己实现方式,做了一些数据结构的改动. 数据结构 link_to_port 字典 有两种关系: 一是 ...