Uva 5002 - The Queue DFS

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;
}
时间: 2024-10-13 21:23:02

Uva 5002 - The Queue DFS的相关文章

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

UVA 10318 - Security Panel dfs 剪枝

UVA 10318 - Security Panel dfs 剪枝 ACM 题目地址:UVA 10318 - Security Panel 题意: 这题跟点灯的题目很像,点灯游戏选择一盏灯时会让它以及四周的灯改变状态. 但是我们有特殊的开开关技巧,它给出了改变状态的位置,而不是四周都改变. 问你从全部关着变成全部开着的最小开关步骤. 分析: 很明显,在一个位置上点两次或更多次是没有必要的,所以一个位置只有选择与不选择,用dfs即可,但如果暴力所有可能,复杂度是2^25,会超时,所以要剪枝. 由于

uva The Dole Queue

题目如下: The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The NewNational Green Labour Rhinoceros Party has decided on the followingstrategy. Every day all dole applicants will be placed in a largecircle, facing inwards. Someone

uva 12253 - Simple Encryption(dfs)

题目链接:uva 12253 - Simple Encryption 题目大意:给定K1,求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用快速幂取模判断结果. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll ite=(1<<20)-1; ll N; /* l

Uva LA6450 Social Advertising DFS

You have decided to start up a new social networking company. Other existing popular social networksalready have billions of users, so the only way to compete with them is to include novel features noother networks have.Your company has decided to ma

UVa 208 消防车(dfs+剪枝)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=144 题意:给出一个n个结点的无向图以及某个结点k,按照字典序从小到大顺序输出从1到结点k的所有路径. 思路:如果直接矩阵深搜的话是会超时的,所以我们可以从终点出发,将与终点相连的连通块保存起来,这样dfs深搜时可以剪枝掉一些到达不了的点.只要解决了这个,dfs就是小问题. 这道题还有点坑的

UVa: 12100 - Printer Queue

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252 题目描述:有一些文件需要打印机打印,每个人物有不同的优先级(1-9),打印机的运作方式为:首先从打印队列里取出一个任务J,如果队列里有比J更急的任务,则直接把任务放到打印队列的尾部,否则打印任务J.输入打印队列中各个任务的优先级以及所关注的任务在队列中的位置(对首位置为0).

UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去:若长度相等,则将所有还能到达的数字按从大到小排序后连到当前数字上,如果还比目前最优解小,则减去.找出所有还能到达的点的过程用BFS实现. 1 #pragma comment(linker, "/STACK:1024000000,10

UVA 572- Oil Deposits(简单dfs)

Oil Deposits Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular