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

题干:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441653.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matlab aviobj,MATLAB AVI 视频读取处理

MATLAB AVI 视频读取处理1、用matlab读取avi视频(只能读一定压缩各式的avi 电影&#xff0c;这是因为avi视频文件的编码有很多&#xff0c;而matlab只支持部分编码格式。可见http://www.doczj.com/doc/4ae6c6222af90242a895e5fd.html/IdoIwill/article/details/2125838)aviinfo…

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

题干&#xff1a; Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character X denote…

升级oracle spu,关于Oracle数据库PSU/SPU/BundlePatch的补丁号变化

思庄中心OCP脱产班1月15日开班&#xff0c;周末班于1月12日开班&#xff01;熟悉 OracleDatabase PSU、Bundle Patch 的DBA一定知道&#xff0c;一般来讲&#xff0c;这些 patch的命名规则一般是按照推出的先后顺序&#xff0c;比如在 2015年7月 推出的对 11.2.0.4 的第7个 DB …

【POJ - 2631】Roads in the North (树的直径,模板)

题干&#xff1a; Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village…

h5前端有php,【前端】为什么有些网站的页面地址,没有加上html或者php或者jsp的后辍名?...

如写成http://www.例子.com/page/help的形式回答因为不需要Every HTTP URL conforms to the syntax of a generic URI. A generic URI is of the form:scheme:[//[user:[email protected]]host[:port]][/]path[?query][#fragment]via Wikipedia可见 URI 并不需要以后缀名结尾。…

【CodeForces - 803D】Magazine Ad(二分答案)

题干&#xff1a; The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this: There are space-separated non-empty words of lowercase and uppercase Latin letters. There are hyphen characters - i…

freeradius mysql php,freeradius mysql的相关配置说明

freeradius和mysql的配置# mysqladmin -u root -p create radius# mysql -u root -p radius#GRANT SELECT ON radius.* TO radiuslocalhost IDENTIFIED BY radpass;mysql> GRANT ALL on radius.radacct TO radiuslocalhost;mysql> GRANT ALL on radius.radpostauth TO ra…

php的mcypt,php 7.3 在Centos6.x下的安装过程

提示unrecognized options: –with-mcrypt, –enable-gd-native-ttf表示php7.3不支持这两个选项&#xff0c;编译准备工作:yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-dev…

【CSU - 1023】【HRBUST - 1039】修路(二分,枚举)

题干&#xff1a; 前段时间&#xff0c;某省发生干旱&#xff0c;B山区的居民缺乏生活用水&#xff0c;现在需要从A城市修一条通往B山区的路。假设有A城市通往B山区的路由m条连续的路段组成&#xff0c;现在将这m条路段承包给n个工程队&#xff08;n ≤ m ≤ 300&#xff09;。…

php继承和重载区别,php继承中方法重载(覆盖)的应用场合

php继承中方法重载(覆盖)的应用场合发布于 2015-02-23 17:57:23 | 91 次阅读 | 评论: 0 | 来源: 网友投递PHP开源脚本语言PHP(外文名: Hypertext Preprocessor&#xff0c;中文名&#xff1a;“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点&am…

【CodeForces - 570A】Elections(模拟,水题)

题干&#xff1a; The country of Byalechinsk is running elections involving n candidates. The country consists of m cities. We know how many people in each city voted for each candidate. The electoral system in the country is pretty unusual. At the first …

linux查看文件从底部开始,linux中怎样从底部向上查看log文件

Leetcode&colon; sliding window maximumAugust 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...用 pyvenv 创建几个不相互影响的python虚拟环境IN MY UBUNTU python2的环…

【CodeForces - 569B】Inventory (水题)

题干&#xff1a; Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep …

linux文件目录作用,linux 文件目录,及其作用

/boot:系统启动相关的相关文件&#xff0c;如内核&#xff0c;initrd&#xff0c;以及grub(bootloader)/dev: 设备文件设备文件&#xff1a;块设备&#xff1a;随机访问&#xff0c;数据块字符设备&#xff1a;线性访问&#xff0c;按字符文单位设备号&#xff1a;主设备号(maj…

算法笔记 -- 离散化

引入 离散化&#xff0c;就是把一些很离散的点给重新分配。 举个例子&#xff0c;如果一个坐标轴很长(>1e10)&#xff0c;给你1e4个坐标&#xff0c;询问某一个点&#xff0c;坐标比它小的点有多少。 很容易就知道&#xff0c;对于1e4个点&#xff0c;我们不必把他们在坐…

【CodeForces - 580D】Kefa and Dishes (状压dp)

题干&#xff1a; kefa进入了一家餐厅&#xff0c;这家餐厅中有n个菜&#xff08;0<n≤18&#xff09;,kefa对第i个菜的满意度为ai&#xff08;0≤ai≤10^9&#xff09;&#xff0c;并且对于这n个菜有k个规则&#xff0c;如果kefa在吃完第xi个菜之后吃了第yi个菜&#xff0…

使用linux内核编译独立系统,编译linux内核以及depmod的使用

转载&#xff1a;http://blog.lmtw.com/b/18215/archives/2010/71074.htmldepmod(depend module)功能说明&#xff1a;分析可载入模块的相依性。语  法&#xff1a;depmod [-b basedir] [-e] [-F System.map] [-n] [-v] [version] [-A]depmod [-e] [-FSystem.map] [-…

【CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)

题干&#xff1a; Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable propert…

Linux中软件安装使用的命令是,Linux软件安装命令

1.tartar命令用于把多个文件合并于一个档案文件中,并提供分解的合并后的档案文件的功能.它独立于压缩工具,因此可以选择在合并前是否压缩.tar命令的基本用法为:tar[选项]文件名常用的选项包括-c创建一个新的档案文件-t查看档案文件的内容-x分解档案文件的内容-f指定档案文件的名…

linux data文件如何打开,DATA 文件扩展名: 它是什么以及如何打开它?

DATA 问题的来源常见的 DATA 打开问题Microsoft Excel 消失如果您尝试加载 DATA 文件&#xff0c;则会收到 “无法打开 DATA 文件” 等消息。 通常情况下&#xff0c;这意味着 Microsoft Excel 没有安装在 %%os%% 上。 通过双击打开 DATA 的典型路径将不起作用&#xff0c;因为…