Uva 5002-the Queue DFS

Source: Internet
Author: User

On some special occasions Nadia ' s company provide very special lunch for all employees of the company. Before the food was served all of the employees must stand in a queue with 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 its supervisor in the queue. For example if Abul are the supervisor of Babul and Abul stands in kth position from the front of the queue, then Babul can Not stand at no position in between 1 and k–1 from front of the queue.

The company had N employees and each of them had exactly one supervisor except one who doesn ' t had any superviso R.

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

Input

Input starts with a integer t (t is around ), the number of test cases.

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

Output

For each of the input case, the output a of the format "Case #: W", Here's the case number and W is The number of ways to create the queue. The number of ways can 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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 1010#defineMOD 1000000007Const intinf=0x7fffffff;//infinitely Largell yh[ .][ .];voidBuildyanghui (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; }    }}structnode{intPre;    ll num; Vector<ll>God; intans;}; Node KILL[MAXN];voidDfsintN) {    if(kill[n].god.size () = =0) {Kill[n].num=0; return; }    Else    {         for(intI=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; }    }}voidDFS1 (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(intI=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; }}intMain () {intT; CIN>>T; Buildyanghui (2001);  for(intcas=1; cas<=t;cas++) {memset (Kill,0,sizeof(Kill)); intN; CIN>>N;        ll A, B;  for(intI=0; i<n-1; i++) {cin>>a>>b; Kill[b].pre=A;        Kill[a].god.push_back (b); }        intSB;  for(intI=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;}

Uva 5002-the Queue DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.