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 ...
随机推荐
- caioj1230: [图论补充]哈密顿路径
保存模版 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> ...
- UVALive3415 Guardian of Decency —— 最大独立集
题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...
- jquery $(function(){}) $(document).ready(function(){}); (function(){}); 的区别以及作用
$(function)是$(document).ready(function(){});的简写方法 用来在DOM加载完之后执行一系列预先定义好的函数 (function(){});表示一个匿名函 ...
- VirtualBox搭建1主2从虚拟机
环境要求 最近在使用VirtualBox搭建一个实验环境,由于公司规定了所有的机器都不能使用固定IP,都必须由DHCP自动获取. 为了不影响公司整理的网络环境,只能把实验用的网络环境限制在使用内部IP ...
- 结对测试vs随机测试
在接口测试过程中,最关键的是对参数的各种情况进行测试. 随机测试是指随机选择一些参数值来测. 结对测试是指parewise算法生成较高"性价比"的组合情况来测. 随机测试存在的问题 ...
- 标准块CP功能实现
#include<stdio.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_fp; int src_ret; ]={}; ) ...
- the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-htt ...
- Qt下存储读写应用程序设置的三种方法
一.简介 用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小.位置和密码等等.有三种方法可以实现: 使用注册表: 使用配置文件(.ini): 使用自定义文件(例如.txt) ...
- [POI2001]Gra绿色游戏
Description 绿色游戏是一种两人游戏,双方分别称Ann和Billy.游戏的内容主要是轮流在棋盘上移动一颗棋子.棋盘上的点一部分是绿色的,其余是白色的:全部从1至a+b编号.编号1至a的点属于 ...
- 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon
简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单 扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并.(切法就是每出现一条与y轴平行的线段都切一刀) 对于每一个切出来的矩 ...