Codeforces 791B Bear and Friendship Condition(DFS,有向图)
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
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.

题目链接:http://codeforces.com/contest/791/problem/B
分析:给你m对朋友关系; 如果x-y是朋友,y-z是朋友,要求x-z也是朋友. 问你所给的图是否符合!
用DFS去找他们之间的关系,有向图去算m个点对应的边的数量,看是否相等!
下面给出AC代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=+;
int a[N];
int b[N];
int vis[N];
vector<int>vc[N];//定义一个向量
ll t;
void DFS(int x)
{
t++;//计算有关的节点没有被标记过
vis[x]=;
for(int j=;j<vc[x].size();j++)
{
if(vis[vc[x][j]]==)//如果和它相连的点没有被标记,
DFS(vc[x][j]);
}
}
int main()
{
int m,n;
int x,y;
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
memset(b,,sizeof(b));
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
vc[x].push_back(y);//找到x和y的关系
vc[y].push_back(x);
b[x]=;
b[y]=; }
ll sum=;
for(int i=;i<=n;i++)
{
if(vis[i]==&&b[i])
{
t=;
DFS(i);
sum=sum+t*(t-);//(t-1)*t求边数,完全图
}
}
if(*m!=sum)cout<<"NO"<<endl;//N个完全图的边数等于m个节点所构成的边数
else
cout<<"YES"<<endl;
}
Codeforces 791B Bear and Friendship Condition(DFS,有向图)的更多相关文章
- Codeforces 791B. Bear and Friendship Condition 联通快 完全图
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 (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 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_791_B. 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 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 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 ...
随机推荐
- python爬虫小结1
先看正则化,正则化就是描述命令和字符切分.查找.筛选等功能的方便方式. http://www.cnblogs.com/fnng/archive/2013/05/20/3089816.html 一个游戏 ...
- JavaScript闭包基本概念
闭包的概念 维基百科中是这么解释闭包的: 计算机科学中,闭包(也称为词法闭包或函数闭包)是指一个函数或函数的引用,与一个引用环境绑定在一起.这个函数环境是一个存储该函数每个非局部变量(也叫自由变量)的 ...
- xamarin android 在代码中如何设置文本颜色
xamarin android 在代码中如何设置文本颜色 TextView v = FindViewById<TextView>(Android.Resource.Id.Message); ...
- bzoj 2427: [HAOI2010]软件安装
Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和 ...
- nginx搭建rtmp协议流媒体服务器总结
最近在 ubuntu12.04+wdlinux(centos)上搭建了一个rtmp服务器,感觉还挺麻烦的,所以记录下. 大部分都是参考网络上的资料. 前提: 在linux下某个目录中新建一个nginx ...
- locust 参数,数据详解
参数 说明-h, –help 查看帮助-H HOST, –host=HOST 指定被测试的主机,采用以格式:http://10.21.32.33–web-host=WEB_HOST ...
- js生成word中图片处理
首先功能是要求前台导出word,但是前后台是分离的,图片存在后台,所以就存在跨域问题. 导出文字都是没有问题的(jquery.wordexport.js),但是导出图片就存在问题了: 图片是以链接形式 ...
- 平衡树Treap模板与原理
这次我们来讲一讲Treap(splay以后再更) 平衡树是一种排序二叉树(或二叉搜索树),所以排序二叉树可以迅速地判断两个值的大小,当然操作肯定不止那么多(不然我们还学什么). 而平衡树在排序二叉树的 ...
- 设计模式学习(四): 1.简单工厂 (附C#实现)
New 这是一个典型的情况, 我们需要在运行时来实例化一些具体的类. 在需要修改或者扩展的时候我们就需要改这段代码. 一个程序中可能会多次出现类似的代码, 这使得维护和更新非常困难而且容易出错. 通过 ...
- while100以内的偶数
#显示100以内的偶数 #声明i i = 1 #开始循环条件为i不等于100,执行while代码块 while i != 100: #给i加1 i +=1 #如果循环到此时i的取余运算为0则打印i i ...