2015 Multi-University Training Contest 7 (hdu5379 Mahjong tree)


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
题意:有一棵有n个节点的树,有编号1~n的n个数字要放到树上,且要满足三个要求: 
1.树的每个节点只放一个数字 
2.树的任意一个节点的所有直接孩子节点上面放的数字排序后是连续的 
3.一棵子树的所有节点上面放的数字排序后是连续的 
问有多少种不同的放法。

思路:根据题意子树上的数字排列后是连续的,所以问题也就可以被转化成将字符串切分的问题,所以显而易见,如果一个飞叶子结点有>2个非叶子结点,此时无解。那么有一个或两个子树切割方法数都只有两个。

代码:

来自:https://blog.csdn.net/hyczms/article/details/47426035

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

int dfs(int u, int fa) {
    int tot = 1, ns = 0, nt = 0;//包含根节点的所有节点,直接孩子节点,子树个数
    int n = g[u].size();
    for (int i = 0; i < n; i++) {
        int v = g[u][i];
        if (v == fa)
            continue;
        ns++;   
        int num = dfs(v, u);
        if (num > 1)
            nt++;
        tot += num;
    }
    if (nt > 2)//有大于两个子树无法保证切割后连续
        return res = 0;
    if (nt)//有一个或两个子树切割方法数都只有两个
        res = res * 2 % MOD;
    res = res * A[ns - nt] % MOD;//当个节点可自由排序
    return tot;
}

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();
        int a, b;
        for (int i = 0; i < n - 1; i++) {
            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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值