zoukankan      html  css  js  c++  java
  • CodeForces

    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
    ");
        else printf("NO
    ");
        return 0;
    }

     

  • 相关阅读:
    理解构造器
    if与switch的性能比较
    Java对象的内存(一)
    shell编程_条件判断if
    shell编程_基础&变量
    集群架构篇:Nginx架构演进<拆分数据库 多台web节点共享静态资源>
    集群架构篇:Nginx流行架构LNMP
    集群架构篇:Nginx常用模块
    LInux系统@安装CentOS7虚拟机
    docker pull越来越慢的解决方法
  • 原文地址:https://www.cnblogs.com/RootVount/p/10645006.html
Copyright © 2011-2022 走看看