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

     

  • 相关阅读:
    选择排序
    转:ASP.NET MVC中Unobtrusive Ajax的妙用
    转:MVC 下导航超链接本页面高亮的一种解决方案
    转载:iOS 推送的服务端实现
    转载:Unobtrusive JavaScript in ASP.NET MVC 3 隐式的脚本在MVC3
    待实践三:MVC3下 路由的测试 使用 RouteDebug.dll 来测试判断路由是否符合
    待实践二:MVC3下的3种验证 (1)前台 jquery validate验证 (2)MVC实体验证 (3)EF生成的/自己手写的 自定义实体校验(伙伴类+元素据共享)
    待实践一:仿造博客园后台上传头像并切图生成缩略图fine uploader.js jcrop.js
    Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据
    Linq 时间对比陷阱坑
  • 原文地址:https://www.cnblogs.com/RootVount/p/10645006.html
Copyright © 2011-2022 走看看