CodeForces - 791B ——D - Bear and Friendship Condition

 

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

题意:假如a 认识b ,b认识c  那么a必须认识c,否者输出no;

给你n个点, m条关系;让你判断是否在建立关系以后 的每个图都是完全图;

什么时完全图呢 ?在图论的数学领域,完全图是一个简单的无向图,其中每对不同的顶点之间都恰连有一条边相连。完整的有向图又是一个有向图,其中每对不同的顶点通过一对唯一的边缘(每个方向一个)连接。n个端点的完全图有n个端点以及n(n − 1) / 2条边,

 

那么你就会发现 在完全图中每个点的度否是(n-1)这就是解题的关键;你在建成图之后是不是可能会有很多个图,那么用并查集来把同一个图中的点捆绑,就是同一个图中的点通过并查集以后他们的father相同;那好么,现在我们把同一个father的点拿去判断 图中的点是否满足度数为(size(图的点数) - 1);  不满足就说明不是完全图 输出NO    如果构成的图全是完全图就输出YES
 
ac码
#include<cstdio>
#include<cstring>
#include<iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>

using namespace std;
typedef long long LL;
const int maxn = 2e5 + 5;
int n, m;

int fa[maxn];   
int ind[maxn];  //度数
vector <int> v[maxn];
int find(int x)
{
	if(fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}

void Union(int x, int y)
{
	int fx = find(x), fy = find(y);
	if(fx == fy) return;
	fa[fx] = fy;
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		for(int i = 1;i <= n;i++)
		{
			fa[i] = i;
			v[i].clear();
		}
		for(int i = 1;i <= m;i++)
		{
			int x, y;
			scanf("%d %d",&x,&y);
			ind[x]++; //度数+1
			ind[y]++;  //度数+1
			Union(x,y);  // 合并到同一个fa
		}
		for(int i = 1;i <= n;i++)
		{
			v[find(i)].push_back(i);  //把同一个fa的点放到 fa这一行,都是在一个图中的点
		}
		int f = 0;
		for(int i = 1;i <= n;i++)
		{
			int len = v[i].size();    //判断这同一个图的点数有多少    完全图中每个点的度数都是(n-1)
			int k = len - 1;
			for(int j = 0 ;j < len;j++)
			{
				if(ind[v[i][j]] != k)    //有一个不是完全图就输出NO
				{
					f = 1;
					break;
				}
			}
		}
		if(f == 1) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/strawberry_595/article/details/81124321