• UVA 11174 Stand in a Line


    All the people in the byteland want to stand in a line in such a way that no person stands closer to the

    front of the line than his father. You are given the information about the people of the byteland. You
    have to determine the number of ways the bytelandian people can stand in a line.
    Input
    First line of the input contains T (T < 14) the number of test case. Then following lines contains T
    Test cases.
    Each test case starts with 2 integers n (1 ≤ n ≤ 40000) and m (0 ≤ m < n). n is the number
    of people in the byteland and m is the number of people whose father is alive. These n people are
    numbered 1 . . . n. Next m line contains two integers a and b denoting that b is the father of a. Each
    person can have at most one father. And no person will be an ancestor of himself.
    Output
    For each test case the output contains a single line denoting the number of different ways the soldier
    can stand in a single line. The result may be too big. So always output the remainder on dividing ther
    the result by 1000000007.
    Sample Input
    3
    3 2
    2 1
    3 1
    3 0
    3 1
    2 1
    Sample Output
    2
    6
    3

    题意:n(1<=n<=40000)个农民排成一列,给出m对父子关系,每个人都不能站在自己父亲前面,问有多少种排列方式。

    分析:村民由父子关系组成了一棵树(根节点是虚拟的根),设以i为节点的子树有f[i]种排法,则发f(i)=f(c1)f(c2)...(s(i)-1)!/(s(c1)!s(c2)!...s(ck)!)

    其中cj是节点i的第j个儿子,节点i一共有k个儿子,s[i]表示以i为根的子树的节点种数。可化简为f(root)=(s(root)-1)!/(s(1)s(2)s(3)...s(n))。

    s(root)=n+1,(s(root)-1)!=n!。然后阶乘达表,求出分子的逆元,便可得结果。

    刚开始时没注意树的维护的复杂度,老是T,后来看了别人题解,发现别人是用的vector,树只维护一遍,然后用vector就a了。

    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    #define mod 1000000007
    #define ll long long
    int s[40005];
    int n,m;
    long long a[40005];
    vector<vector<int> > v;
    
    long long poww(long long x,long long n)
    {
        long long ret=1;
        while(n)
        {
            if(n&1)
                ret=ret*x%mod;
            n>>=1;
            x=x*x%mod;
        }
        return ret;
    }
    
    int DFS(int x)
    {
        if(v[x].size()==0)  return s[x]=1;
        if(s[x])    return s[x];
        int c=1;
        for(int i=0;i<v[x].size();i++)
        {
            s[v[x][i]]=DFS(v[x][i]);
            c+=s[v[x][i]];
        }
        return c;
    }
    
    int main()
    {
        a[0]=1;
        for(long long i=1; i<=40000; i++)
            a[i]=a[i-1]*i%mod;
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            if(m==0)
            {
                printf("%lld
    ",a[n]);
                continue;
            }
            memset(s,0,sizeof s);
            v.clear();
            v.resize(n+1);
            for(int i=0; i<m; i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                v[b].push_back(a);
            }
            for(int i=1;i<=n;i++)
                s[i]=DFS(i);
            long long ans=1;
            for(int i=1; i<=n; i++)
                ans=ans*s[i]%mod;
            long long ant=poww(ans,mod-2);
            printf("%lld
    ",a[n]*ant%mod);
        }
        return 0;
    }
  • 相关阅读:
    Ubuntu mongodb 安装和配置
    最基本的SQL语法/语句
    Sphinx学习之sphinx的安装篇
    六关节机器人的雅可比矩阵及微分运算
    六关节机器人的逆运动学计算
    六关节机器人的正运动学计算
    六关节机器人末端的微分运动
    Python3 升级pip
    一般多项式曲线的最小二乘回归(Linear Regression)
    关于卡尔曼滤波(Kalman Filter)的很好讲解
  • 原文地址:https://www.cnblogs.com/siyecaodesushuo/p/5695088.html
Copyright © 2020-2023  润新知