VK Cup 2017 - Round 1 A. Bear and Friendship Condition

A. Bear and Friendship Condition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nm 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
Copy
4 3
1 3
3 4
1 4
output
Copy
YES
input
Copy
4 4
3 1
2 3
3 4
1 2
output
Copy
NO
input
Copy
10 4
4 3
5 10
8 9
1 2
output
Copy
YES
input
Copy
3 2
1 2
2 3
output
Copy
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.

 

题意:

  给一个图,判断是不是所有点都符合如果x和y之间有边,y和z之间有边,那么x和z之间有边。

分析:

  并查集合并一下,1个结点和2个结点的联通块不用管,3个点以上的联通块必须是完全图才能保证题意所说的条件,所以记录一下所有联通块如果都是完全图需要多少边,然后和m对比一下就ok啦。

 

///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
ll n;
ll m;
ll pre[MAX],vis[MAX];
ll Find(ll x)
{
    return pre[x]==x?x:pre[x]=Find(pre[x]);
}
int main()
{
    for(ll i=0;i<MAX;i++) pre[i]=i;
    scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=m;i++)
    {
        ll x,y;
        scanf("%lld%lld",&x,&y);
        ll fx=Find(x);
        ll fy=Find(y);
        if(fx!=fy)
            pre[fx]=fy;
    }

    for(ll i=1;i<=n;i++)
        vis[Find(i)]++;
    ll sum=0;
    for(ll i=1;i<=n;i++)
    {
        if(vis[i]>=2)
            sum+=vis[i]*(vis[i]-1)/2LL;
    }
    if(sum==m) printf("YES\n");
    else printf("NO\n");
    return 0;
}
View Code

 

posted @ 2019-03-07 11:06  Kissheart_LMC  阅读(202)  评论(0编辑  收藏  举报