Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题

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\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/6582685.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞