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. Lightoj 1020 - A Childhood Game

    Allice先拿,最后拿球的输. Bob先拿,最后拿球的赢. 考虑Alice先拿球,当n=1时 Alice输  记dp[1]=0; n=2,  dp[2]=1 n=3,  dp[3]=1 因为n=1, ...

  2. POJ1679 The Unique MST —— 次小生成树

    题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  3. xcode 8.1 (8B62)真机调试配置

    1.点击菜单栏中的Xcode->Preferences->Accounts,如图: 点击上图左下角中的“+”号,登陆一个Apple id(前提已经有了一个apple id账号), 2.然后 ...

  4. BZOJ3732:Network(LCT与最小生成树)

    给你N个点的无向图 ( <= N <= ,),记为:…N. 图中有M条边 ( <= M <= ,) ,第j条边的长度为: d_j ( < = d_j < = ,,, ...

  5. 协议森林02 小喇叭开始广播 (以太网与WiFi协议)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. “小喇叭开始广播啦”,如果你知道这个,你一定是老一辈的人.“小喇叭”是五十年代到八十年代的儿童广播 ...

  6. 【黑金教程笔记之006】【建模篇】【Lab 05 SOS信号之一】—笔记

    sos_module.v是产生SOS信号的功能模块.即有次序的输出莫斯码:点.画.间隔.control_module.v是一个定时触发器,每一段时间使能sos_module.v. 模块: /***** ...

  7. bzoj 4816: [Sdoi2017]数字表格【莫比乌斯反演+逆元】

    把题意简化,就是要求 \[ \prod_{d=1}^{min(n,m)}f[d]^{\sum_{i=1}^{n}\sum_{j=1}^{m}e[gcd(i,j)==d]} \] 把幂用莫比乌斯反演转化 ...

  8. Unexpected EOF 远程主机强迫关闭了一个现有的连接 如何处理

    由于数据量的增大,调用接口的次数会增加. 当连续向目标网站发送多次request后,目标网站可能会认为是,恶意攻击. 于是会抛出requests异常. 测试代码: for i in range(200 ...

  9. Data Center Maintenance CodeForces - 950E

    http://codeforces.com/contest/950/problem/E 贴一份板子 #include<cstdio> #include<vector> #inc ...

  10. 贪心 HDOJ 5385 The Path

    题目传送门 /* 题意:给一张图和一些有向边,问如何给边赋值使得d1 < d2 < .. < dx > ,,,< ddn 贪心:左边从2开始,右边从n开始,每次选择未标记 ...