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
  1. 4 3
    1 3
    3 4
    1 4
Output
  1. YES
Input
  1. 4 4
    3 1
    2 3
    3 4
    1 2
Output
  1. NO
Input
  1. 10 4
    4 3
    5 10
    8 9
    1 2
Output
  1. YES
Input
  1. 3 2
    1 2
    2 3
Output
  1. 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<bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. const int N=+;
  5. int a[N];
  6. int b[N];
  7. int vis[N];
  8. vector<int>vc[N];//定义一个向量
  9. ll t;
  10. void DFS(int x)
  11. {
  12. t++;//计算有关的节点没有被标记过
  13. vis[x]=;
  14. for(int j=;j<vc[x].size();j++)
  15. {
  16. if(vis[vc[x][j]]==)//如果和它相连的点没有被标记,
  17. DFS(vc[x][j]);
  18. }
  19. }
  20. int main()
  21. {
  22. int m,n;
  23. int x,y;
  24. scanf("%d%d",&n,&m);
  25. memset(vis,,sizeof(vis));
  26. memset(b,,sizeof(b));
  27. for(int i=;i<m;i++)
  28. {
  29. scanf("%d%d",&x,&y);
  30. vc[x].push_back(y);//找到x和y的关系
  31. vc[y].push_back(x);
  32. b[x]=;
  33. b[y]=;
  34.  
  35. }
  36. ll sum=;
  37. for(int i=;i<=n;i++)
  38. {
  39. if(vis[i]==&&b[i])
  40. {
  41. t=;
  42. DFS(i);
  43. sum=sum+t*(t-);//(t-1)*t求边数,完全图
  44. }
  45. }
  46. if(*m!=sum)cout<<"NO"<<endl;//N个完全图的边数等于m个节点所构成的边数
  47. else
  48. cout<<"YES"<<endl;
  49. }

Codeforces 791B Bear and Friendship Condition(DFS,有向图)的更多相关文章

  1. Codeforces 791B. Bear and Friendship Condition 联通快 完全图

    B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...

  2. CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)

    题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...

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

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

  5. Codeforces_791_B. Bear and Friendship Condition_(dfs)

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  6. Codeforces791 B. Bear and Friendship Condition

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  7. 【codeforces 791B】Bear and Friendship Condition

    [题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...

  8. Codeforces 653B Bear and Compressing【DFS】

    题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...

  9. CodeForce-791B Bear and Friendship Condition(并查集)

    Bear Limak examines a social network. Its main functionality is that two members can become friends ...

随机推荐

  1. iOS 实现后台 播放音乐声音 AVAudioPlayer 以及铃声设置(循环播放震动)

    1.步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio 或者: 步骤二: - (BOOL) ...

  2. [置顶] xamarin Tablayout+Viewpager+Fragment顶部导航栏

    最近几天不忙,所以把项目中的顶部导航栏的实现归集一下.android中使用TabLayout+ViewPager+Fragment制作顶部导航非常常见,代码实现也比较简单.当然我这个导航栏是基于xam ...

  3. HTTPS从认识到线上实战全记录

    前言 关于HTTPS,基本上你想知道的都在这里了.本文原标题<HTTPS原理与实践>,下图是本文配套PPT的目录截图: [TOC] 原理篇 认识HTTPS 先说一下,本文可能有些地方由于描 ...

  4. python爬虫——建立IP池,将可用IP存放到redis

    直接上代码,每行代码后面都有注释 import urllib.request import urllib import re import time import random import sock ...

  5. 一、JavaSE语言概述

    1.软件:系统软件 VS 应用软件 2.人与计算交互:使用计算机语言.图形化界面VS命令行. 3.语言的分类:第一代:机器语言 第二代:汇编语言 第三代语言:高级语言(面向过程-面向对象) 4.jav ...

  6. Jenkins:基于linux构建ivy项目

    Jenkins:基于linux构建ivy项目 (二) 基于以上<Jenkins:VMware虚拟机Linux系统的详细安装和使用教程(一)>的配置再进行对ivy项目构建: 启动tomcat ...

  7. 标准模式 怪异模式 盒模型 doctype

    在页面顶部设置 doctype是为了统一标准 浏览器有标准模式和怪异模式 而这两种模式最大区别就是 盒模型的解析不同 ============================== 图片摘自网络 === ...

  8. Pandas库的使用--Series

    一.概念 Series相当于一维数组. 1.调用Series的原生方法创建 import pandas as pd s1 = pd.Series(data=[1,2,4,6,7],index=['a' ...

  9. 企业级memcached缓存数据库结合php使用与web管理memcached

    环境 [root@cache01 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@cache01 ~]# uname -a ...

  10. Atlas 安装报错 package Atlas-2.2.1-1.x86_64 is intended for a x86_64 architecture

    安装atlas 报错: package Atlas-2.2.1-1.x86_64 is intended for a x86_64 architecture 百度了好久没找到相关信息,最后看见官网文档 ...