hdu5379||2015多校联合第7场1011 树形统计

http://acm.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

/**
hdu5379||2015多校联合第7场1011  树形统计
题目大意:给定一棵树n个节点,用1~n这n个数给每一个节点染色,兄弟节点染色值要连续,以当前节点为根节点的子树中所有节点的染色值要连续,问总共有
           多少种染色的方案
解题思路:对于一个节点u:
          1.如果他的兄弟节点中sum>=2的节点如果有两以上,则整棵树无解,输出0;
          2.如果正好有2个那么二者可以分别在最左或最右,剩下的sum==1的节点全排列那么答案:2*A[son[fa[u]-2],如果fa[u]无兄弟节点那么fa[u]将在
          以其为节点的子树中可以在最左或最右边,那么答案还要乘2,为:2*2*A[son[fa[u]-2]*dfs(son);
          3.如果有1个,那么他自己可在两头答案:2*A[son[fa[u]-1]*dfs(son);,同样要考虑fa[u]无兄弟节点的情况
          4.如果有0个,那么A[son[fa[u]]],同样要考虑fa[u]无兄弟节点的情况
          值得一提的是:对于sum为1的节点的个数也要考虑,具体见代码注释
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn=100005;
const LL mod=1e9+7;

LL A[maxn];
int head[maxn],ip,n;
int son[maxn],sum[maxn],bigson[maxn],fa[maxn];
///儿子的数目,根节点为i的子树的节点树,子树节点数大于2的儿子个数,父亲节点

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

struct note
{
    int v,next;
} edge[maxn*2];

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

void dfs(int u,int pre)///对树的预处理,求出各种参数
{
    son[u]=0;
    bigson[u]=0;
    sum[u]=1;
    fa[u]=pre;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==pre)continue;
        dfs(v,u);
        son[u]++;
        if(sum[v]>1)
        {
            bigson[u]++;
        }
        sum[u]+=sum[v];
    }
    //printf("sum%d:%d\n",u,sum[u]);
    // printf("son%d:%d\nbigson%d\n",u,son[u],bigson[u].size());
}

LL dfs1(int u,int pre)
{
    LL cnt=1;
    if(bigson[u]==1)///子树节点数大于1的儿子个数为1
    {
        if(son[u]==1)///无兄弟节点此时在最左边和最右边属于同一种情况,无须乘2,下同
        {
            cnt=(cnt*A[son[u]-1]%mod)%mod;
        }
        else
        {
            cnt=(cnt*A[son[u]-1]%mod*2)%mod;
        }
        if(son[fa[u]]==1)cnt=(cnt*2)%mod;
    }
    else if(bigson[u]==2)///子树节点数大于1的儿子个数为2
    {
        cnt=(cnt*A[son[u]-2]%mod*2)%mod;
        if(son[fa[u]]==1)cnt=(cnt*2)%mod;
    }
    else if(bigson[u]>2)///子树节点数大于1的儿子个数大于2
    {
        return 0;
    }
    else///无子树节点数大于1的儿子个数
    {
        if(son[u]>0)
        {
            if(son[fa[u]]==1)
                return (2*A[sum[u]-1])%mod;
            return A[sum[u]-1];
        }
        else if(son[u]==0)
            return 1;
    }
    for(int i=head[u]; i!=-1; i=edge[i].next)///递归进入儿子节点
    {
        int v=edge[i].v;
        if(v==pre)continue;
        cnt=(cnt*dfs1(v,u))%mod;
    }
    // printf("dfs%d:%I64d\n",u,cnt);
    return cnt;
}
int main()
{
    A[0]=1;
    for(int i=1; i<100005; i++)
    {
        A[i]=(A[i-1]*i)%mod;
        //printf("%I64d\n",A[i]);
    }
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i=1; i<=n-1; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        son[0]=1;
        dfs(1,0);
        printf("Case #%d: %I64d\n",++tt,dfs1(1,0));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值