题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=5379

Problem Description
Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, indexed from 1 to n.)
Thought for a long time, finally he decides to use the mahjong to decorate the tree.
His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has only n mahjong tiles, and the mahjong tiles indexed from 1 to n.)
He put the mahjong tiles on the vertexs of the tree.
As is known to all, little sun is an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules are as follows:

(1)Place exact one mahjong tile on each vertex.
(2)The mahjong tiles' index must be continues which are placed on the son vertexs of a vertex.
(3)The mahjong tiles' index must be continues which are placed on the vertexs of any subtrees.

Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.

 
Input
The first line of the input is a single integer T, indicates the number of test cases. 
For each test case, the first line contains an integers n. (1 <= n <= 100000)
And the next n - 1 lines, each line contains two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.
 
Output
For each test case, output one line. The output format is "Case #x: ans"(without quotes), x is the case number, starting from 1.
 
Sample Input
2
9
2 1
3 1
4 3
5 3
6 2
7 4
8 7
9 3
8
2 1
3 1
4 3
5 1
6 4
7 5
8 4
 
Sample Output
Case #1: 32
Case #2: 16
 
Author
UESTC
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5867 5866 5865 5864 5863 
 
题意:有一棵n个节点的树,有n-1条边,现在将1~n的数放到节点中,满足一下两个条件:
        1、每个节点的儿子节点上的数必须是连续的;
        2、每棵子树上的节点的数必须是连续的;
    求有多少种放这些数的方法?
 
思路:可以发现,对于某个节点,要求它的儿子之间连续,那么它的儿子节点中最多有两个儿子节点有孩子,其它叶子儿子结点之间可以随意排列,也就是排列;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int N=;
const long long MOD=1e9+;
vector<int>g[N];
long long A[N],res; int dfs(int u,int f)
{
int tot=,nt=;///包含根节点的所有节点,直接孩子节点,子树个数
int n=g[u].size();
int ns=n-;
if(u==) ns++;
for(int i=;i<n;i++)
{
int v=g[u][i];
if(v==f) continue;///防止相邻的两个节点上下反复递归;
int num=dfs(v,u);
if(num>) nt++;
}
if(nt>)///有大于两个子树不能使切割后连续
return res =;
if(nt)///有一个或两个子树切割方法数都只有两个
res=res*%MOD;
res=res*A[ns-nt]%MOD;///当有多个(非子树的)节点可自由排序;
return ns+;
} int main()
{
A[]=;
for (int i=;i<N;i++)
A[i]=A[i-]*i%MOD; int t,cas=,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
g[i].clear();
for(int i=;i<n-;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
if(n==)
{
printf("Case #%d: 1\n",cas++);
continue;
}
res=;
dfs(,);
printf("Case #%d: %lld\n",cas++,res*%MOD);//根节点左右两种切法
}
return ;
}
 

2015暑假多校联合---Mahjong tree(树上DP 、深搜)的更多相关文章

  1. 2015暑假多校联合---Expression(区间DP)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...

  2. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  3. 2015暑假多校联合---Friends(dfs枚举)

    原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...

  4. 2015暑假多校联合---CRB and His Birthday(01背包)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...

  5. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  6. 2015暑假多校联合---Assignment(优先队列)

    原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...

  7. 2015暑假多校联合---Problem Killer(暴力)

    原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...

  8. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  9. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

随机推荐

  1. Fiddler实战深入研究(二)

    Fiddler实战深入研究(二) 阅读目录 Fiddler不能捕获chrome的session的设置 理解数据包统计 请求重定向(AutoResponder) Composer选项卡 Filters选 ...

  2. Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php )

    Atitit 通过调用gui接口杀掉360杀毒 360卫士  qq保镖等难以结束的进程(javac# php ) 1.1. 这些流氓软件使用操作系统os提供的普通api根本就杀不掉啊1 1.2. 使用 ...

  3. Atitit 表达式原理 语法分析 原理与实践 解析java的dsl  递归下降是现阶段主流的语法分析方法

    Atitit 表达式原理 语法分析 原理与实践 解析java的dsl  递归下降是现阶段主流的语法分析方法 于是我们可以把上面的语法改写成如下形式:1 合并前缀1 语法分析有自上而下和自下而上两种分析 ...

  4. iOS-应用打包发布常见问题

    这个月公司安排我一个人做iOS客户端开发,由于急着用,我先发布一个版本,由于第一次发布iOS应用,期间出了不少问题,记录于此. 1.使用Application Loader 发布时报错:Communi ...

  5. mysql基础知识扫盲

    本篇主要介绍关于mysql的一些非常基础的知识,为后面的sql优化做准备. 一:连接mysql 关于mysql的下载和安装我在这里就不说了,第一步我们要连接我们的mysql服务器,打开cmd命令切换到 ...

  6. AngularJS快速入门01-基础

    记得第一次听说AngularJS这项很赞的Web的前端技术,那时还是2014年,年中时我们我的一个大牛兄弟当时去面试时,被问到了是否熟悉该技术,当时他了解和使用的技术比较多.我们询问他面试情况时,他给 ...

  7. CSS实现垂直居中的4种思路

    × 目录 [1]line-height [2]vertical-align [3]absolute [4]flex 前面的话 相对于水平居中,人们对于垂直居中略显为难,大部分原因是vertical-a ...

  8. Android开发之网络

    很多初学Android开发的人员对网络这一点感觉很高大上,现在我就和大家分享一下我的学习心得. 就目前的Android手机来说,可能存在5种网络状态: ----无网络(这种状态可能是因为手机停机,网络 ...

  9. Nginx配置网站适配PC和手机

    考虑到网站的在多种设备下的兼容性,有很多网站会有手机版和电脑版两个版本.访问同一个网站URL,当服务端识别出用户使用电脑访问,就打开电脑版的页面,用户如果使用手机访问,则会得到手机版的页面. 1.判断 ...

  10. NUMA架构的CPU -- 你真的用好了么?

    本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...