B. Bear and Friendship Condition----并查集或BFS
2017-03-28 21:31
549 查看
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 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.
Input
The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000,
![](http://codeforces.com/predownloaded/42/25/42252ce0112fbfd8e396b9ee524bb6cfe2cd93d4.png)
) —
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
output
input
output
input
output
input
output
Note
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.
![](http://codeforces.com/predownloaded/3d/a7/3da745afd71f6f50df3d203450291984fe6e3d4a.png)
题目链接:http://codeforces.com/contest/791/problem/B
题目的意思不难懂,就是说1和2是朋友,2和3是朋友,检查1和3是不是朋友,虽然题目不是这个意思,但是考点在这。
我第一发强行并查集,然后超时了,然后强行bfs,然后wa在了第29组,然后改了long long,然后就过了......比赛中我可能会被hack掉,因为我可能不会注意那个long long 的问题,赛后虚拟和赛时还是不一样啊。
题目很好做,就是判断每一个小圈子的完全图的边数和m比较一下就可以了
赛后看了并查集代码,宇航菊苣告诉我并查集要写路径压缩,以前做题并没有卡过我路径压缩,受教。
并查集代码:
bfs代码:
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 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.
Input
The first line of the input contain two integers n and m (3 ≤ n ≤ 150 000,
![](http://codeforces.com/predownloaded/42/25/42252ce0112fbfd8e396b9ee524bb6cfe2cd93d4.png)
) —
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
Note
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.
![](http://codeforces.com/predownloaded/3d/a7/3da745afd71f6f50df3d203450291984fe6e3d4a.png)
题目链接:http://codeforces.com/contest/791/problem/B
题目的意思不难懂,就是说1和2是朋友,2和3是朋友,检查1和3是不是朋友,虽然题目不是这个意思,但是考点在这。
我第一发强行并查集,然后超时了,然后强行bfs,然后wa在了第29组,然后改了long long,然后就过了......比赛中我可能会被hack掉,因为我可能不会注意那个long long 的问题,赛后虚拟和赛时还是不一样啊。
题目很好做,就是判断每一个小圈子的完全图的边数和m比较一下就可以了
赛后看了并查集代码,宇航菊苣告诉我并查集要写路径压缩,以前做题并没有卡过我路径压缩,受教。
并查集代码:
#include <cstdio> #include <cstring> #include <iostream> #define LL long long using namespace std; int pre[151000]; int Find(int x){ return x==pre[x]?x:pre[x]=Find(pre[x]);//以后要注意路径压缩 } void mix(int x,int y){ int xx=Find(x),yy=Find(y); if(xx!=yy){ pre[xx]=yy; } } LL t[151000]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ pre[i]=i; } for(int i=0;i<m;i++){ int x,y; scanf("%d%d",&x,&y); mix(x,y); } for(int i=1;i<=n;i++){ t[Find(i)]++; } LL sum=0; for(int i=1;i<=n;i++){ if(t[i]!=0&&t[i]!=1){ sum+=(t[i]*(t[i]-1)/2); } } if(sum!=m){ printf("NO\n"); } else{ printf("YES\n"); } return 0; }
bfs代码:
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #define LL long long using namespace std; struct node{ int u,v,pre; }edge[400000]; int nEdge,p[151000]; bool vis[151000]; void Init(){ memset(p,-1,sizeof(p)); memset(vis,false,sizeof(vis)); nEdge=0; } void connect(int u,int v){ edge[nEdge].u=u; edge[nEdge].v=v; edge[nEdge].pre=p[u]; p[u]=nEdge++; edge[nEdge].u=v; edge[nEdge].v=u; edge[nEdge].pre=p[v]; p[v]=nEdge++; } int bfs(int x){//强行代替了并查集的功能。。。 queue<int>Q; Q.push(x); vis[x]=true; int ans=0; while(!Q.empty()){ int t=Q.front(); Q.pop(); ans++; for(int i=p[t];~i;i=edge[i].pre){ int v=edge[i].v; if(!vis[v]){ vis[v]=true; Q.push(v); } } } return ans; } int main(){ int n; LL m; scanf("%d%I64d",&n,&m); Init(); for(int i=0;i<m;i++){ int x,y; scanf("%d%d",&x,&y); connect(x,y); } LL sum=0; for(int i=1;i<=n;i++){ if(!vis[i]){ LL ans=bfs(i); sum+=(ans*(ans-1)/2); } } if(sum!=m){ printf("NO\n"); } else{ printf("YES\n"); } return 0; }
相关文章推荐
- G - Bear and Friendship Condition (并查集)
- Bear and Friendship Condition CodeForces - 771A(图论,并查集)题解
- Codeforces Round #405 B.Bear and Friendship Condition【Dfs+思维】
- Codeforces791 B. Bear and Friendship Condition
- Codeforces_791_B. Bear and Friendship Condition_(dfs)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
- Codeforces Round #405 B.Bear and Friendship Condition
- Bear and Friendship Condition
- (codeforces)A. Bear and Friendship Condition
- Codeforces 791 B Bear and Friendship Condition 规律
- codeforces 791 B. Bear and Friendship Condition (DFS搜索)
- codeforces round #405 B. Bear and Friendship Condition
- 【codeforces 791B】Bear and Friendship Condition
- (DFS, 图)Codeforces Round #405 B. Bear and Friendship Condition
- India and China Origins---hdu5652(二分 + bfs)或者(并查集)
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
- HDU 1312 Red and Black(并查集或者BFS)
- 【HDU5652 BestCoder Round 77 (div1) B】【并查集 or 二分答案BFS】India and China Origins 两国中间出现山脉 最早时间断绝联系
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树★ ★