1382-the Queue

Source: Internet
Author: User

1382-the Queue
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

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 cannot stand at any 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 (CEO) who doesn ' t had any SUP Ervisor.

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 an integer T (≤700), denoting the number of test cases.

Each case is starts with a line containing an integer N (1≤n≤1000). Each of the following N-1 lines would contain, integers a and B (1≤a, B≤n, a≠b), whic h denotes A is the supervisor of b. For the sake of simplicity we is representing each employee by an integer number. Assume that the given input follows the restrictions stated above.

Output

For each case, print the case number and the number of ways to create the queue. The result can be large, print the result modulo 007.

Sample Input Output for Sample Input

1

5

2 1

2 3

3 4

3 5

Case 1:8

Problem setter:md. Arifuzzaman arifspecial thanks:jane Alam Jan Test instructions: n individuals, each of whom has a boss, except the highest boss. Ask to arrange these people, and to be satisfied if the boss has to be in front of the person in his jurisdiction, ask how many permutations there are. At first, it was sort of like a topology, then arranged, and then it didn't feel right. Idea: Tree DP Each person has only one boss, and only one of the biggest bosses, so these points can form a tree. Then consider that if there is a root node, the following two nodes, then we can combine these three into a single point, the number of scenarios is dp[v1]*dp[v2]* (C (Size (v1) +size (v2,size (v1))); This equation can be understood as: Dp[v1] is the number of V1 of the node, the same as the dp[v2]; The current total number is size[v1]+size[v2]+1;1 for the root node of the above two points, so that the merged nodes are taken SIZE[V1] Put all the points below the V1 then the rest will be put v2 so the legal number is dp[v1]*dp[v2]* (C (Size (v1) +size (v2,size (v1))); This is the final result of merging to the boss; using DFS to achieve DP complexity O (N+E);
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <queue>6#include <stack>7#include <vector>8 using namespacestd;9typedefLong LongLL;Ten Const intn=1e9+7; Onevector<int>vec[1100]; Aqueue<int>que; - intans[2100]; -LL dp[1100]; theLL cnt[1100]; -LL ju[1100][1100]; - voidDfsintn); - intMainvoid) + { -         intk,i,j; +ju[0][0]=1; Aju[1][0]=1; atju[1][1]=1; -          for(i=2; i<=1099; i++) -         { -                  for(j=0; j<=i; J + +) -                 { -                         if(i==j| | j==0) inju[i][j]=1; -                         ElseJu[i][j]= (ju[i-1][j]+ju[i-1][j-1])%N; to                 } +         } -scanf"%d",&k); the         ints; *         intn,m; $         intx, y;Panax Notoginseng          for(s=1; s<=k; s++) -         { the                  for(i=0; i<1050; i++) +                 { Adp[i]=1; the vec[i].clear (); +                 } -scanf"%d",&n); $                  for(i=1; i<=n; i++) $cnt[i]=i; -                  for(i=1; i<n; i++) -                 { thescanf"%d%d",&x,&y); - vec[x].push_back (y);Wuyicnt[y]=x; the                 } -                 intId=1; Wumemset (ans,0,sizeof(ans)); -                  for(i=1; i<=n; i++) About                 { $                         if(cnt[i]==i) -Id=i; -                 } -memset (ans,0,sizeof(ans)); A dfs (ID); +printf"Case %d:", s); theprintf"%lld\n", Dp[id]); -         } $         return 0; the } the voidDfsintN) the { the         if(!vec[n].size ()) -         { inans[n]=1; thedp[n]=1; the                 return ; About         } the         intCc=vec[n].size (); the         inti,j; theLL ak=0; +ans[n]+=1; -          for(i=0; I<vec[n].size (); i++) the         {Bayi DFS (Vec[n][i]); theans[n]+=Ans[vec[n][i]]; thedp[n]= (dp[vec[n][i]]*dp[n]%n) * (ju[ans[n]-1][ans[vec[n][i]])%N; -         } -}

1382-the Queue

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.