CodeForce-791B 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).
Example
4 3
1 3
3 4
1 4
YES
4 4
3 1
2 3
3 4
1 2
NO
10 4
4 3
5 10
8 9
1 2
YES
3 2
1 2
2 3
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。
题意:给定N个点M条链路来描述两个点之间的关系,并且A-B,B-C,那么A-C一定要有边,问你给定的符不符合要求
思路:并查集,把连在一起的统计到一棵树上,然后树上所有点的边数都应该相等
#include <iostream>
#include <cstdio>
using namespace std;
int fa[100050], du[100050], ran[100050];
int find(int a)
{
return fa[a] == a ? a : find(fa[a]);
}
void bing(int x, int y)
{
x = find(x);
y = find(y);
if (x != y)
{
fa[x] = y;
}
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; i++)
{
fa[i] = i, du[i] = 0, ran[i] = 0;
}
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
bing(a, b);
du[a]++, du[b]++;
}
for (int i = 0; i <= n; i++)
{
int x = find(i);
ran[x]++;
}
int flag = 0;
for (int i = 0; i <= n; i++)
{
int x = find(i);
if (du[i] != ran[x] - 1)
{
flag = 1;
break;
}
}
if (!flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
CodeForce-791B Bear and Friendship Condition(并查集)的更多相关文章
- Codeforces 791B. Bear and Friendship Condition 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- Codeforces 791B Bear and Friendship Condition(DFS,有向图)
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- Codeforces791 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- codeforces round #405 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- 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 ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
- ZOJ:2833 Friendship(并查集+哈希)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833 A friend is like a flower, a rose ...
随机推荐
- 热血动漫番太好看了!用Python爬取了1T的动漫,内存都爆了
最近被室友安利热血动漫番<终末的女武神>和<拳愿阿修罗>,太上头了周末休息熬夜看完了.不过资源不太好找,辣条一怒爬取了资源,这下可以看个够了.室友崇拜连连,想起了我的班 ...
- k8s之数据存储-高级存储
PV和PVC 前面已经学习了使用NFS提供存储,此时就会要求用户会搭建NFS系统,并且会在yaml配置nf's,由于k8s支持的存储系统有很多,要求客户全部掌握,显然不现实.为了能够屏蔽底层存储实现的 ...
- SQL 练习40
按照出生日期来计算学生的年龄信息 IF OBJECT_ID('GetStudentAge','FN') IS NOT NULL DROP FUNCTION GetStudentAge GO CREAT ...
- SSM自学笔记(一)
本文内容 Ioc和DI Spring快速入门 Spring配置文件 Spring IoC和DI注解开发 Spring配置数据源 Spring注解开发 Spring整合Junit IoC 和 DI 1. ...
- docker 安装 sonarQube
sonarQube 是一款开源代码检测工具.本篇介绍通过 docker 来安装.大概的一个运作流程是这样的,先通过 sonar-scanner 插件扫描代码,把数据存储到数据库,sonarQube 读 ...
- css内容渐入效果实现
.fade-in-section { opacity: 0; transform: translateY(20vh); visibility: hidden; transition: opacity ...
- WPF 饼状图,柱形图,折线图 (2 折线图)
折线图在柱形图的基础上,做了一些修改.大概效果和用法如下. X轴和Y轴的刻度,使用用了Path的Figures属性,绘制多条Figure+LineSegment完成. 同时,由于折线图很可能会画多条线 ...
- IOC--框架进阶
IOC控制反转 含义:把高层对底层的依赖 转移到由第三方决定 避免高层对底层的直接依赖 使得程序架构具有良好的扩展性和稳定性 理解:就是一种目的--解除依赖 DI依赖注入 含义:在构造对象是 可以自动 ...
- C# 线程同步的多种方式
实际应用中多个线程往往需要共享数据,因此必须使用同步技术,确保一次只有一个线程访问和改变共享数据.同步又分为进程内部线程的同步以及进程之间线程的同步. 进程内部线程同步: 1. lock : 使用比较 ...
- jQuery中的文档操作处理(五):append()、prepend()、after()、before()、wrap()、wrapAll()、wrapInner()、clone()等
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...