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. ABAP 常用函数

    函数名 描述 SD_VBAP_READ_WITH_VBELN 根据销售订单读取表vbap中的信息EDIT_LINES 把READ_TEXT返回的LINES中的行按照TDFORMAT=“*”重新组织VI ...

  2. crontab -e文件存放路径

    crontab -e结果存放在/var/spool/cron/crontabs中

  3. python找包的路径(找不到自定义包的问题解决)

    问题:工程下自定义的包,python在执行时经常找不到包   python找包的路径:python安装路径下的lib包和PYTHONPATH下的包     可以使用[sys.path]打印出pytho ...

  4. 分布式监控系统Zabbix-3.0.3-完整安装记录

    由于采用sendmail发送邮件,常常会被认为是垃圾邮件被拒,所以不推荐这种方式!这里,针对zabbix报警信息的发送,可以采用下面两种方式中的任意一种:1)利用sendEmail程序来发送报警邮件. ...

  5. 第四章 栈与队列(a)栈接口与实现

  6. A class of finite groups with abelian 2-Sylow subgroups By CHIH-HAN SAH

    Remark: 1.An element of a group which conjugate to its inverse is called a real element. If $G$ has ...

  7. Django项目之客户

    关于客户的操作 主页(被继承) {% load static %} <!DOCTYPE html> <html lang="en"> <head> ...

  8. http://www.bugku.com:Bugku——jsfuckWEB5(http://120.24.86.145:8002/web5/index.php)

      今天又做了bugku上面的一道题.使用到了jsfuck,它是什么捏?   它是Javascript原子化的一种简易表达方式,用[]()!+就可以表示所有的Javascript字符,不依赖于浏览器. ...

  9. 安装vCenter server 6.0

    注意,5.5的还可以直接把iso里的ova直接导入为模板,6.0之后的要拉到Windows下安装. 总路线 ESXI是服务器系统,用vsphere client连接,在client里新建一个虚拟机为W ...

  10. crm作业知识点集合[三]

    知识点1 我们要实现一个这样的功能,在学生表中,可以查看每个学生的报名的班级的所有的成绩,就是下图的效果 1.首先我们需要在学生表中自定义一列,这一列的内容就是一个a标签,指向另外一个页面,而我们在另 ...