zoukankan      html  css  js  c++  java
  • Uva 5002

    On some special occasions Nadia’s company provide very special lunch for all employees of the company. Before the food is served all of the employees must stand in a queue in front of the food counter. The company applied a rule for standing in the queue. The rule is nobody can stand anywhere in front of his supervisor in the queue. For example if Abul is the supervisor of Babul and Abul stands in kth position from the front of the queue, then Babul cannot stand at any position in between 1 and k – 1 from front of the queue.

    The company has N employees and each of them has exactly one supervisor except one who doesn’t have any supervisor.

    You have to calculate in how many ways the queue can be created. For this problem, you can safely assume that in at least one way the queue can be created.

    Input

    Input starts with an integer T (T is around 700), the number of test cases.

    Each test case starts with a line containing one integer N (1 ≤ N ≤ 1000). Each of the following N - 1 lines will contain two integers a and b (1 a, b N and a b), which denotes that a is the supervisor of b. For the sake of simplicity we are representing each employee by an integer number.

     

    Output

    For each input case, output a single line in the format “Case #: w”, here # is the case number and w is the number of ways to create the queue. The number of ways can be very large. You have to print the number modulo 1,000,000,007.

    Sample Input                               Output for Sample Input

    1

    5

    2 1

    2 3

    3 4

    3 5

    Case 1: 8

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 1010
    #define MOD 1000000007
    const int inf=0x7fffffff;   //无限大
    ll yh[2010][2010];
    
    void BuildYangHui(ll n)
    {
        ll i,j;
        yh[0][0]=1;yh[0][1]=0;
        for (i=1;i<=n;i++)
        {
            yh[i][0]=1;
            for (j=1;j<=n;j++)
            {
                yh[i][j]=(yh[i-1][j-1]+yh[i-1][j])%MOD;
            }
        }
    }
    
    
    struct node
    {
        int pre;
        ll num;
        vector<ll> god;
        int ans;
    };
    node kill[maxn];
    
    void dfs(int n)
    {
        if(kill[n].god.size()==0)
        {
            kill[n].num=0;
            return;
        }
        else
        {
            for(int i=0;i<kill[n].god.size();i++)
            {
                if(kill[kill[n].god[i]].num==0)
                    dfs(kill[n].god[i]);
                kill[n].num+=kill[kill[n].god[i]].num+1;
            }
        }
    }
    
    void dfs1(ll n)
    {
        if(kill[n].god.size()==0)
        {
            kill[n].ans=1;
            return;
        }
    
        kill[n].ans=1;
    
        if(kill[n].god.size()==1)
        {
            if(kill[kill[n].god[0]].ans==0)
            {
                dfs1(kill[n].god[0]);
            }
            kill[n].ans=kill[kill[n].god[0]].ans;
            return;
        }
        ll num=0;
        for(int i=0;i<kill[n].god.size();i++)
        {
            if(kill[kill[n].god[i]].ans==0)
            {
                dfs1(kill[n].god[i]);
            }
            if(i==0)
            {
                num=kill[kill[n].god[i]].num+1;
                kill[n].ans=kill[n].ans*kill[kill[n].god[i]].ans%MOD;
                kill[n].ans%MOD;
                continue;
            }
            kill[n].ans=kill[n].ans*yh[num+kill[kill[n].god[i]].num+1][kill[kill[n].god[i]].num+1]%MOD*kill[kill[n].god[i]].ans%MOD;
            kill[n].ans%=MOD;
            num+=kill[kill[n].god[i]].num+1;
        }
    }
    
    
    
    int main()
    {
        int t;
        cin>>t;
        BuildYangHui(2001);
        for(int cas=1;cas<=t;cas++)
        {
            memset(kill,0,sizeof(kill));
    
            int n;
            cin>>n;
            ll a,b;
            for(int i=0;i<n-1;i++)
            {
                cin>>a>>b;
                kill[b].pre=a;
                kill[a].god.push_back(b);
            }
            int sb;
    
            for(int i=1;i<=n;i++)
            {
                if(kill[i].pre==0)
                    sb=i;
            }
    
            dfs(sb);
            dfs1(sb);
    
    
            cout<<"Case "<<cas<<":"<<" "<<kill[sb].ans%MOD<<endl;
        }
        return 0;
    }
  • 相关阅读:
    某个周六加班日的划水记
    如何保证消息的可靠性传输
    PHP面向对象学习六 多态
    PHP面向对象学习五 类中接口的应用
    PHP面向对象学习四 类的关键字
    PHP面向对象学习三 类的抽象方法和类
    PHP面向对象学习二
    PHP面向对象学习一
    高级ql
    mysql 方法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4233108.html
Copyright © 2011-2022 走看看