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 ...
随机推荐
- redis编译安装
centos7 redis-4.0.6 make 报错 jemalloc.h: No such file or directory make MALLOC=libc MALLOC默认值是jemallo ...
- 微信小程序实现滚动分页加载更多
参考网址:https://www.cnblogs.com/Smiled/p/8203306.html 1.wxml: <view class='myScroll' style='float:le ...
- extJS 动态引用加载(转)
ExtJs有庞大的类型库,很多类可能在当前的页面根本不会用到,我们可以引入动态加载的概念来即用即取.这些代码都要写在Ext.onReady外面. 1.动态引用外部Js //加载配置可用 Ext.Loa ...
- android 区分wifi是5G还是2.4G(转)
http://bbs.csdn.net/topics/391033966?page=1 我一开始看这帖子,找不到答案,为了后来的人,我来回复吧.WifiManager wifiManager = (W ...
- ASP.NET 在请求中检测到包含潜在危险的数据,因为它可能包括 HTML 标记或脚本
<textarea><%=Server.HtmlEncode(strContent)%></textarea> 转载:https://www.cnblogs.com ...
- jdk1.8的项目在jdk1.7的环境下运行
- elk6快速安装
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch [elasticsearch-6.x] name=Elasticsear ...
- 二叉树中和为某一值的路径(python)
题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...
- 第十章 优先级队列 (b3)完全二叉堆:删除与下滤
- 200. Number of Islands (Graph)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...