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 nm 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

题意:给定一个图(盆友关系),判断盆友关系是否合理(若A-B,B-C,则A-C)。

思路:先dfs判断一个盆友圈有圈有c人,若盆友圈合理,那么其中所有点的度都是c-1,再用dfs判断即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 150005 struct Eage
{
int v,next;
} eage[N*];
int head[N],cnt[N],cnte; void addEage(int a,int b)
{
eage[cnte].v=b;
eage[cnte].next=head[a];
head[a]=cnte++;
cnt[a]++;
} bool vis1[N];
int dfs1(int u)
{
int tmp=;
for(int i=head[u]; i!=; i=eage[i].next)
{
int v=eage[i].v;
if(vis1[v]==)
{
vis1[v]=;
tmp+=dfs1(v);
}
}
return tmp;
} bool vis2[N];
bool dfs2(int u,int c)
{
if(cnt[u]!=c)
return ;
for(int i=head[u]; i!=; i=eage[i].next)
{
int v=eage[i].v;
if(vis2[v]==)
{
vis2[v]=;
return dfs2(v,c);
}
}
return ;
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
cnte=;
for(int i=; i<=m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
addEage(a,b);
addEage(b,a);
}
int flag=;
for(int i=;i<=n;i++)
{
if(vis1[i]==)
{
vis1[i]=;
int cc=dfs1(i);
if(dfs2(i,cc-)==)
{
flag=;
break;
}
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return ;
}

Codeforces_791_B. Bear and Friendship Condition_(dfs)的更多相关文章

  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. CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)

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

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

  5. Codeforces 791B. Bear and Friendship Condition 联通快 完全图

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

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

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

  7. Codeforces791 B. Bear and Friendship Condition

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

  8. Codeforces 653B Bear and Compressing【DFS】

    题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...

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

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

随机推荐

  1. mysql14---手动备份

    PHP定时完成数据库的备份 1.手动备份数据库(表的)方法 cmd控制台(windows指令): mysqldump –u root –proot 数据库 [表名1 表名2..] > 文件路径 ...

  2. div+css布局教程系列1

    <!doctype html><html><head><meta charset="utf-8"><title>简单布局 ...

  3. BZOJ 4815 数论

    今年的重庆省选? 具体就是,对于每次修改,A[p,q]这个位置,  设d=gcd(p,q) ,则 gcd为d的每一个格子都会被修改,且他们之间有个不变的联系 A[p,q]/p/q==A[k,t]/k/ ...

  4. bootstrap 表单元素、按钮、链接的禁用

    在Bootstra中,表单元素,按钮通过在标签内设置 disabled 或 disabled="disabled" 可以禁用表单元素,按钮.链接需要加入class="di ...

  5. java日期和时间戳格式互转

    // 将日期格式转换成时间戳 public static void main(String[] args) throws Exception{ String time = "2018-05- ...

  6. Day.js 是一个仅 2kb 大小的轻量级 JavaScript 时间日期处理库,和 Moment.js 的 API 设计保持完全一样,dayjs

    https://gitee.com/mirrors/Day.js api: https://gitee.com/mirrors/Day.js/blob/master/docs/zh-cn/API-re ...

  7. 三种实现AJAX的方法以及Vue和axios结合使用的坑

    前言 之前曾经想自己写一个天气的App,并找到了一个免费的天气数据接口的服务商--和风天气,当时也不懂怎么发HTTP请求,而且也只会用Java语言,看到官方文档里面有一个Java代码示例,就复制了一下 ...

  8. mysql 状态查询

    select  COUNT(case when info.State = '0' then State end  ) as daichuliCount, COUNT(case when info.St ...

  9. NOIp 2017 奶酪 【并查集】 By cellur925

    题目传送门 Orz去年考场上做这道题的我应该还在抱怨没学过空间几何,不一会太困了就开始打瞌睡,然后为了防止睡觉开始在devc++上写默写离骚(逃 思路:如果两个空洞相交,那么把他们并在一个集合里.最后 ...

  10. Luogu P3619 魔法 【贪心/微扰证明】

    题目描述 cjwssb知道是误会之后,跟你道了歉.你为了逗笑他,准备和他一起开始魔法.不过你的时间不多了,但是更惨的是你还需要完成n个魔法任务.假设你当前的时间为T,每个任务需要有一定的限制ti表示只 ...