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 ;
}

Uva 5002 - The Queue DFS的更多相关文章

  1. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  2. UVA - 11853 Paintball(dfs)

    UVA - 11853 思路:dfs,从最上面超过上边界的圆开始搜索,看能不能搜到最下面超过下边界的圆. 代码: #include<bits/stdc++.h> using namespa ...

  3. UVA.540 Team Queue (队列)

    UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...

  4. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. uva 12253 - Simple Encryption(dfs)

    题目链接:uva 12253 - Simple Encryption 题目大意:给定K1.求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用高速幂取模推断结果. #inc ...

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

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  7. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  8. UVA - 1103Ancient Messages(dfs)

    UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...

  9. 【紫书】Tree UVA - 548 静态建树dfs

    题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...

随机推荐

  1. 002_安装第三方APP好的站点及解除安全与隐私限制

    一.解除安全与隐私限制的任何来源. http://bbs.feng.com/read-htm-tid-10714286.html 接下来,我们就打开终端,然后输入以下命令:   sudo spctl ...

  2. Intel大坑之一:丢失的SSE2 128bit/64bit 位移指令,马航MH370??

    缘由 最近在写一些字符串函数的优化,兴趣使然,可是写的过程中,想要实现 128bit 的按 bit 逻辑位移,遇到了一个大坑,且听我娓娓道来. 如果要追究标题,更确切的是丢失的SSE2 128 bit ...

  3. JS判断是否是PC端访问网站

    function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...

  4. unity 优秀开源项目

    ihaiu.GUIDRef (查看项目资源使用情况) http://blog.ihaiu.com/unity-GUIDRef Ihaiu.PoolManager (对象池) http://github ...

  5. 玩玩 Nginx 1----- Nginx + ngx_lua安装测试【CentOs下】

          最近打算搞搞nginx,扒着各位先驱的文章自己进行测试下,中间过程也是错误不断,记录一下,以备使用.       nginx的安装挺简单的,主要还是研究下一些第三方的模块,首先想试下初始化 ...

  6. gc overhead limit exceeded内存问题

    gc overhead limit exceeded 当出现这种问题的时候一般有两种思路 一.修改idea或者eclipse中的配置文件,将内存调到1024即可 二.在代码中通过system.gc 手 ...

  7. Hash 分布均衡算法

    1.移位实现 public static int GetIndex(string str, int count) { , (current, c) => (current << ) ...

  8. docker修改docker0 mtu

    由于docker宿主机设置了mtu造成docker镜像中mtu和宿主机mtu不匹配,大包后网络不同.所以需要设置docker0的mtu. 1.修改docker.service vi /usr/lib/ ...

  9. java 二叉树遍历

    package com.lever; import java.util.LinkedList;import java.util.Queue; /** * 二叉树遍历 * @author lckxxy ...

  10. 20165333 2017-2018-2《Java程序设计》课程总结

    一.每周作业链接汇总 1.预备作业一:我期望的师生关系 简要内容: 印象深刻的老师 我期望的师生关系 关于JAVA学习 2.预备作业二:学习基础和C语言学习基础 简要内容: 技能学习 C语言学习 关于 ...