Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
2017-03-19 22:21
423 查看
B. Bear and Friendship Condition
题目连接:
http://codeforces.com/contest/791/problem/BDescription
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 31 3
3 4
1 4
Sample Output
YESHint
题意
给你一个图,问你这个图是不是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. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
- 【VK Cup 2016 - Round 1 (Div 2 Edition)B】【水题】Bear and Displayed Friends 即时维护最大6个数
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) C. Bear and Company(DP)
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
- VK Cup 2016 - Round 1 (Div. 2 Edition) A. Bear and Reverse Radewoosh 水题
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
- Codeforces Round #356 (Div. 2) B. Bear and Finding Criminal 水题
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
- VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials
- 【VK Cup 2016 - Round 1 (Div 2 Edition)C】【构造】Bear and Forgotten Tree 3 构造一棵树直径为d且点1的深度为h
- VK Cup 2016 - Round 1 (Div. 2 Edition) A Bear and Reverse Radewoosh
- 【VK Cup 2016 - Round 1 (Div 2 Edition)D】【二进制思想 扫描贪心】Bear and Polynomials 2的幂次多项式改变一位使得和为0
- VK Cup 2016 - Round 1 (Div. 2 Edition)C. Bear and Forgotten Tree 3(构造)
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer(水题)