Mahjong tree(构造+dfs+思维)(好题!!!)

Mahjong tree

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

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:  6349 6348 6347 6346 6345 

题意:给一固定形态的树,有n个节点,现在要给这n个节点赋值,每个节点取1到n的某个数,不能重复,并且满足:1.任意节点的所有子节点的值必须连续 2.任意节点的子树的值必须连续。 问有多少种赋值方法。

思路:由于这两条规则的限制,不能有超过两个非叶子节点,并且非叶子节点一定是取连续区间的端点。这样的话非叶子节点有两种取法,剩下的叶子节点有k!种取法,因为顺序是任意的。所以对于某个根节点而言,它如果有孩子,那么首先他有两种取法,可以取区间最大或最小值,然后孩子中的叶子节点有k!取法,非叶节点再dfs下去求。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Max 100010
const int  MOD = 1e9+7;
vector <int > q[Max];
int vis[Max];
long long res=1;
int flag=1;
int n;
void dfs(int a)
{
    int k=q[a].size();///表示有几个儿子
    int cnt=0;
    vis[a]=1;///说明这个点被染上了颜色
    for(int i=0;i<k;i++)
    {
        if(vis[q[a][i]]) ///如果染过
            continue;
        if(q[q[a][i]].size()==1 && vis[q[a][i]]==0)///如果这个儿子是叶子节点   并且这个儿子没有染色
            cnt++;///cnt:叶子节点的个数
        else 
            dfs(q[a][i]);///dfs下去
    }
    if(a!=1)///如果a是1,那么儿子节点就是k,否则有个父节点的那一支,减一才是儿子节点的个数
        k--;
    if(k-cnt>2){///非叶子节点的个数大于2,就不合法了
        flag=0;
        return ;
    }
    if(cnt!=k)
    {
        res*=2;///因为非叶子节点只能去区间的最大或最小值
        res%=MOD;
    }
    //cout<<k<<" "<<cnt<<" ";
    while(cnt>0){///叶子节点的排列组合方式是叶子节点的阶乘。
        res*=cnt;   
        res%=MOD;   
        cnt--;///相当于求阶乘
    }
    //cout<<a<<" "<<res<<endl;
    //cout<<res<<endl;
}
int main()
{
    int T;
    cin>>T;
    int _case=0;
    while(T--)
    {
        cin>>n;
        flag=1;
        for(int i=1;i<=n;i++) 
            q[i].clear();
        memset(vis,0,sizeof(vis));
        int a,b;
        for(int i=1;i<n;i++)
        {
            scanf("%d %d",&a,&b);
            q[b].push_back(a);
            q[a].push_back(b);
        }
        res=2;
        if(n!=1)
            dfs(1);
        else 
            res=1;
        if(!flag) 
            res=0;
        cout<<"Case #"<<++_case<<": "<<res%MOD<<endl;
    }
    return 0;
}

参考博客:https://blog.csdn.net/u013840081/article/details/48012323

这道题需要认真分析一波,想明白。

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/81450023
今日推荐