博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 791B Bear and Friendship Condition(DFS,有向图)
阅读量:6463 次
发布时间:2019-06-23

本文共 2271 字,大约阅读时间需要 7 分钟。

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

题目链接:http://codeforces.com/contest/791/problem/B

分析:给你m对朋友关系; 如果x-y是朋友,y-z是朋友,要求x-z也是朋友. 问你所给的图是否符合!

用DFS去找他们之间的关系,有向图去算m个点对应的边的数量,看是否相等!

下面给出AC代码:

1 #include
2 typedef long long ll; 3 using namespace std; 4 const int N=150000+100; 5 int a[N]; 6 int b[N]; 7 int vis[N]; 8 vector
vc[N];//定义一个向量 9 ll t;10 void DFS(int x)11 {12 t++;//计算有关的节点没有被标记过13 vis[x]=1;14 for(int j=0;j

 

转载地址:http://unhzo.baihongyu.com/

你可能感兴趣的文章
Redis详解
查看>>
论程序员加班的害处
查看>>
codeblocks快捷键
查看>>
基于HTML5的WebGL设计汉诺塔3D游戏
查看>>
WPF资料链接
查看>>
过滤DataTable表中的重复数据
查看>>
再次更新
查看>>
mysql的数据类型int、bigint、smallint 和 tinyint取值范围
查看>>
利用网易获取所有股票数据
查看>>
移动铁通宽带上网设置教程
查看>>
Python算法(含源代码下载)
查看>>
利用Windows自带的Certutil查看文件MD5
查看>>
通过原生js添加div和css
查看>>
简单的导出表格和将表格下载到桌面上。
查看>>
《ArcGIS Engine+C#实例开发教程》第一讲桌面GIS应用程序框架的建立
查看>>
查询指定名称的文件
查看>>
Python 嵌套列表解析
查看>>
[GXOI/GZOI2019]旧词——树链剖分+线段树
查看>>
anroid 广播
查看>>
AJAX POST&跨域 解决方案 - CORS
查看>>