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. windows 下 redis安装

    在D盘新建文件夹[redis],右键解压Redis ZIP包,把所有文件解压到redis文件夹中.(其他盘符也可以滴^_^) 文件介绍: redis-benchmark.exe         #基准 ...

  2. linux下各权限的细分

    PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...

  3. idea 修改 使用的git账号

    打开控制面板-->用户账户-->凭证管理器 如下图点击进入,删除原有的账号 当在idea中再提交或下载代码时,就会弹出如下提示框: 重新输入你自己的账号就可以了.

  4. Java 动态代理 两种实现方法

    AOP的拦截功能是由java中的动态代理来实现的.说白了,就是在目标类的基础上增加切面逻辑,生成增强的目标类(该切面逻辑或者在目标类函数执行之前,或者目标类函数执行之后,或者在目标类函数抛出异常时候执 ...

  5. 直接修改java的war包

    会出现 jsp特征类型不对   descriptior UTF8  与ntfs 可以用jar命令来解决. 1.将war包移动到一个干净的路径下,使用jar xvf ROOT.war命令将war进行解压 ...

  6. 关于JavaScript全局作用域和函数作用域的拙见

    在类c的语言中,用{}引起来的部分称为块级作用域,而在JS中没有块级作用域 作用域:一个变量作用的范围:中Js中一共有两种作用: 全局作用域 - 直接编写在script标签中的JS代码,都在全局作用域 ...

  7. AttributeError: 'dict' object has no attribute 'iteritems'

    在python3.6中运行 sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse= ...

  8. c# 关闭和重启.exe程序

    Process[] myprocess = Process.GetProcessesByName("a"); if (myprocess.Count() > 0)//判断如果 ...

  9. Python开发【第三篇】:Python函数

    set     无序,不重复,可嵌套. 函数     创建函数:     1.def关键字,创建函数     2.函数名     3.()     4.函数体     5.返回值 发邮件函数 def ...

  10. 【mysql】分区表

    分区表是什么? 分区表可以按照事先创建的规则,对mysql的记录进行分组,每一个组具有一个独立的逻辑单元来存储该组的数据.典型的如:按照创建时间的年份分组,按照id的顺序分组(每1000万条数据分一个 ...