【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 ...
随机推荐
- Android开发——Android进程保活招式大全
)前台进程(Foreground process),即用户当前操作所必需的进程,通常数量不多.举例如下: //拥有用户正在交互的 Activity(已调用 onResume()) //拥有某个 Ser ...
- 汇编 do while循环
do while生成的汇编代码 do while汇编还原成C++代码 一. do while成生的汇编代码 // int i=0; // do // { // i++; // } while ( ...
- eclipse + maven + com.sun.jersey 创建 restful api
maven 创建 jersey 项目 如果没找到 jersey archetype, 下载 maven 的 archetype xml, 然后导入 archetypes 运行 右击 main.java ...
- Python魔术世界 1 如何使用Visual Studio在WIN10中一键安装Python3入门编程环境并测试Django
本文通过VS安装Python和Django的环境,创建了一个Web程序,前后5分钟的操作,让你快速入门Python的编程世界,各种Python和Django的概念会在实战中给你娓娓道来. Django ...
- numpy 初识(三)
基本运算 exp: e sqrt:开放 floor:向下取整 ravel:矩阵拉成一个向 T:转置(行和列变换) 改变形状: resize: 更改其形状(返回值为None)a.resize(6,2) ...
- 第十六次ScrumMeeting博客
第十六次ScrumMeeting博客 本次会议于12月5日(二)22时整在3公寓725房间召开,持续20分钟. 与会人员:刘畅.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的内容和链接): ...
- pycharm 调试 scrapy
http://blog.csdn.net/shijichao2/article/details/61940931
- M1事后分析报告
在得到M1团队成绩之后,每个团队都需要编写一个事后分析报告,对于团队在M1阶段的工作做一个总结. 请在2015年11月24日上课之前根据下述博客中的模板总结前一阶段的工作,发表在团队博客上,并在课上的 ...
- 剑指offer:二叉搜索树的后续遍历序列
题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 解题思路: 根据二叉搜索的性质,后序遍历是先搜索 ...
- 【助教】浅析log4j的使用
有不少童鞋私信我一些在写代码时候遇到的问题,但是无法定位问题出在哪里,也没有日志记录,实际上,写日志是开发项目过程中很重要的一个环节,很多问题都可以从日志中找到根源,从而定位到出错位置,为解决问题提供 ...