CodeForces - 771A Bear and Friendship Condition(思维! + 无向图连通分量)

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

题意:告诉你 n 个人之间的 m 对朋友关系。而对于每两对朋友关系,必须有 假如   A 和 B 是朋友, B 和 C 是朋友,那么 A 和 C 就应该是朋友。 题目问是否满足这个条件。

思路:仔细思考,对于 有关系的 x 个之间,他们应该拥有的关系对数为   x * (x - 1) / 2, 可以利用这一点  来计算有多少的关系圈,再判断这些关系圈 之间的关系对数是否大于 m,大于   则表示 m 不够,就输出NO. 否则输出 YES 

AC代码:

#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
vector<int> v[150010];  /// 放儿子
int vis[150010];
int ltfl[150010];

void dfs(int p,int &sz){
    for(int i = 0;i < v[p].size();i ++){
        int son = v[p][i];
        if(!vis[son]){
            vis[son] = 1;
            dfs(son,++ sz);
        }
    }
}

int main()
{
    int n; long long m;
    while(~scanf("%d%I64d",&n,&m)){
        memset(vis,0,sizeof(vis));
        memset(ltfl,0,sizeof(ltfl));
        for(int i = 0;i < 150010;i ++) v[i].clear();
        int a,b;
        for(int i = 0;i < m;i ++){
            scanf("%d%d",&a,&b);
            v[a].push_back(b);
            v[b].push_back(a);
        }
        int sz = 1,coun = 0;
        for(int i = 1;i <= n;i ++){
            if(!vis[i]){
                vis[i] = 1;
                dfs(i,sz);
                //printf("%d\n",sz);
                ltfl[coun ++] = sz;
                sz = 1;
            }
        }
        long long ans = 0;
        for(int i = 0;i < coun;i ++){
            ans += (long long)ltfl[i] * (long long)(ltfl[i] - 1) / 2;
        }
        if(m == ans) puts("YES");
        else puts("NO");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82083072