Codeforces_791_B. Bear and Friendship Condition_(dfs)
1 second
256 megabytes
standard input
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 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.
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.
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).
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
题意:给定一个图(盆友关系),判断盆友关系是否合理(若A-B,B-C,则A-C)。 思路:先dfs判断一个盆友圈有圈有c人,若盆友圈合理,那么其中所有点的度都是c-1,再用dfs判断即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 150005 struct Eage
{
int v,next;
} eage[N*];
int head[N],cnt[N],cnte; void addEage(int a,int b)
{
eage[cnte].v=b;
eage[cnte].next=head[a];
head[a]=cnte++;
cnt[a]++;
} bool vis1[N];
int dfs1(int u)
{
int tmp=;
for(int i=head[u]; i!=; i=eage[i].next)
{
int v=eage[i].v;
if(vis1[v]==)
{
vis1[v]=;
tmp+=dfs1(v);
}
}
return tmp;
} bool vis2[N];
bool dfs2(int u,int c)
{
if(cnt[u]!=c)
return ;
for(int i=head[u]; i!=; i=eage[i].next)
{
int v=eage[i].v;
if(vis2[v]==)
{
vis2[v]=;
return dfs2(v,c);
}
}
return ;
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
cnte=;
for(int i=; i<=m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addEage(a,b);
addEage(b,a);
}
int flag=;
for(int i=;i<=n;i++)
{
if(vis1[i]==)
{
vis1[i]=;
int cc=dfs1(i);
if(dfs2(i,cc-)==)
{
flag=;
break;
}
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return ;
}
Codeforces_791_B. Bear and Friendship Condition_(dfs)的更多相关文章
- 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 ...
- CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
- 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 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- Bear and Friendship Condition-HZUN寒假集训
Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...
- 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 653B Bear and Compressing【DFS】
题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...
- CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
随机推荐
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
- 地图上显示X,Y 坐标代码
事件数据 所有的鼠标事件都使用MouseButtonEventArgs和MouseEventArgs作为事件数据,通过这两个参数可以获取相关事件数据,使用GetPosition方法或者Source.H ...
- Ruby: 获取IE的一些信息(其实应用AutoIt脚本本身,获取这些信息更加简单)
require'win32/registry' hkey_local_machine=Win32::Registry::HKEY_LOCAL_MACHINE defgetKeyValue(hive, ...
- E20180309-hm-xa
conformance n. 顺应,一致; symmetric adj. 相称性的,均衡的; raw adj. 生的,未加工的; 无经验的; 新近完成的; 发炎的,疼痛的; exceed ...
- thinkphp5.0常遇到的错误
call a member xxxx on null 1.一般是没有继承controller: 2.对象和数组使用错误.
- Word Cloud (词云) - R
在前面已经陆续总结了如何用 Python 和 JavaScript 创建词云了,今天要说的是 R.其实 SPSS 和 SAS 的 Word Cloud 扩展模板都是基于 R 实现的. >> ...
- Word Cloud (词云) - Python
>>What's Word Cloud 词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法.目前已有多种数据分析工具支持这种图形, ...
- 前缀和小结 By cellur925
这篇主要是来介绍前缀和的QAQ. 前缀和有一维的和二维的,一维的很容易理解,高中数学必修5第二章数列给出了前n项和的概念,就是前缀和.一维的我们在这里简单说一句. 一维前缀和 预处理:在输入一个数列的 ...
- 51nod 1068 Bash游戏 V3
列出前几项可以发现是个规律题,不要被题目的文字所欺骗,字符串处理10^1000即可 #include <bits/stdc++.h> using namespace std; int ge ...
- centOS下安装JDK1.8.60,glassfish4.1.1以及MySQL
一.安装环境 操作系统 Windows7 Enterprise 64位 需要用到的软件 JDK:jdk-8u60-linux-x64.rpm Glassfish: Glassfish4.1.1.zip ...