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 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).
Sample Input
4 3
1 3
3 4
1 4
Sample Output
YES
Hint
题意
给你一个图,问你这个图是不是reasonable,reasonable的定义是:如果a和b相连,b和c相连,那么a和c必须相连。
题解:
翻译一下题意,实际上就是说每个连通块都必须是完全图才行。
那么假设这个连通块的点数有n个,那么每个点的边集就得是n-1,边的数量为n(n-1)
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5+7;
int fa[maxn];
vector<int> E[maxn];
long long p,e;
int vis[maxn];
void dfs(int x){
p++;
vis[x]=1;
for(int i=0;i<E[x].size();i++){
e++;
if(vis[E[x][i]])continue;
dfs(E[x][i]);
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
}
for(int i=1;i<=n;i++){
p = 0;
e = 0;
if(vis[i])continue;
dfs(i);
if(e!=p*(p-1)){
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题的更多相关文章
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
如果某个位置i是Y,直接直到i+m-1为止填上新的数字. 如果是N,直接把a[i+m-1]填和a[i]相同即可,这样不影响其他段的答案. 当然如果前面没有过Y的话,都填上0就行了. #include& ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
Description In the army, it isn't easy to form a group of soldiers that will be effective on the bat ...
随机推荐
- oracle 用户 权限
一. 概述 与权限,角色相关的视图大概有下面这些: DBA_SYS_PRIVS: 查询某个用户所拥有的系统权限 USER_SYS_PRIVS: 当前用户所拥有的系统权限 SESSION_PRIVS ...
- 通用jsonp跨域技术获取天气数据
1. 前言 在进行网站开发的过程中经常会用到第三方的数据,但是由于同源策略的限制导致ajax不能发送请求,因此也无法获得数据.解决ajax的跨域问题可以使用jsonp技术 2.代码 <!DOCT ...
- PYTHON-模块定义 搜索路径
模块是什么: ***** 模块 是一系列功能的集合体 一个py文件就是一个模块 一个函数就是一个功能 例如 A.py 文件名A.py 模块名 A 模块有哪些来源 内置 第三方 自定义 模块有四种通用的 ...
- webpack 4.0.0-beta.0 新特性介绍
webpack 可以看做是模块打包机.它做的事情是:分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等),并将其打包为合适的格式 ...
- 性能测试八:jmeter进阶之beanshell
* BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; * BeanShell是一种松散类型的脚本语言(这点和JS类似); * BeanShell是用Java ...
- 步步为营-47-分页显示的SQL语句
说明:分页显示在实际业务中经常需要用到,其SQL语句分两种 1:分页显示SQL语句 --方法一:跳过多少行,选中多少行 --每页n条,选择第m页--n= m= --)*n 主键 from 表); se ...
- Chakra TypedArray代码实现笔记
ArrayBuffer.cpp阅读 对象继承关系 JavascriptArrayBuffer: ArrayBuffer: ArrayBufferBase: DynamicObject: Recycla ...
- gulp给文件加版本号
版本号用文件MD5生成 默认根据文件MD5生成,因此文件未发生改变,此版本号将不会变 所以当没有改变文件的时候,我们就不能用gulp来改变版本号了 需要安装的插件 npm install --save ...
- HTML编码规范 - (WEB前端命名规范)
HTML编码规范 (一)命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wr ...
- java07接口与继承动手动脑
1.源代码: package Work; class A{ public A(){ System.out.println("Class A."); } public A(Strin ...