G - Bear and Friendship Condition (并查集)
2017-07-18 19:30
465 查看
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 isreasonable if and only
if the following condition is satisfied: For every threedistinct 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).
Example
Input
Output
Input
Output
Input
Output
Input
Output
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.
思路:认识是相互的说明是无向边,由传递性可知这个无向图必须是无向完全图才是YES
每个联通分支中无向完全图边的条数与顶点数n关系为n*(n-1)/2 。 用并查集求分支,
比以往不同,可以在并查集时增添一个数组记录每个分支元素数目。要注意得用loog long
虽然边数不会越界,但是你计算的边数(完全图的边数)是可能越界的。
思路:认识是相互的说明是无向边,由传递性可知这个无向图必须是无向完全图才是YES
每个联通分支中无向完全图边的条数与顶点数n关系为n*(n-1)/2 。 用并查集求分支,
比以往不同,可以在并查集时增添一个数组记录每个分支元素数目。要注意得用loog long
虽然边数不会越界,但是你计算的边数(完全图的边数)是可能越界的。
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 isreasonable if and only
if the following condition is satisfied: For every threedistinct 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).
Example
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.
思路:认识是相互的说明是无向边,由传递性可知这个无向图必须是无向完全图才是YES
每个联通分支中无向完全图边的条数与顶点数n关系为n*(n-1)/2 。 用并查集求分支,
比以往不同,可以在并查集时增添一个数组记录每个分支元素数目。要注意得用loog long
虽然边数不会越界,但是你计算的边数(完全图的边数)是可能越界的。
#include <iostream> #include <cstring> #include <climits> #include <cmath> #include <cstdio> using namespace std; typedef long long ll; const int maxn = 151000; ll pre[maxn]; ll num[maxn]; // 存每个分支数目 ll m; void init() { for(int i = 1; i<=m ;i ++) pre[i] = i,num[i] = 1; } ll find_ (ll x) { return pre[x] == x ? x : (pre[x] = find_(pre[x])); } void join(int x, int y) { ll fx = find_ (x); ll fy = find_ (y); if(fx != fy) { pre[fy] = fx; num[fx] += num[fy]; } } int main() { ll n,tmp; scanf("%lld%lld",&m,&n); init(); tmp = n; while(n --) { ll x,y; scanf("%lld%lld",&x,&y); join(x,y); } ll tol = 0; for(int i = 1; i <= m ; i++) { if(pre[i] == i) tol += (num[i]*(num[i]-1) / 2); } if(tmp == tol) printf("YES\n"); else printf("NO\n"); return 0; }
思路:认识是相互的说明是无向边,由传递性可知这个无向图必须是无向完全图才是YES
每个联通分支中无向完全图边的条数与顶点数n关系为n*(n-1)/2 。 用并查集求分支,
比以往不同,可以在并查集时增添一个数组记录每个分支元素数目。要注意得用loog long
虽然边数不会越界,但是你计算的边数(完全图的边数)是可能越界的。
相关文章推荐
- B. Bear and Friendship Condition----并查集或BFS
- Bear and Friendship Condition CodeForces - 771A(图论,并查集)题解
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
- (DFS, 图)Codeforces Round #405 B. Bear and Friendship Condition
- Codeforces Round #405 B.Bear and Friendship Condition【Dfs+思维】
- 【codeforces 791B】Bear and Friendship Condition
- Codeforces791 B. Bear and Friendship Condition
- Codeforces Round #405 B.Bear and Friendship Condition
- codeforces 791 B. Bear and Friendship Condition (DFS搜索)
- Codeforces_791_B. Bear and Friendship Condition_(dfs)
- Bear and Friendship Condition
- Codeforces 791 B Bear and Friendship Condition 规律
- codeforces round #405 B. Bear and Friendship Condition
- (codeforces)A. Bear and Friendship Condition
- Codeforces Round #226 (Div. 2)A. Bear and Raspberry
- zoj 2833 Friendship(并查集)
- CodeForces 385C Bear and Prime Numbers
- codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)
- A. Bear and Reverse Radewoosh CodeForces 658A
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors