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;
    }

     

  • 相关阅读:
    Android Studio官方文档: 如何在你的设备上运行你的程序
    Android Studio ERROR: x86 emulation currently requires hardware acceleration!报错解决傻瓜教程~
    Android studio 无法启动安卓模拟器
    从Windows系统服务获取活动用户的注册表信息(当前活动用户的sessionId. 当前活动用户的 hUserToken)
    Linux IO模式
    宽字符std::wstring的长度和大小问题?sizeof(std::wstring)是固定的32,说明std::wstring是一个普通的C++类,而且和Delphi不一样,没有负方向,因为那个需要编译器的支持
    Delphi 拦截滚轮事件不响应滚轮的上下滚动
    台湾芯片产业的兴与衰:输赢千亿美元的生死搏杀
    go语言
    一次请求到响应的整个流程
  • 原文地址:https://www.cnblogs.com/RootVount/p/10645006.html
Copyright © 2011-2022 走看看