Codeforces 791B Bear and Friendship Condition(DFS,有向图)
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<bits/stdc++.h>
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<int>vc[N];//定义一个向量
9 ll t;
10 void DFS(int x)
11 {
12 t++;//计算有关的节点没有被标记过
13 vis[x]=1;
14 for(int j=0;j<vc[x].size();j++)
15 {
16 if(vis[vc[x][j]]==0)//如果和它相连的点没有被标记,
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,0,sizeof(vis));
26 memset(b,0,sizeof(b));
27 for(int i=0;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]=1;
33 b[y]=1;
34
35 }
36 ll sum=0;
37 for(int i=1;i<=n;i++)
38 {
39 if(vis[i]==0&&b[i])
40 {
41 t=0;
42 DFS(i);
43 sum=sum+t*(t-1);//(t-1)*t求边数,完全图
44 }
45 }
46 if(2*m!=sum)cout<<"NO"<<endl;//N个完全图的边数等于m个节点所构成的边数
47 else
48 cout<<"YES"<<endl;
49 }
- asp.net mvc SelectList 的selected 失效及解决方案
- 类极客公园火箭发射“返回顶部”jQuery效果(WordPress代码教程)
- 利用ganymed-ssh2远程执行其它Linux机器上的shell命令
- 浅析软件开发的3个层次
- MSBUILD 命令行编译的时候请注意msbuild文件名称或路经中空格导致出错
- Python可以用来做什么?
- 使用View Model从表现层分离领域模型
- 代码实现WordPress点击进入随机一篇文章的方法
- UE4新手编程之创建空白关卡和添加碰撞体
- Office Web Apps
- hadoop: hive 1.2.0 在mac机上的安装与配置
- ASP.NET 5运行时升级到Beta5
- WordPress免插件仅代码实现“返回顶部、返回底部、评论”效果(样式二)
- jQuery/asp.net mvc DateTime 的处理
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- prometheus入门(一)
- Go语言(十九)日志采集项目之logagent开发(一)
- Go语言(十 八)context&日志项目
- 使用梯度上升欺骗神经网络,让网络进行错误的分类
- Go语言(十七) 配置文件库项目
- Python 相对路径问题:“No such file or directory“
- 基于etcd服务发现的overlay跨多宿主机容器网络
- Go语言(十六) 日志项目升级
- PyQt5 技术篇-设置窗口相对桌面位置,按屏幕比例
- Go语言(十五) 反射
- SpringBoot应用跨域访问解决方案
- Spring Boot 2.2都有哪些新变化
- Go语言(十四)日志项目
- 如何在Spring Boot中使用Cookies
- 在SpringBoot中使用flyway管理数据库版本状态