Uva 5002 - The Queue DFS
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的更多相关文章
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVA - 11853 Paintball(dfs)
UVA - 11853 思路:dfs,从最上面超过上边界的圆开始搜索,看能不能搜到最下面超过下边界的圆. 代码: #include<bits/stdc++.h> using namespa ...
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 12253 - Simple Encryption(dfs)
题目链接:uva 12253 - Simple Encryption 题目大意:给定K1.求一个12位的K2,使得KK21=K2%1012 解题思路:按位枚举,不且借用用高速幂取模推断结果. #inc ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- UVA - 1103Ancient Messages(dfs)
UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...
- 【紫书】Tree UVA - 548 静态建树dfs
题意:给你中序后序 求某叶子节点使得从根到该节点权值和最小.若存在多个,输出其权值最小的那个. 题解:先建树,然后暴力dfs/bfs所有路径,取min 技巧:递归传参数,l1,r1,l2,r2, su ...
随机推荐
- linux中断申请之request_threaded_irq 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609 在linux里,中断处理分 ...
- Kettle进行数据迁移(ETL)
由于开发新的系统,需要将之前一个老的C/S应用的数据按照新的数据设计导入到新库中.此过程可能涉及到表结构不一致.大数据量(千万级,甚至上亿)等情况,包括异构数据的抽取.清洗等等工作.部分复杂的工作需要 ...
- MVC Ajax Form & Ajax Valida(笔记)
1.引入必要的文件 <script src=.min.js")" type="text/javascript"></script> &l ...
- 24 The Go image package go图片包:图片包的基本原理
The Go image package go图片包:图片包的基本原理 21 September 2011 Introduction The image and image/color packag ...
- Filebeat入门
一.安装filebeat 简介 Beats 是安装在服务器上的数据中转代理. Beats 可以将数据直接传输到 Elasticsearch 或传输到 Logstash . Beats 有多种类型,可以 ...
- Linux学习笔记:rm删除文件和文件夹
使用rm命令删除一个文件或者目录 使用rmdir可以删除空文件夹 参数: -i:删除前逐一询问确认 -f:即使原档案属性设为唯读,亦直接删除,无需逐一确认 -r:递归 删除文件可以直接使用rm命令,若 ...
- AutoCompleteTextView,Spinner,消息提示
package com.example.wang.testapp2; import android.app.Notification; import android.app.NotificationM ...
- 黑马程序员_java基础笔记(09)...HTML基本知识、CSS、JavaScript、DOM
—————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— 基本标签(a.p.img.li.table.div.span).表单标签.ifra ...
- MongoDB入门教程二[MongoDB Shell 简介与使用]
MongoDB Shell 是MongoDB自带的JavaScript Shell,随MongoDB一同发布,它是MonoDB客户端工具,可以在Shell中使用命令与MongoDB实例交互,对数据库的 ...
- 41-2:和为S的连续正数序列
import java.util.ArrayList; /** * 面试题41-题目2:和为S的连续正数序列 * 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案 ...