【CF771A】Bear and Friendship Condition(并查集加深理解)

题目概述

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

题目链接

传送门

分析

对于某几个节点,如果他们之间形成朋友关系,则任意两个节点之间必有一条边相连。因此我们可以使用并查集实现,每一个森林,(若干个节点的集合)它们之间的边数必须等于排列数C(n, 2),n是节点数。

代码

version1

如果两个点x, y所在集合(U,V)的代表元素不同,设为设u, v.让u认v做父亲,v拿走u的节点和边数,最后要判断一个集合是否满足条件,我们只需要确定集合U的代表元素u,判断边和点的数量关系即可

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 2e5;
int n;
long long m;
int par[N];
long long edge[N], node[N];

void init(int n){
    for (int i = 1; i <= n; ++i)
        par[i] = i, node[i] = 1;
}

int find(int x){
    return x == par[x] ? x : (par[x] = find(par[x]));
}

int main() {
    scanf("%d%lld", &n, &m);
    init(n);
    int x = 0, y = 0;
    for (long long i = 1; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        int u = find(x), v = find(y);
        if (u != v) {
            par[u] = v;
            node[v] += node[u];
            edge[v] += (edge[u] + 1);
        }else{
            ++edge[v];
        }
    }

    bool flag = true;
    for (int i = 1; i <= n; ++i) {
            if (par[i] == i && node[i] * (node[i] - 1) != 2 * edge[i]){
                flag = false;
                break;
            }
    }
    printf("%s\n",flag ? "YES": "NO");
    return 0;
}

version2

干脆直接省去对每个代表元素u对应集合U的边的统计,直接看总和,若总和小于m,肯定有集合少边了,不能满足条件。

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 2e5;
int n;
long long m;
int par[N];
long long node[N];

void init(int n){
    for (int i = 1; i <= n; ++i)
        par[i] = i, node[i] = 1;
}

int find(int x){
    return x == par[x] ? x : (par[x] = find(par[x]));
}

int main() {
    scanf("%d%lld", &n, &m);
    init(n);
    int x = 0, y = 0;
    for (long long i = 1; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        int u = find(x), v = find(y);
        if (u != v) {
            par[u] = v;
            node[v] += node[u];
        }
    }

    long long sum = 0;
    for (int i = 1; i <= n; ++i) {
            if (par[i] == i && node[i] * (node[i] - 1))
                sum += node[i] * (node[i] - 1) / 2;
    }
            
    printf("%s\n",sum == m? "YES": "NO");
    return 0;
}

version3

node数用int存,最后统计的时候再转化为long long

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 2e5;
int n;
long long m;
int par[N],node[N];

void init(int n){
    for (int i = 1; i <= n; ++i)
        par[i] = i, node[i] = 1;
}

int find(int x){
    return x == par[x] ? x : (par[x] = find(par[x]));
}

int main() {
    scanf("%d%lld", &n, &m);
    init(n);
    int x = 0, y = 0;
    for (long long i = 1; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        int u = find(x), v = find(y);
        if (u != v) {
            par[u] = v;
            node[v] += node[u];
        }
    }

    long long sum = 0;
    for (int i = 1; i <= n; ++i) {
            if (par[i] == i && node[i] * (node[i] - 1))
                sum += 1LL * node[i] * (node[i] - 1) / 2;
    }

    printf("%s\n",sum == m? "YES": "NO");
    return 0;
}

小结

同样是93ms,空间从8592kb省到了6240kb,逻辑也更简练,实际上n最大为150000,还可以再省,不过意义不大了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值