【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 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的更多相关文章
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 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 ...
- 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 ...
- 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 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 ...
- 【CF573D】Bear and Cavalry 线段树
[CF573D]Bear and Cavalry 题意:有n个人和n匹马,第i个人对应第i匹马.第i个人能力值ai,第i匹马能力值bi,第i个人骑第j匹马的总能力值为ai*bj,整个军队的总能力值为$ ...
- 【CF679D】Bear and Chase 最短路+乱搞
[CF679D]Bear and Chase 题意:近日,鼠国的头号通缉犯,神出鬼没的怪盗——Joker正于摩登市出没!对于名侦探Jack来说,这正是将其捉拿归案的大号时机.形式化地,摩登市可以看成一 ...
- Codeforces 791B. Bear and Friendship Condition 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- 【CF573E】Bear and Bowling
[CF573E]Bear and Bowling 题面 洛谷 题解 首先有一个贪心的结论: 我们一次加入每个数,对于\(\forall i\),位置\(i\)的贡献为\(V_i = k_i\times ...
随机推荐
- WPF编程,自定义鼠标形状的一种方法。
原文:WPF编程,自定义鼠标形状的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details/8727 ...
- mfc CSpinButton
知识点: CSliderCtrl(滑块)控件 CSliderCtrl常用属性 CSliderCtrl类常用成员函数 CSliderCtrl运用示例 一.CSliderCtr常用属性 Orientati ...
- python 回溯法 子集树模板 系列 —— 2、迷宫问题
问题 给定一个迷宫,入口已知.问是否有路径从入口到出口,若有则输出一条这样的路径.注意移动可以从上.下.左.右.上左.上右.下左.下右八个方向进行.迷宫输入0表示可走,输入1表示墙.为方便起见,用1将 ...
- python3获取主机名、主机IP
python3可以通过socket模块获取主机名及主机IP 代码如下: *********************************************************** 学习永远 ...
- JS基础内容小结(DOM&&BOM)(二)
元素.childNodes:只读 属性 子节点列表集合 元素.nodeType:只读 属性 当前元素下的节点类型 元素.attributes : 只读 属性 属性列表集合 元素.children: 只 ...
- Asp.Net_Mvc3.5语法_<%%>的用法
一. <%%>这种格式实际上就是和asp的用法一样的,只是asp中里面是vbscript或 者javascript代码,而在asp.net中用的是.net平台下支持的语言.特别 注意:服务 ...
- CMake与MSVC工程化实践
CMake与MSVC工程化实践 CMake基础 cmake无疑是最流行的c++跨平台构建工具之一,关于cmake入门指南这里不再赘述,官方文档是最好的参考,这里通过一个例子简述构建一个工程常用的函数和 ...
- 1分钟,通过docker-compose 搭建zookeeper 集群
一.创建三节点 zookeeper 集群 将 docker-compose.yml 保存到当前命令行目录下 docker-compose.yml 文件 version: '2' networks: z ...
- ElasticSearch 2 (17) - 深入搜索系列之部分匹配
ElasticSearch 2 (17) - 深入搜索系列之部分匹配 摘要 到目前为止,我们介绍的所有查询都是基于完整术语的,为了匹配,最小的单元为单个术语,我们只能查找反向索引中存在的术语. 但是, ...
- python项目离线环境配置指南
参考文献: http://blog.csdn.net/candcplusplus/article/details/52156324 https://www.cnblogs.com/michael-xi ...