题干:

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

Note

The drawings below show the situation in the first sample (on the left) and in the second sample (on the right). Each edge represents two members that are friends. The answer is "NO" in the second sample because members (2, 3) are friends and members (3, 4) are friends, while members (2, 4) are not.

题目大意:

朋友关系,定义如果x和y是朋友,y和z是朋友,那么x和z也使朋友,给出朋友关系图,问你这张图是否正确。

解题报告:

其实就是判断一个图的每连通分支,是否都是完全图。如果用dfs做

AC代码:(这里是用vector存边,其实用邻接表存边更妥当,时间也更快)

#include<iostream>
#include<vector>
using namespace std;
bool vis[150000 + 5];
vector<int > vv[150000 + 5];
int n,m;
int cnt;
bool dfs(int x) {for(int i = 0; i<vv[x].size(); i++) {if(vis[vv[x][i] ] == 0) {cnt++;vis[vv[x][i] ] = 1;if(vv[x].size() !=vv[vv[x][i]].size() ) return 0;if( dfs(vv[x][i] ) == 0 ) return 0;} }return 1;
}int main()
{cin>>n>>m;int u,v;while(m--) {scanf("%d %d",&u,&v);vv[u].push_back(v);vv[v].push_back(u);}//暴力所有的点 int flag = 1;for(int i = 1; i<=n; i++) {if(vis[i] == 1) continue;if(vv[i].size() == 0) continue; cnt = 1;vis[i] = 1;if(dfs(i) == 0 ) {
//          cout<<11111<<endl;flag = 0;break;}if(cnt-1 != vv[i].size() ) {
//          cout<<cnt<<endl;flag = 0;break;} }if(flag == 0) printf("NO\n");else printf("YES\n");return 0 ;
}

再贴一个网络上的并查集AC代码:(还未看)维护两个权值,一个集合的点的个数和,一个集合包含的边的个数和。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int mod = 1000000007;
const int maxm = 1005;
const int maxn = 150050;
int n, m;
int f[maxn];
ll num[maxn];
ll e[maxn];
int F(int x){if (f[x] == x)return x;return f[x] = F(f[x]);
}
int main(){int x, y;scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++){f[i] = i;num[i] = 1;e[i] = 0;}for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);if (F(x) == F(y)){ e[f[x]]++; }else{num[f[y]] += num[f[x]];e[f[y]] += e[f[x]] + 1;f[f[x]] = f[y];}}bool flag = 1;for (int i = 1; i <= n; i++){if (F(i) == i){ll u = num[i] * (num[i] - 1) / 2;if (e[i] != u){ flag = 0; break; }}}if (flag)printf("YES");else printf("NO");return 0;
}

*【CodeForces - 791B】Bear and Friendship Condition (图论,判断完全图,dfs乱搞或带权并查集)相关推荐

  1. Codeforces Round #836 (Div. 2) E.Tick, Tock(在线:带权并查集/离线:dfs判环)

    题目 n*m(1<=n,m<=2e5,n*m<=2e5)的网格图, 有一些格子内已经放入了闹钟,当前时刻在[0,h)(1<=h<=1e9)之间 还有一些位置没有放闹钟,输 ...

  2. 【CF771A】Bear and Friendship Condition(并查集加深理解)

    Bear and Friendship Condition 题目概述 题目链接 分析 代码 version1 version2 version3 小结 题目概述 Bear Limak examines ...

  3. sdut 2129树结构练习——判断给定森林中有多少棵树(并查集)

    树结构练习--判断给定森林中有多少棵树 Time Limit: 1000MS Memory limit: 65536K 题目描述 众人皆知,在编程领域中,C++是一门非常重要的语言,不仅仅因为其强大的 ...

  4. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  5. Codeforces Round #405 B. Bear and Friendship Condition a-b,b-c a-c??

    就是说1和2是朋友,2和3是朋友,检查1和3是不是朋友 求完全图个数就完了-.- //china no.1 #pragma comment(linker, "/STACK:102400000 ...

  6. A. Bear and Friendship Condition

    题目 题意:对于一个朋友圈,一个圈内所有的人都是朋友,所以是一个完全无向图,如果是输出"YES",不是输出"NO" 思路:将给出的两个是朋友放在一个数组,并用一 ...

  7. 【CodeForces - 745B】Hongcow Solves A Puzzle (思维,乱搞,字符串)

    题干: Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the inst ...

  8. Codeforces Round #376 (Div. 2) D. 80-th Level Archeology(乱搞)

    题目链接: codeforces731d 题意: 给你n个字符串,每个字符串的元素x取值范围在[1,c],问你能否将所有的x同时进行若干次加1(如果x==c,x变成1)使得字符串是按字典序从小到大排序 ...

  9. 图论 —— 图的连通性 —— 并查集判断连通性

    当需要判断图是否为连通图时,可以使用并查集来进行连通分量的统计,若连通分量大于 1,则说明图中存在多个连通分量,图不为连通图. int n,m; int father[N]; int Find(int ...

最新文章

  1. 增加ESXI中虚拟机CENTOS系统分区容量
  2. mysql索引有哪些_MySQL索引是个什么东西
  3. ClearCanvas DICOM 开发系列 一
  4. oracle如何创建一个定时任务,怎么创建定时任务
  5. 【cocos2d-x从c++到js】20:脚本语言风格的JS代码
  6. 我教育和科研计算机网是指,我校成为中国教育和科研计算机网湘潭城市节点单位...
  7. python实验题_python实验二
  8. c语言 freopen txt_C语言文件操作函数freopen详细解析
  9. html 全屏显示某个区域,JS实现指定区域的全屏显示功能示例
  10. cenyos7安装 yum不可用_centos7安装fabric
  11. 第一季度VR市场报告出炉,中国市场份额下降至全球第三
  12. 【C语言】实现简易计算器
  13. 浅析智慧照明,实现建筑节能
  14. 汽车租赁管理系统的设计与实现(JSP+SqlServer在线租车网站)
  15. 今年春节北京烟花爆竹备货量下降46.7%
  16. 【饭谈】软件测试薪资层次和分段(修仙)
  17. 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
  18. 史上最全BigDecimal的5种进位方式:ROUND_UP,ROUND_DOWN,ROUND_CEILING,ROUND_FLOOR,ROUND_HALF_UP,ROUND_HALF_DOWN的比较
  19. 不看后悔!阿里内部技术参考图册算法篇!(附网盘链接)
  20. 宝塔提示PHP7.3等版本无zip扩展解决

热门文章

  1. HDU-1251 统计难题 map写法
  2. outlook2013邮箱找不到服务器,outlook发邮件总提示找不到
  3. ad19原理图标注_AD19中原理图的模板如何进行编辑?
  4. 计算机选修课学什么,计算机专业都学什么 主要课程有什么
  5. 动态更新纹理闪烁问题
  6. ARMV4,ARMV4T,ARMV4I的意义
  7. linux下 如何调试php,linux下使用gdb对php源码调试
  8. 计算机控制的点火系统由,第八节(点火系统)
  9. maven依赖 spark sql_window环境运行spark-xgboost 8.1踩到的坑
  10. url采集器_Linux「第三节」-centos7.5部署数据采集器Telegraf