2015暑假多校联合---Mahjong tree(树上DP 、深搜)

题目链接

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

2015 Multi-University Training Contest 7

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=100005;
const long long MOD=1e9+7;
vector<int>g[N];
long long A[N],res;

int dfs(int u,int f)
{
    int tot=1,nt=0;///包含根节点的所有节点,直接孩子节点,子树个数
    int n=g[u].size();
    int ns=n-1;
    if(u==1) ns++;
    for(int i=0;i<n;i++)
    {
        int v=g[u][i];
        if(v==f)  continue;///防止相邻的两个节点上下反复递归;
        int num=dfs(v,u);
        if(num>1)   nt++;
    }
    if(nt>2)///有大于两个子树不能使切割后连续
        return res =0;
    if(nt)///有一个或两个子树切割方法数都只有两个
        res=res*2%MOD;
    res=res*A[ns-nt]%MOD;///当有多个(非子树的)节点可自由排序;
    return ns+1;
}

int main()
{
    A[0]=1;
    for (int i=1;i<N;i++)
    A[i]=A[i-1]*i%MOD;

    int t,cas=1,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<=n;i++)
        g[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if(n==1)
        {
            printf("Case #%d: 1\n",cas++);
            continue;
        }
        res=1;
        dfs(1,0);
        printf("Case #%d: %lld\n",cas++,res*2%MOD);//根节点左右两种切法
    }
    return 0;
}
时间: 2024-11-14 00:53:00

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

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

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numbers a1,a2,?,anand n−1 operators("+", "-" or "*")op1,op2,?,opn−1, which are arranged in the form a1 op1 a2 op2 a3 ? an. He wan

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

原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they can choose to become online friends (communicating using online applications) or offline friends (mostly using face-to-face communication). However, e

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

题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son.She went to the nearest shop with M Won(currency unit).At the shop, there are N kinds of pr

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

2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on the top of a mountain. Recently, our old monk found the operating system of his computer was updating to windows 10 automatically and he even can't j

2016暑假多校联合---Substring(后缀数组)

2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings. But ?? thinks that is too easy, he wants to make this problem more interesti

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

2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has an array A with n numbers. Then he make

2016暑假多校联合---Another Meaning

2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”. Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to

HDOJ 5379 Mahjong tree 树形DP

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

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