HDU 5379 Mahjong tree (树形DP+组合数学)*

本文介绍了一种基于树形结构的计数问题,旨在利用组合数学原理寻找所有可能的装饰方案。问题设定为在一棵树上放置连续数值的麻将牌,使得每个节点恰好有一张牌,并且相邻节点间的牌值连续。文章提供了详细的解析过程及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mahjong tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1935    Accepted Submission(s): 651


 

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

2015 Multi-University Training Contest 7

 

 

Recommend

wange2014   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long

const int  maxn =1e5+5;
const int mod=1e9+7;
/*
题目大意:给定一棵树,然后要求给每个节点都安排一个值,唯一的。
要求满足:子树值连续,儿子节点形成的值域连续,计数这样的方案数。

这道题很好,大部分都结合了组合数学,
首先考虑一颗子树,如果其占用了连续的区间L,
儿子节点中的叶子节点数量为s1,非叶子节点数量为s2,
dp[u]=s1!*dp[s2]*dp[u];很明显这样排列,
并且不难发现,非叶子节点的数量不超过2,
如果有非叶子节点的存在,最后子树的总计数要乘2,
因为考虑对称,如果全是叶子节点那么明显没有这方面的顾虑,
全排列计数即可。

分析至此,这道题细节还是蛮多的,
特殊情况,单个节点时计数为1,
最终计算答案要倍增,因为根节点位置可以对称。
*/

ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); }

///链式前向星
struct node{int u,nxt;};
int head[maxn],tot=0;
node edge[maxn<<1];
void init(){memset(head,-1,sizeof(head));tot=0;}
void add(int x,int y){edge[tot]=node{y,head[x]};head[x]=tot++;}
///阶乘处理
ll fac[maxn];
int son[maxn];///节点个数记录数组
ll dfs(int u,int pre)
{
    son[u]=1;
    int tp=0,ts=0;
    ll ret=1;
    for(int i=head[u];~i;i=edge[i].nxt)
    {
        int v=edge[i].u;
        if(v==pre) continue;
        ts++;
        ret=ret*dfs(v,u)%mod;
        if(son[v]==1) tp++;
        son[u]+=son[v];
    }
    ret=ret*fac[tp]%mod;
    if(ts-tp) ret=ret*2%mod;///如果存在非叶子节点,则答案倍增。因为可以与叶子节点互换位置
    if(ts-tp>2) ret=0;///观察是否有三个非叶子节点
    return ret;
}
///数据域
int n,x,y;

int main()
{
    fac[0]=1;for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;
    int t;scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        init();  scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);add(y,x);
        }
        printf("Case #%d: %lld\n",ca,(n==1)?1LL:2LL*dfs(1,-1)%mod);///特殊情况的考虑
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值