Bear and Big Brother

签到题,直接模拟就可以了。

 

Bear and Friendship Condition

满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的。

然后并查集求每个朋友圈大小再判断是否合法就可以啦。

#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
#define maxn 300000
#define rep(i,l,r) for(int i=l;i<=r;i++)
using namespace std;

LL size[maxn],e[maxn];
int fa[maxn],n,m; 

int find(int x)
{
    if (fa[x]!=x) return fa[x]=find(fa[x]);
    return x;
}
int main()
{
    scanf("%d %d",&n,&m);
    rep(i,1,n) fa[i]=i,size[i]=1,e[i]=0;
    rep(i,1,m) {
        int j,k;
        scanf("%d %d",&j,&k);
        int fa1=find(j),fa2=find(k);
        if (fa1!=fa2) fa[fa2]=fa1,e[fa1]+=e[fa2],size[fa1]+=size[fa2];
        ++e[fa1];
    }
    int flag=1;
    rep(i,1,n)
        if (fa[i]==i && size[i]*(size[i]-1)!=e[i]*2) {
            flag=0;
    //        printf("%d %d %d\n",i,size[i],e[i]);
            break;
        } 
    printf("%s\n",flag?"YES":"NO");
    return 0;
}
View Code

相关文章:

  • 2021-10-22
  • 2021-11-01
  • 2021-09-06
  • 2021-07-15
  • 2021-10-06
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-02
  • 2021-09-09
  • 2021-09-13
  • 2021-10-29
  • 2021-07-15
  • 2021-07-04
  • 2021-11-06
相关资源
相似解决方案