您的位置:首页 > 其它

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, 

) —
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/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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: