CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO.
思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边和点
n个节点的有向完全图边数为e=n*(n-1)
代码:
#include <bits/stdc++.h>
#define maxn 150000
#define ll long long
using namespace std; vector <int> k[maxn+];
bool visit[maxn+];
int t; void dfs(int x,int &v,int &e)
{
assert(!visit[x]);
v++;
visit[x]=true;
e+=k[x].size();
for(int i=;i<k[x].size();i++)
{
if(visit[k[x][i]]==)
dfs(k[x][i],v,e);
}
} int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=;i<m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
k[x].push_back(y);
k[y].push_back(x);
}
for(int i=;i<=n;i++)
{
if(!visit[i])
{
int v=,e=;
dfs(i,v,e);
if(e!=(long long) v*(v-))
{
cout<<"NO"<<endl;
return ;
}
}
}
cout<<"YES"<<endl;
}
return ;
}
CF #405 (Div. 2) B. Bear ad 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 ...
- 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 ...
- 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 联通快 完全图
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 ...
- CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
随机推荐
- PHP基础学习(函数一)
PHP(Hypertext Preprocessor):超文本预处理器,一种嵌入在HTML中并且运行在服务器端的脚本语言. var_dump--打印变量相关信息 说明: <?php var_d ...
- C# 图片平移及缩放
1.图片平移 Monitor.rar 在CSDN上下载,是个有地图编辑功能. http://download.csdn.net/detail/gxingmin/883699 2.图片缩放 http:/ ...
- python 写的几道题
''' #乘法口诀''' for i in range(1,10): for j in range(1,i+1): print("%d*%d=%2d" % (i,j,i*j),en ...
- 服务器数据库搭建流程(CentOs+mysql)
前言: 服务器上数据库搭建需要知道Linux系统的版本,以前的Ubuntu14.04直接在终端下输入apt-get install (package)便可方便的下载并安装mysql,但是在centOs ...
- 【JS】JavaScript中的闭包
在JavaScript中,闭包指的是有权访问另一个函数作用域中的变量的函数:创建闭包最常见的方式就是在一个函数内创建另一个函数.如下例子: function A(propertyName){ retu ...
- Java面试02|Java集合
关于Java中并发集合有: (1)CouncurrentHashMap (2)CopyOnWriteArrayList (3)LinkedBlockingQueue (4)ArrayBlockingQ ...
- 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛
2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 252 Solved: 1 ...
- yaourt 之 Curl 错误
最近执行 yaourt 更新时总是出现以下错误: curl error: Couldn't connect to server 无法进行更新.把配置中的下载工具更换了成 axel 等其它下载工具,还是 ...
- css中设置div垂直水平居中的方法
设置要水平垂直居中的div的position为absolute,left:50%;margin-left为负的这个元素宽度的一半,同理,top:50%;margin-top为负的这个元素的高度的一半. ...
- protected private public 的区别
1.public,protected,private是Java里用来定义成员的访问权限的,另外还有一种是"default",也就是在成员前不加任何权限修饰符.如: publi ...