Codeforces_791_B. Bear and Friendship Condition_(dfs)
1 second
256 megabytes
standard input
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.
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.
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).
4 3
1 3
3 4
1 4
YES
4 4
3 1
2 3
3 4
1 2
NO
10 4
4 3
5 10
8 9
1 2
YES
3 2
1 2
2 3
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)的更多相关文章
- 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 ...
- CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
- 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 ...
- 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 ...
- Codeforces 791B. Bear and Friendship Condition 联通快 完全图
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
- Bear and Friendship Condition-HZUN寒假集训
Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...
- Codeforces791 B. Bear and Friendship Condition
B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces 653B Bear and Compressing【DFS】
题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...
- CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
随机推荐
- ubuntu12.04 64位系统配置jdk1.6和jdk-6u20-linux-i586.bin下载地址
1:下载地址http://code.google.com/p/autosetup1/downloads/detail?name=jdk-6u20-linux-i586.bin&can=2&am ...
- shell网络管理
背景知识 联网就是通过网络将主机进行互联并采用不同的规范配置网络上的节点.我们以 TCP/IP 作为网络栈,所有的操作都是基于它进行的.网络是计算机系统中重要的部分.连接在网络上的每个节点都分配了一个 ...
- redhat输入用户名密码后又跳回到登录
一.如果忘记密码,可以进入single模式修改密码: 1.进入linux启动界面之后按e进入如下界面 2.按选择kernel /vmlinuz-2.4.20-8 ro root=LABEL=/ 项,按 ...
- [Usaco2015 DEC] Counting Haybales
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4392 [算法] 线段树 时间复杂度 : O(MlogN) [代码] #include ...
- 谁动了我的cpu——oprofile使用札记
引言 cpu无端占用高?应用程序响应慢?苦于没有分析的工具? oprofile利用cpu硬件层面提供的性能计数器(performance counter),通过计数采样,帮助我们从进程.函数.代码层面 ...
- css3 vw -----解决页面滚动出现跳动的bug
100vw相对于浏览器的window.innerWidth,是浏览器的内部宽度,注意,滚动条宽度也计算在内!而100%是可用宽度,是不含滚动条的宽度. demo: h1{font-size:8vw;} ...
- 学习笔记::AC自动机
最先开始以为和自动刷题机是一个东西... 其实就是kmp的一个拓展.学完kmp再学这个就会发现其实不难 1.kmp是一个串匹配一个串,但是当我们想用多个串匹配一个文本的时候,kmp就不行了,因此我们有 ...
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
转自:https://blog.csdn.net/myyugioh/article/details/7724915 今天在导入一个工程时,编译并打包到Tomcat后,发现出现java.lang.Uns ...
- idea设置控制台不打印日志
这样做的好处是当想打印数据到控制台查看就特别方便,这个在大数据spark sql使用的多.当然如果代码报错也会打印,这个不必担心. 方案Ⅰ 方法是将这个log日志文件放到idea的资源目录里即可 lo ...
- 转载别人的ognl
一.循环遍历集合 1.在jsp中引入标准函数声明<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix=" ...