码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 771A Bear and Friendship Condition

时间:2019-04-02 21:22:14      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:force   while   index   app   title   个数   这一   i++   otherwise   

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

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.

技术图片

 

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<cmath>
#include<set>
#include<queue>
using namespace std;
#define ll long long
const int inf = 0xffffff;
const int maxn = 1e6;
int n, m;
int sign[maxn];
ll number[maxn], sum[maxn];
int get(int x)//并查集查找最新的那个朋友
{
    if(x != sign[x])
    {
        sign[x] = get(sign[x]);
    }
    return sign[x];
}
int main()
{
    int x, y;
    scanf("%d%d", &n, &m);
    for(int i = 0; i <= n; i++)
    {
        sign[i] = i;
        number[i] = 1;//实际上跟自己是直接朋友的个数
        sum[i] = 0;//理论上跟自己是朋友的个数
    }
    for(int i = 0; i<m; i++)
    {
        scanf("%d%d", &x, &y);
        if(get(x) == get(y))//如果他们已经有共同的朋友
        {
            sum[sign[x]]++;//那理论朋友数量就加加
        }
        else//如果还没有共同好友,那就让他们成为朋友
        {
            number[sign[y]] += number[sign[x]];//实际朋友的数量用y加上x的,因为彼此已经是朋友了,那我的朋友就是你的朋友
            sum[sign[y]] += sum[sign[x]]+1;//理论朋友除了加上x的朋友之外,还要加上x这个人
            sign[sign[x]] = sign[y];//让他们成为朋友
        }
    }
    int flag = 1;
    for(int i = 1; i <= n; i++)
    {
        if(sign[i] == i)//如果这个玩意儿已经是这一部分里统一指向的那个好友
        {
            ll miao = number[i]*(number[i]-1)/2;
            if(miao != sum[i])
            {
                flag = 0;
                break;
            }
        }
    }
    if(flag)printf("YES\n");
    else printf("NO\n");
    return 0;
}

 

 

CodeForces - 771A Bear and Friendship Condition

标签:force   while   index   app   title   个数   这一   i++   otherwise   

原文地址:https://www.cnblogs.com/RootVount/p/10645006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!