1382 - The Queue
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 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 (CEO) 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 (≤ 700), denoting the number of test cases.
Each case starts with a line containing an integer N (1 ≤ N ≤ 1000). Each of the following N - 1 lines will contain two integers a and b (1 ≤ a, b ≤ N, 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. 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 1000 000 007.
Sample Input |
Output for Sample Input |
1 5 2 1 2 3 3 4 3 5 |
Case 1: 8 |
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 namespace std;
9 typedef long long LL;
10 const int N=1e9+7;
11 vector<int>vec[1100];
12 queue<int>que;
13 int ans[2100];
14 LL dp[1100];
15 LL cnt[1100];
16 LL ju[1100][1100];
17 void dfs(int n);
18 int main(void)
19 {
20 int k,i,j;
21 ju[0][0]=1;
22 ju[1][0]=1;
23 ju[1][1]=1;
24 for(i=2; i<=1099; i++)
25 {
26 for(j=0; j<=i; j++)
27 {
28 if(i==j||j==0)
29 ju[i][j]=1;
30 else ju[i][j]=(ju[i-1][j]+ju[i-1][j-1])%N;
31 }
32 }
33 scanf("%d",&k);
34 int s;
35 int n,m;
36 int x,y;
37 for(s=1; s<=k; s++)
38 {
39 for(i=0; i<1050; i++)
40 {
41 dp[i]=1;
42 vec[i].clear();
43 }
44 scanf("%d",&n);
45 for(i=1; i<=n; i++)
46 cnt[i]=i;
47 for(i=1; i<n; i++)
48 {
49 scanf("%d %d",&x,&y);
50 vec[x].push_back(y);
51 cnt[y]=x;
52 }
53 int id=1;
54 memset(ans,0,sizeof(ans));
55 for(i=1; i<=n; i++)
56 {
57 if(cnt[i]==i)
58 id=i;
59 }
60 memset(ans,0,sizeof(ans));
61 dfs(id);
62 printf("Case %d: ",s);
63 printf("%lld\n",dp[id]);
64 }
65 return 0;
66 }
67 void dfs(int n)
68 {
69 if(!vec[n].size())
70 {
71 ans[n]=1;
72 dp[n]=1;
73 return ;
74 }
75 int cc=vec[n].size();
76 int i,j;
77 LL ak=0;
78 ans[n]+=1;
79 for(i=0; i<vec[n].size(); i++)
80 {
81 dfs(vec[n][i]);
82 ans[n]+=ans[vec[n][i]];
83 dp[n]=(dp[vec[n][i]]*dp[n]%N)*(ju[ans[n]-1][ans[vec[n][i]]])%N ;
84 }
85 }
1382 - The Queue的更多相关文章
- lightoj 1382 - The Queue(树形dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1382 题解:简单的树形dp加上组合数学. #include <iostr ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
随机推荐
- 详解 Rainbond Ingress 泛解析域名机制
Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.区别于一般的 Ingress 配置中,用户需要自行定义域名的使用体验,Rainbond 的 ...
- absorb
absorb 物理的absorb比较直观.被书本/知识absorb也好理解.涉及到money/time时有点抽象,但汉语也有"吸金"的说法,consume, use up.可以吸收 ...
- day10 ajax的基本使用
day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...
- 通信协议 HTTP TCP UDP
TCP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则"说话",对方才能理解或为之服务. TCP HTTP UDP三者的关系: T ...
- HTTP协议及常见状态码
超文本传输协议(HTTP)是用于传输诸如HTML的超媒体文档的应用层协议.它被设计用于Web浏览器和Web服务器之间的通信,但它也可以用于其他目的. HTTP遵循经典的客户端-服务端模型,客户端打开一 ...
- JpaRepository 增删改查
Jpa查询 JpaRepository简单查询 基本查询也分为两种,一种是spring data默认已经实现,一种是根据查询的方法来自动解析成SQL. 预先生成方法 spring data jpa 默 ...
- 【前端】关于DOM节点
参考这个: https://juejin.cn/post/6844903849614901261 DOM树的根节点是document对象 DOM节点类型:HTML元素节点(element nodes) ...
- 如何用three.js实现数字孪生、3D工厂、3D工业园区、智慧制造、智慧工业、智慧工厂-第十课
文章前,先聊点啥吧. 最近元宇宙炒的挺火热,在所有人都争相定义元宇宙的时候,资本就开始着手入场了.当定义明确,全民皆懂之后,风口也就过去了. 前两天看到新闻,新世界CEO宣布购入最大的数字地块,这块虚 ...
- Python multiprocessing 基础使用和小trick
最近进行数据预处理时(噪声插入),单进程严重影响实验周期,故学习了multiprocessing并发执行不同数据集的处理,加快执行效率.现于此进行一些简单记录以供日后参考. 1. 基础: From m ...
- 微软开源的Web测试和自动化神器 Playwright
Playwright 是微软开源的一个用于 Web 测试和自动化的框架, 提供了可靠的端到端测试, 功能非常强大, 可以在测试, 爬虫,自动化场景中使用. 跨浏览器 Playwright 支持所有现代 ...