HDOJ 5379 Mahjong tree 树形DP

在一棵树上给所有点标号,要求任意一个子树里的点编号连续,每一个点的儿子编号连续。 那么,一个点的非叶子儿子应该是连续的,即一个点的非叶子儿子最多只有两个。 对于每一个点,我们把它的叶子儿子的个数记作S,所有儿子的方案数积为T。当非叶子儿子节点个数小于2的时候,方案数为2T*(S!). 当非叶子儿子节点数等于2的时候,这个点为根的子树合法方案数位T*(S!).
这样dfs一遍即可以处理整棵树的方案数。

Mahjong tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 742    Accepted Submission(s): 227

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

Source

2015 Multi-University Training Contest 7

/* ***********************************************
Author        :CKboss
Created Time  :2015年08月12日 星期三 10时23分35秒
File Name     :HDOJ5379.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

typedef long long int LL;

const int maxn=100100;
const LL mod=1e9+7;

int n;

LL jc[maxn];
int sons[maxn],s[maxn];
LL dp[maxn];
vector<int> G[maxn];

void JC()
{
	jc[0]=1LL;
	for(LL i=1;i<maxn;i++) jc[i]=(jc[i-1]*i)%mod;
}

void init()
{
	for(int i=1;i<=n;i++)
	{
		G[i].clear();
		sons[i]=0; s[i]=0; dp[i]=0;
	}
}

void check_dfs(int u,int fa)
{
	sons[u]=G[u].size();
	if(u!=fa) sons[u]--;
	if(sons[u]==0) return ;

	LL ts=0;
	for(int i=0,sz=G[u].size();i<sz;i++)
	{
		int v=G[u][i];
		if(v==fa) continue;
		check_dfs(v,u);
		if(sons[v]==0) ts++;
	}

	s[u]=sons[u]-ts;
}

void dfs(int u,int fa)
{
	if(sons[u]==0)
	{
		dp[u]=1LL;
		return ;
	}
	LL T=1;
	for(int i=0,sz=G[u].size();i<sz;i++)
	{
		int v=G[u][i];
		if(v==fa||v==0) continue;
		dfs(v,u);
		T=(T*dp[v])%mod;
	}
	if(s[u]==2)
	{
		dp[u]=T*jc[sons[u]-2]%mod;
	}
	else
	{
		dp[u]=2*T*jc[sons[u]-s[u]]%mod;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	JC();
	int cas=1,T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&n);
		init();
		for(int i=0,u,v;i<n-1;i++)
		{
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}

		check_dfs(1,1);

		bool fg=true;
		for(int i=1;i<=n&&fg;i++)
			if(s[i]>2) fg=false;

		if(fg==false)
		{
			printf("Case #%d: 0\n",cas++);
			continue;
		}

		dfs(1,1);

		//for(int i=1;i<=n;i++) printf("%d: dp:%lld sons: %d s: %d\n",i,dp[i],sons[i],s[i]);

		printf("Case #%d: %d\n",cas++,(int)dp[1]);
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-01 02:25:35

HDOJ 5379 Mahjong tree 树形DP的相关文章

hdu 5379 Mahjong tree 树形DP入门

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

hdu 5379 Mahjong tree 树形dp

链接 题意:给定一棵树 把1-n填到树的节点上,使得: 1:儿子节点上填的数字是连续的. 2:子树节点上填的数字是连续的. 把儿子节点分成两种,一种是叶子节点,一种是非叶子节点. 显然非叶子节点个数不能超过2个,不然就不存在这样的方案了. 然后分类讨论一下非叶子节点个数即可. #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #i

hdu 5379 Mahjong tree(树形dp)

题目链接:hdu 5379 Mahjong tree 树形dp,每个节点最多有2个子节点为一棵节点数大于1的子树的根节点,而且要么后代的节点值都大于,要么都小于本身(所以tson不为0是,要乘2).对于K个单一节点的子节点,种类数即为全排K!.当一个节点没有兄弟节点时,以这个节点为根结点的子树,根可以选择最大或者最小. #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

codeforces161D - Distance in Tree 树形dp

题意:给你一棵树,问你树中距离为k的有多少种情况. 解题思路:树形dp  维护每个节点(1-K)深度的情况, 解题代码: 1 // File Name: 161d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 19时20分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #incl

hdu5593/ZYB&#39;s Tree 树形dp

ZYB's Tree Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一颗NN个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采用如下方式进行读入输出: 读入:读入两个数A,BA,B,令fa_ifa?i??为节点ii的父亲,fa_1=0fa?1??=0;fa_i=(A*i+B)\%(i-1)+1fa

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

Hdu 5379 Mahjong tree (dfs + 组合数)

题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号? 解题思路: 对于一个节点来讲,非叶子儿子节点最多有两个才能满足要求,否则满足子树节点连续的话就无法满足兄弟节点连续.然后有dfs计算每棵子树的贡献值,每棵子树的子节点可以分为叶子节点X和非叶子节点Y,叶子节点可以分配到一组连续的编号,非叶子节点只能分配到兄弟节点中最大或者最小编号两种情况,叶子节

HDU 5379 Mahjong tree(dfs)

题目链接:http://acm.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 verte