【并查集】问题 A: Bovine Alliance

题目描述

Bessie and her bovine pals from nearby farms have finally decided that they are going to start connecting their farms together by trails in an effort to form an alliance against the farmers.  The cows in each of the N (1 <= N
<= 100,000) farms were initially instructed to build a trail to exactly one other farm, for a total of N trails.  However months into the project only M (1 <= M < N) of these trails had actually been built.

Arguments between the farms over which farms already built a trail now threaten to split apart the cow alliance.  To ease tension, Bessie wishes to calculate how many ways the M trails that exist so far could have been built.  For example, if there is a trail connecting farms 3 and 4, then one possibility is that farm 3 built the trail, and the other possibility is that farm 4 built the trail.  Help Bessie by calculating the number of different assignments of trails to the farms that built them, modulo 1,000,000,007.  Two assignments are considered different if there is at least one trail built by a different farm in each assignment.

输入

* Line 1: Two space-separated integers N and M
* Lines 2..1+M: Line i+1 describes the ith trail.  Each line contains  two space-separated integers u_i and v_i (1 <= u_i, v_i <= N, u_i != v_i) describing the pair of farms connected by the  trail.
 

输出

* Line 1: A single line containing the number of assignments of trails to farms, taken modulo 1,000,000,007.  If no assignment satisfies the above conditions output 0.
 

样例输入 Copy

5 4
1 2
3 2
4 5
4 5

样例输出 Copy

6

提示

Note that there can be two trails between the same pair of farms.There are 6 possible assignments.  Letting {a,b,c,d} mean that farm 1 builds trail a, farm 2 builds trail b, farm 3 builds trail c, and farm 4 builds trail d, the assignments are:
{2, 3, 4, 5}
{2, 3, 5, 4}
{1, 3, 4, 5}
{1, 3, 5, 4}
{1, 2, 4, 5}
{1, 2, 5, 4}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
///树 n
///1个环/环+外向数 :2
///多个环:0
int fa[100005];
int findfa(int x)
{
     if(fa[x]==x) return x;
     return fa[x]=findfa(fa[x]);
}
const ll mod=1000000007;
int huan[100005];
int son[100005];
int main()
{
    int n,m,x,y;
    scanf("%d%d",&n,&m);
    ///初始化
    for(int i=1;i<=n;i++)
        fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        int fx=findfa(x);
        int fy=findfa(y);
        if(fx==fy)///如果x,y已经在一颗树上了,
            huan[fx]++;///再在x,y之间加上一条边的话,就会形成一个环
                  ///这里就标记huan[fx]或者huan[fy]
        else
            fa[fx]=fy;
    }
    ///更新父节点
    for(int i=1;i<=n;i++)
        findfa(i);
     for(int i=1;i<=n;i++)
     {
         son[fa[i]]++;
         if(i!=fa[i])///此时i节点不是顶级节点
         huan[fa[i]]+=huan[i];///要将i拥有的环数加到顶级节点里面
     }
     ll ans=1;
     for(int i=1;i<=n;i++)
     {
         if(fa[i]==i) ///对于每一个顶级节点而言,相当于对于每一个联通块而言
         {
             if(huan[i]>=2)///这种情况是不可能的
             {
                 return 0*puts("0");
             }
             else if(huan[i]==1)
             {
                 ans*=2;
             }
             else ans*=son[i];
         }
         ans%=mod;
     }
     printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值