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 ...
随机推荐
- 解决修改表结构,添加外键时出现“约束冲突”的错误
由于表结构更改,使用新建表,现有部分表需要更改外键,将引用更改到新建表的相应字段.在更改过程中,部分表出现如下错误提示: ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束 ' ...
- ETL利器Kettle实战应用解析系列三
本系列文章主要索引如下: 一.ETL利器Kettle实战应用解析系列一[Kettle使用介绍] 二.ETL利器Kettle实战应用解析系列二 [应用场景和实战DEMO下载] 三.ETL利器Kettle ...
- 『实践』百度地图给map添加右键菜单(判断是否为marker)
var map; var s;//经度 var w;//纬度 $(document).ready(function(){ $(".mune").load("jsp/c ...
- js实现图片懒加载
大型购物网站都会采用图片懒加载技术来优化网站首页打开速度,以提高用户体验,那么具体是怎么实现的呢,我们一探究竟. html结构(div包裹一层用来显示背景图片,等待图片加载完成后,显示真实图片) &l ...
- No.19 selenium学习之路之os模块
os模块没有什么好说的,直接看实例就可以了 读取文件内容: open只能读文件的内容,不能读文件夹的内容 常用方法: 1. os.name——判断现在正在实用的平台,Windows 返回 ‘nt'; ...
- CxGrid 表格列内容居中
首先每一列 Cxgrid 都不知道要当成什么来出来,所以每一列都有个properties 让你来设置,告诉cxgrid 这列的内容是什么,然后根据你给出的内容 再来决定用什么居中方式: 就是说 官方再 ...
- Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- IPC对象的持续性
转载:http://book.51cto.com/art/201006/207275.htm <UNIX网络编程:第2版.第2卷,进程间通信>本书全面深入地讲解了各种进程间通信形式,包括消 ...
- CSDN博客专家申请成功
又一个值得纪念的日子,上周六申请CSDN博客专家,今天中午审批通过.使用CSDN好几年了,从未想到能把博客一步步的写到这个地步. 曾经,写过一段博客,只是为了记录和分享.中间由于工作的变动和繁忙中断了 ...
- Owin 自定义中间件
/// <summary> /// 自定义的中间件 /// </summary> public class CustomMiddleware : OwinMiddleware ...