Uva 5002 - The Queue DFS

时间:2023-03-09 15:32:27
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[][]; void BuildYangHui(ll n)
{
ll i,j;
yh[][]=;yh[][]=;
for (i=;i<=n;i++)
{
yh[i][]=;
for (j=;j<=n;j++)
{
yh[i][j]=(yh[i-][j-]+yh[i-][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()==)
{
kill[n].num=;
return;
}
else
{
for(int i=;i<kill[n].god.size();i++)
{
if(kill[kill[n].god[i]].num==)
dfs(kill[n].god[i]);
kill[n].num+=kill[kill[n].god[i]].num+;
}
}
} void dfs1(ll n)
{
if(kill[n].god.size()==)
{
kill[n].ans=;
return;
} kill[n].ans=; if(kill[n].god.size()==)
{
if(kill[kill[n].god[]].ans==)
{
dfs1(kill[n].god[]);
}
kill[n].ans=kill[kill[n].god[]].ans;
return;
}
ll num=;
for(int i=;i<kill[n].god.size();i++)
{
if(kill[kill[n].god[i]].ans==)
{
dfs1(kill[n].god[i]);
}
if(i==)
{
num=kill[kill[n].god[i]].num+;
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+][kill[kill[n].god[i]].num+]%MOD*kill[kill[n].god[i]].ans%MOD;
kill[n].ans%=MOD;
num+=kill[kill[n].god[i]].num+;
}
} int main()
{
int t;
cin>>t;
BuildYangHui();
for(int cas=;cas<=t;cas++)
{
memset(kill,,sizeof(kill)); int n;
cin>>n;
ll a,b;
for(int i=;i<n-;i++)
{
cin>>a>>b;
kill[b].pre=a;
kill[a].god.push_back(b);
}
int sb; for(int i=;i<=n;i++)
{
if(kill[i].pre==)
sb=i;
} dfs(sb);
dfs1(sb); cout<<"Case "<<cas<<":"<<" "<<kill[sb].ans%MOD<<endl;
}
return ;
}