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. [转]关于MyEclipse下的项目无法使用BASE64Encoder问题的解决办法

    [链接] http://blog.csdn.net/longlonglongchaoshen/article/details/75087616

  2. 小程序开发总结一:mpvue框架及与小程序原生的混搭开发

    mpvue-native:小程序原生和mpvue代码共存 问题描述 mpvue和wepy等框架是在小程序出来一段时间之后才开始有的,所以会出现的问题有:需要兼容已有的老项目,有些场景对小程序的兼容要求 ...

  3. JAVA随笔(三)

    私有是针对类的,而不是对象. static 函数,其实是类函数.之前一直不太理解每个类中的static main是什么意思,为什么main中不能直接调用非静态的变量:因为main是 类函数,不是属于某 ...

  4. MyBatis框架的基本使用

    MyBatis框架简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...

  5. (转)链接服务器——获取EXCEL数据

    测试目的:验证利用链接服务器.分布式查询获取EXCEL中的数据测试环境:Microsoft SQL Server 2005 - 9.00.3080.00 (X64)  Enterprise Editi ...

  6. Python获取指定文件夹下的文件名

    本文采用os.walk()和os.listdir()两种方法,获取指定文件夹下的文件名. 一.os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, t ...

  7. dp入门题目

    本文文旨,如题... 转载请注明出处... HDOJ 1176 免费馅饼 http://acm.hdu.edu.cn/showproblem.php?pid=1176 类似数塔,从底往上推,每次都是从 ...

  8. servlet 学习笔记(二)

    ---------------------第二讲--------------------------------- 开发servlet有三种方法: 1.实现servlet接口(最原始的) 实现接口的5 ...

  9. CF474D. Flowers

    D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...

  10. 用 Java 实现一个快速排序算法

           快速排序是排序算法中效率最高的一种,它是利用递归的原理,把数组无限制的分成两个部分,直到所有数据都排好序为止. 快速排序是对冒泡排序的一种改进.它的基本思想是通过一趟排序将要排序的数据分 ...