zoukankan      html  css  js  c++  java
  • Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B

    B. Bear and Friendship Condition

    题目连接:

    http://codeforces.com/contest/791/problem/B

    Description

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

    Sample Input

    4 3
    1 3
    3 4
    1 4

    Sample Output

    YES

    Hint

    题意

    给你一个图,问你这个图是不是reasonable,reasonable的定义是:如果a和b相连,b和c相连,那么a和c必须相连。

    题解:

    翻译一下题意,实际上就是说每个连通块都必须是完全图才行。

    那么假设这个连通块的点数有n个,那么每个点的边集就得是n-1,边的数量为n(n-1)

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 4e5+7;
    int fa[maxn];
    vector<int> E[maxn];
    long long p,e;
    int vis[maxn];
    void dfs(int x){
        p++;
        vis[x]=1;
        for(int i=0;i<E[x].size();i++){
            e++;
            if(vis[E[x][i]])continue;
            dfs(E[x][i]);
        }
    }
    int main(){
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            E[a].push_back(b);
            E[b].push_back(a);
        }
        for(int i=1;i<=n;i++){
            p = 0;
            e = 0;
            if(vis[i])continue;
            dfs(i);
            if(e!=p*(p-1)){
                printf("NO
    ");
                return 0;
            }
        }
        printf("YES
    ");
        return 0;
    }
  • 相关阅读:
    php获取随机字符串
    php短网址生成算法
    tp5.1发送邮件
    PHP简单 对象(object) 与 数组(array) 的转换
    PHP获取接下来一周的日期
    swoole 连接池
    PHP静态文件缓存
    php微信分享demo
    生成二维码并指定地址跳转
    tp5依赖注入(自动实例化):解决了像类中的方法传对象的问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/6582685.html
Copyright © 2011-2022 走看看