Bear and Friendship Condition
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures).
There are n members, numbered 1 through n. m pairs of members are friends. Of course, a member can't be a friend with themselves.
Let A-B denote that members A and B are friends. Limak thinks that a network is reasonable if and only if the following condition is satisfied: For every three distinct members (X, Y, Z), if X-Y and Y-Z then also X-Z.
For example: if Alan and Bob are friends, and Bob and Ciri are friends, then Alan and Ciri should be friends as well.
Can you help Limak and check if the network is reasonable? Print "YES" or "NO" accordingly, without the quotes.
Input
The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000, ) — the number of members and the number of pairs of members that are friends.
The i-th of the next m lines contains two distinct integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Members ai and bi are friends with each other. No pair of members will appear more than once in the input.
Output
If the given network is reasonable, print "YES" in a single line (without the quotes). Otherwise, print "NO" in a single line (without the quotes).
Examples
input
4 3
1 3
3 4
1 4
output
YES
input
4 4
3 1
2 3
3 4
1 2
output
NO
input
10 4
4 3
5 10
8 9
1 2
output
YES
input
3 2
1 2
2 3
output
NO
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
int pre[];
int Find(int x){
return x==pre[x]?x:pre[x]=Find(pre[x]);
}
void mix(int x,int y){
int xx=Find(x),yy=Find(y);
if(xx!=yy){
pre[xx]=yy;
}
}
LL t[];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
pre[i]=i;
}
for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
mix(x,y);
}
for(int i=;i<=n;i++){
t[Find(i)]++;
}
LL sum=;
for(int i=;i<=n;i++){
if(t[i]!=&&t[i]!=){
sum+=(t[i]*(t[i]-)/);
}
}
if(sum!=m){
printf("NO\n");
}
else{
printf("YES\n");
}
return ;
}
Bear and Friendship Condition-HZUN寒假集训的更多相关文章
-
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 ...
-
Codeforces 791B. Bear and Friendship Condition 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
-
CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
-
CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
-
食物链-HZUN寒假集训
食物链 总时间限制: 1000ms 内存限制: 65536kB 描述 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动 ...
-
【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
-
【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
随机推荐
-
redis总结
redis总结 redis与memcached redis支持更多的数据结构 redis支持数据持久化 redis支持两种存储方式:snapshot(快照)和aof(append only mode) ...
-
Dynamic CRM 2013学习笔记(三十四)自定义审批流5 - 自动邮件通知
审批过程中,经常要求自动发邮件:审批中要通知下一个审批人进行审批:审批完通知申请人已审批完:被拒绝后,要通知已批准的人和申请人.下面详细介绍如何实现一个自动发邮件的插件: 1. 根据审批状态来确定 ...
-
ectouch第五讲 之表
Ectouch本身相关的表 17个ecs_touch_activity[touch优惠活动扩展表] 优惠活动的自增id 取值ecs_favourable_activity表cat_id,给优惠活动加b ...
-
cocos2d-x读取xml(适用于cocos2d-x 2.0以上版本号)
为了能在cocos2d-x的文本标签中显示中文,一个是转换文件编码格式,还有一种就是读取utf-8格式的xml文件.我选择了后者,其原因大家可以去搜索一下cocos2d-x显示中文,希望可以你给答案. ...
-
jenkins 修改log路径
修改log路径 默认的路径是/var/log/jenkins/jenkins.log; 修改的话,同样是在/etc/init.d/jenkins中修改: JAVA_CMD="$JENKINS ...
-
JavaScript大杂烩4 - 理解JavaScript对象的继承机制
JavaScript是单根的完全面向对象的语言 JavaScript是单根的面向对象语言,它只有单一的根Object,所有的其他对象都是直接或者间接的从Object对象继承.而在JavaScript的 ...
-
三种数据库连接池的配置及使用(For JDBC)
DBCP 一.导包 Apache官网下载DBCP包,导入两个包路径如下: commons-dbcp-1.4-bin\commons-dbcp-1.4\commons-dbcp-1.4.jar:连接池的 ...
-
c#调用dll接口传递utf-8字串方法
1. 起源: VCU10之视频下载模块,采用纯python编码实现,c++代码调用pythonrun.h配置python运行环境启动python模块,编译为dll给c#调用,以使界面UI能够使用其中功 ...
-
office 格式刷双击无法启用连刷模式
1.问题所在是双击被设置太快了导致office无法接受,请设置成下图中的中等速度即可. 2.可使用快捷键代替 Ctrl+Shift+c(复制格式)Ctrl+Shift+v(粘贴格式)
-
HQL查询中取个别几个字段
数据表: