HDU 5379 (dfs)

Mahjong tree

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


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
 


题意:给一棵树每个节点分配一个编号,使得:子树的编号连续;兄弟的编号连续.

发现每个节点最多只能有2个非叶子孩子,所以特判下0答案.然后假设一个子树

的根节点有p个叶子孩子,q个非叶子孩子,那么q如果大于0对答案的贡献就是2(正

两种方向反),p对答案的贡献就是p!(叶子节点全排列).

然后按照这样的计算方法dfs下就好了.因为根比较特殊,所以根节点要*2(头尾都

可以放根),但是n=1不必.

#include <bits/stdc++.h>
using namespace std;
#define maxn 111111
#define mod 1000000007

int n;
struct node {
    int v, next;
}edge[maxn<<1];
int head[maxn], cnt;

void add_edge (int u, int v) {
    edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;
}

bool is_leaf[maxn];
long long A[maxn];

void init () {
    A[0] = 1;
    for (int i = 1; i < maxn; i++)
        A[i] = A[i-1]*i%mod;
}

long long ans;
bool flag;

void dfs (int u, int fa) {
    int son = 0;
    int not_leaf_son = 0;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if (v == fa)
            continue;
        son++;
        dfs (v, u);
        if (!is_leaf[v])
            not_leaf_son++;
    }
    if (!son) {
        is_leaf[u] = 1;
    }
    if (not_leaf_son >= 3)
        flag = 1;
}

long long cal (int son, int leaf_son) {
    int not_leaf_son = son-leaf_son;
    if (!son)
        return 1;
    if (not_leaf_son) {
        return 2*A[leaf_son]%mod;
    }
    else return A[leaf_son];
}

long long go (int u, int fa) {
    int son = 0, leaf_son = 0;
    long long tmp = 1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if (v == fa)
            continue;
        tmp = tmp*go (v, u)%mod;
        son++;
        if (is_leaf[v])
            leaf_son++;
    }
    tmp = tmp * cal (son, leaf_son) % mod;
    return tmp;
}

void solve () {
    flag = 0;
    dfs (1, 0);
    if (flag) {
        printf ("0\n");
        return ;
    }
    ans = 2;//第一个节点可以放在头尾
    int son = 0, leaf_son = 0;
    for (int i = head[1] ; i != -1; i = edge[i].next) {
        int v = edge[i].v;
        if (is_leaf[v])
            leaf_son++;
        son++;
        ans = ans*go (v, 1)%mod;
    }
    ans = ans * cal (son, leaf_son) % mod;
    printf ("%lld\n", ans%mod);
}

int main () {
    //freopen ("in.txt", "r", stdin);
    init ();
    int kase = 0, t;
    scanf ("%d", &t);
    while (t--) {
        printf ("Case #%d: ", ++kase);
        scanf ("%d", &n);
        if (n == 1) {
            printf ("1\n");
            continue;
        }
        memset (head, -1, sizeof head);
        memset (is_leaf, 0, sizeof is_leaf);
        cnt = 0;
        for (int i = 1; i < n; i++) {
            int u, v;
            scanf ("%d%d", &u, &v);
            add_edge (u, v);
            add_edge (v, u);
        }
        solve ();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值