Codeforces 791B. Bear and Friendship Condition 联通快 完全图
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
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.
#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 联通快 完全图的更多相关文章
- 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 ...
- 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 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 ...
- 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 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- CodeForce-791B Bear and Friendship Condition(并查集)
Bear Limak examines a social network. Its main functionality is that two members can become friends ...
- 【CF771A】Bear and Friendship Condition
题目大意:给定一张无向图,要求如果 A 与 B 之间有边,B 与 C 之间有边,那么 A 与 C 之间也需要有边.问这张图是否满足要求. 题解:根据以上性质,即:A 与 B 有关系,B 与 C 有关系 ...
- CF #405 (Div. 2) B. Bear ad Friendship Condition (dfs+完全图)
题意:如果1认识2,2认识3,必须要求有:1认识3.如果满足上述条件,输出YES,否则输出NO. 思路:显然如果是一个完全图就输出YES,否则就输出NO,如果是无向完全图则一定有我们可以用dfs来书边 ...
- Bear and Friendship Condition-HZUN寒假集训
Bear and Friendship Condition time limit per test 1 secondmemory limit per test 256 megabytesinput s ...
随机推荐
- java.lang.StringIndexOutOfBoundsException: String index out of range: 0
hibernet 报错 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 处理方法 数据表字段为char ...
- 用JS和JQ来获取子节点!
用JS和JQ来获取子节点! 在JS中,如果通过document.getElementsByTagName来获取子元素有个弊端:它不单会获取符合要求的子元素,就连同孙元素也会获取.如果有特殊要求,那 ...
- vue 父向子组件传递数据,子组件向父组件传递数据方式
父组件向子组件传递数据通过props,子组件引入到父组件中,设置一个值等于父组件的数据,通过:bind将数据传到子组件中,子组件中通过props接收父组件的数据,这样就可以使用父组件的数据了,循环组件 ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- AlertDialog 无法去掉自带的白边
项目中开始采用AlertDialog ,根据要求要显示加圆角.但是设置圆角的背景后,会存在白边. 按网上提示的设置透明的背景都不可以. 最后采用Dialog.
- Java虚拟机1
Java内存区域 程序计数器(Program Counter Register):记录当前线程所执行字节码的行号指示器.字节码解释器工作时,判断是循环,分支,跳转,异常等条件,然后更新这个计数器的值来 ...
- 安装Eclipse Maven插件的几种方法
文章出处:http://blog.csdn.net/lfsfxy9/article/details/9397937 感谢作者的分享! 昨天直接在机器上配置了Maven环境,今天顺便把Eclipse等I ...
- oracle 查询列表中选取其中一行
select k.SAL from (select SAL,rownum rn from (select SAL from SCOTT.EMP where MGR = 7698 order by SA ...
- centos 7 redis-4.0.11 哨兵
redis-master:192.168.199.223 redis-slave_1: 192.168.199.224 redis-slave_2: 192.168.199.252 redis-mas ...
- poj 1789 prime
链接:Truck History - POJ 1789 - Virtual Judge https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...