B. Bear and Friendship Condition
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output: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.

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).

Examples
Input
4 3
1 3
3 4
1 4
Output
YES
Input
4 4
3 1
2 3
3 4
1 2
Output
NO
Input
10 4
4 3
5 10
8 9
1 2
Output
YES
Input
3 2
1 2
2 3
Output
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.

题目链接:http://codeforces.com/contest/791/problem/B

题意:求一个无向图的每个联通快是否都是完全图。
思路:dfs搜索或者并查集。m条边和n个点的无向完全图满足关系m=n*(n-1)/2,或者每个点的度de[i]都满足de[i]+1=n;
代码:

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=2e5+,INF=0x3f3f3f3f,MOD=1e9+;
int n,m;
map<int,vector<int> >G;
int vis[MAXN];
int current_cc=;
map<int,vector<int> >cc;
void dfs(int u)
{
vis[u]=;
cc[current_cc].push_back(u);
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(!vis[v]) dfs(v);
}
}
void find_cc()
{
current_cc=;
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
if(vis[i]) continue;
current_cc++;
dfs(i);
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
find_cc();
int ans=;
for(int i=; i<=current_cc; i++)
{
long long cou=;
long long x=cc[i].size();
for(int j=; j<x; j++)
{
int u=cc[i][j];
cou+=G[u].size();
}
cou=cou/;
if(cou!=x*(x-)/) ans=;
}
if(ans==) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return ;
}

dfs求联通快

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=2e5+,INF=0x3f3f3f3f,MOD=1e9+;
map<int,vector<int> >G;
queue<int>Q;
int pa[MAXN];
int findset(int x)
{
return pa[x]==x?x:pa[x]=findset(pa[x]);
}
ll de[MAXN];
ll x[MAXN];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++) pa[i]=i,de[i]=;
for(int i=; i<=m; i++)
{
int u,v;
scanf("%d%d",&u,&v);
de[u]++,de[v]++;
int fau=findset(u),fav=findset(v);
if(fau>fav) pa[fau]=fav;
else pa[fav]=fau;
}
for(int i=; i<=n; i++)
x[findset(i)]++;;
int ans=;
for(int i=;i<=n;i++)
{
//cout<<pa[i]<<" "<<x[pa[i]]<<" "<<de[i]<<endl;
if(x[pa[i]]!=de[i]+) ans=;
if(ans==) break;
}
if(ans==) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return ;
}

并查集

Codeforces 791B. Bear and Friendship Condition 联通快 完全图的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces791 B. Bear and Friendship Condition

    B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...

  5. 【codeforces 791B】Bear and Friendship Condition

    [题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...

  6. CodeForce-791B Bear and Friendship Condition(并查集)

    Bear Limak examines a social network. Its main functionality is that two members can become friends ...

  7. 【CF771A】Bear and Friendship Condition

    题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...

  8. CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)

    题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...

  9. Bear and Friendship Condition-HZUN寒假集训

    Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...

随机推荐

  1. 微信小程序----搜索框input回车搜索事件

    在微信小程序里的搜索框,按软键盘回车键触发搜索事件. <input type="text"  placeholder="搜索" value="{ ...

  2. 2017秋季面向对象程序设计(Java)教材、教学纲要、考核要求

    教材简况 凯 S.霍斯特曼 (Cay S. Horstmann)(作者), 周立新(译者), Java核心技术(卷1):基础知识(原书第10版) , 2016年9月1出版 本书包括两卷,选做教材的是卷 ...

  3. 关于python中的is和==的区别

    Python 中的比较:is 与 ==   在 Python 中会用到对象之间比较,可以用 ==,也可以用 is .但是它们的区别是什么呢? is 比较的是两个实例对象是不是完全相同,它们是不是同一个 ...

  4. Mysql5.6 导出sql文件数据导入到5.7

    由于在linux安装了mysql5.7,在需要导入数据时发现报错,说时间默认值不能为0,因为之前用的是mysql5.6 的版本.经过网上百度查找方法,发现是mysql的sql_mode值的问题,于是就 ...

  5. NPM安装依赖速度慢问题

    [NPM安装依赖速度慢问题] npm config set registry http://registry.npm.taobao.org 参考:http://blog.csdn.net/rongbo ...

  6. apache 多并发测试

    进入到apache的bin目录打开  temp.bat 如果没有,新建temp.bat,打开输入 命令:ab -c 一次并发的数量 -n 总共请求的数量 请求的地址 例:ab -c 10 -n 100 ...

  7. 牛客网练习赛44-B(快速幂+模拟)

    题目链接:https://ac.nowcoder.com/acm/contest/548/B 题意:计算m/n小数点后k1位到k2位,1≤m≤n≤109,1<=k1<=k2<=109 ...

  8. 第三章 列表(a)接口与实现

  9. Maven clean基本命令

    转自--------------------------------------https://www.cnblogs.com/hiver/p/7850954.html 假设现有项目结构如下 dail ...

  10. 微信小程序开发——设置默认图片、错误加载图片

    小程序不支持h5中的onerrorimg,只开放了binderror属性,当错误发生时,会发布到 AppService,事件对象event.detail = {errMsg: 'something w ...