Codeforces Round #405 Bear and Friendship Condition 并查集

题目:

A. 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 nm 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 (XYZ), if X-Y and Y-Zthen 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 aiand 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

这个题就是问对于每个联通分量,是不是完全图。

我们可以通过并查集来处理连通分量的问题,例如求连通分量的个数就是求根节点的个数。求这个连通分量中有多少个点,就是求根节点与多少个点相邻(包括根节点本身)。判断每个连通分量是不是完全图,可以通过判断这个连通分量中的每个点的度数是否等于与根节点相邻点数-1。

code:61ms

#include<cstdio>
const int MAXN=1.5e5+5;
int par[MAXN],r[MAXN],top[MAXN];
int find(int x){
    return par[x]==x?par[x]:par[x]=find(par[x]);
}
void unit(int x,int y){
    x=find(x);
    y=find(y);
    if(x!=y)par[x]=y;
}
int main(){
    int n,m;scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)par[i]=i;
    for(int i=0;i<m;++i){
        int a,b;scanf("%d%d",&a,&b);
        r[a]++;
        r[b]++;//r记录每个点的度数
        unit(a,b);
    }
    for(int i=1;i<=n;++i){
        int x=find(i);
        top[x]++;//记录每个根节点与多少个点相连。
    }
    for(int i=1;i<=n;++i){
        int x=find(i);
        if(r[i]!=top[x]-1){
            printf("NO\n");
            return 0;
        }
    }
    printf("YES\n");
    return 0;
}

另外一种写法是构造图,然后通过dfs来找连通块。

code:77ms

/*暨通过计算每一个连通块如果是完全图那么应该有多少条边
  再与题目已知边数进行比较
*/
#include<cstdio>
#include<cstring>
const int MAXN=2e5;
struct edge{int v,next;}es[MAXN*2];
int head[MAXN],tot;
bool visit[MAXN],has[MAXN];
long long t;
void init(){
    tot=0;
    memset(head,-1,sizeof(head));
}
void addEdge(int a,int b){
    es[tot].v=b;
    es[tot].next=head[a];
    head[a]=tot++;
}
void dfs(int no){
    t++;
    visit[no]=true;
    for(int i=head[no];i!=-1;i=es[i].next){
        if(!visit[es[i].v])dfs(es[i].v);
    }
}
int main(void){
    int n,m;scanf("%d%d",&n,&m);
    init();
    memset(visit,false,sizeof(visit));
    memset(has,false,sizeof(has));
    for(int i=0;i<m;++i){
        int a,b;scanf("%d%d",&a,&b);
        has[a]=true;has[b]=true;
        addEdge(a,b);
        addEdge(b,a);
    }
    bool noAns=false;
    long long sum=0;
    for(int i=1;i<=n;++i){
        if(has[i]&&!visit[i]){
            t=0;
            dfs(i);
            sum+=t*(t-1);
            //printf("t=%d\n",t);
        }
    }
    if(sum==2*m)printf("YES\n");
    else printf("NO\n");
}

这个方法的构图方式可以换一换用容器来写,感觉这样比较好看一些://来源:ACM进阶之路

code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=150000+100;
int b[N];
int vis[N];
vector<int>vc[N];//定义一个向量
ll t;
void DFS(int x)
{
    t++;//计算有关的节点没有被标记过
    vis[x]=1;
    for(int j=0;j<vc[x].size();j++)
    {
        if(vis[vc[x][j]]==0)//如果和它相连的点没有被标记,
        DFS(vc[x][j]);
    }
}
int main()
{
    int m,n;
    int x,y;
    scanf("%d%d",&n,&m);
    memset(vis,0,sizeof(vis));
    memset(b,0,sizeof(b));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        vc[x].push_back(y);//找到x和y的关系
        vc[y].push_back(x);
        b[x]=1;
        b[y]=1;
    }
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0&&b[i])
        {
            t=0;
            DFS(i);
            sum=sum+t*(t-1);//(t-1)*t求边数,完全图
        }
    }
    if(2*m!=sum)cout<<"NO"<<endl;//N个完全图的边数等于m个节点所构成的边数
    else
        cout<<"YES"<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值