CF - 791B. Bear and Friendship Condition - 并查集+思维

1.题目描述:

B. 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 (XYZ), 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.


2.题意概述:

n个人,m对关系,关系具有传递性,给你m个描述,问你这样的图是否符合关系图

3.解题思路:

关系具有传递性,很容易想到用并查集来维护。主要是怎么处理关系图矛盾。观察到,正常关系图,每点的度即为他和别人的关系,如果他在一棵树里面,则他的度应该等于 这棵树的节点总数-1(这个很容易理解,你的朋友圈为n人(包括你自己),那么你就有n-1对关系),那么我们在连边的时候处理同时一下他们的度就好了。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 150150
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int pa[maxn], r[maxn], cnt[maxn];
void init(int n)
{
    for (int i = 1; i <= n; i++)
    {
        pa[i] = i;
        r[i] = 0;
        cnt[i] = 0;
    }
}
int findset(int x)
{
    if (x == pa[x])
        return x;
    return pa[x] = findset(pa[x]);
}
void unionset(int a, int b)
{
    int a1 = findset(a);
    int b1 = findset(b);
    if (a1 != b1)
        pa[a1] = b1;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        init(n);
        while (m--)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            cnt[u]++;	//记录每个节点的度
            cnt[v]++;
            unionset(u, v);
        }
        for (int i = 1; i <= n; i++)
            r[findset(i)]++;	//根节点的秩统计这棵树上有多少元素
        int flag = 1;
        for (int i = 1; i <= n; i++)
        {
            int root = findset(i);	
            if (cnt[i] != r[root] - 1)
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            puts("YES");
        else
			puts("NO");
    }
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值