【CF771A】 Bear and Friendship Condition

题目

题意翻译
题意: 给定一张无向图, 要求如果 A 与 B 之间有边, B 与 C 之间有边, 那么 A 与 C 之间也需要有边. 问这张图是否满足要求.
感谢@星烁晶熠辉 提供的翻译

题目描述
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 n members, numbered 1 1 through n n . m 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.

输入输出格式
输入格式:
The first line of the input contain two integers n n and m m ( 3<=n<=150000 3<=n<=150000 , ) — the number of members and the number of pairs of members that are friends.

The i i -th of the next m m lines contains two distinct integers a_{i} a
i
​ and b_{i} b
i
​ ( 1<=a_{i},b_{i}<=n,a_{i}≠b_{i} 1<=a
i
​ ,b
i
​ <=n,a
i
​ ≠b
i
​ ). Members a_{i} a
i
​ and b_{i} b
i
​ are friends with each other. No pair of members will appear more than once in the input.

输出格式:
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).

输入输出样例
输入样例#1: 复制
4 3
1 3
3 4
1 4
输出样例#1: 复制
YES
输入样例#2: 复制
4 4
3 1
2 3
3 4
1 2
输出样例#2: 复制
NO
输入样例#3: 复制
10 4
4 3
5 10
8 9
1 2
输出样例#3: 复制
YES
输入样例#4: 复制
3 2
1 2
2 3
输出样例#4: 复制
NO
说明
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) (2,3) are friends and members (3,4) (3,4) are friends, while members (2,4) (2,4) are not.

思路

题目大概是问每一个联通块是否是完全图

用并查集求出联通块,判断点数和边数的关系即可

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
long long f[150077],u[150077],v[150077],n,m,size_e[150077],size_v[150077];
int gf(int x)
{
	return x==f[x]?x:f[x]=gf(f[x]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; i++) f[i]=i;
	for(int i=1; i<=m; i++)
	{
		scanf("%d%d",&u[i],&v[i]);
		int uu=gf(u[i]),vv=gf(v[i]);
		if(uu!=vv) f[uu]=vv;
	}
	for(int i=1; i<=m; i++) size_e[gf(u[i])]++;
    for(int i=1; i<=n; i++) size_v[gf(i)]++;
    for(int i=1; i<=n; i++)
    if(gf(i)==i)
    {
        long long sv=size_v[i],se=size_e[i];
        if(sv*(sv-1)>>1!=se) 
        {
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值