【HDU5379】【Mahjong tree】

Mahjong tree

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


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
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5378  5377  5376  5375  5374 


#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll N = 100010;
ll jie[N];
void init()
{
    jie[0] = 1;
    jie[1] = 1;
    for(ll i=2;i<=100000;i++)
    {
        jie[i] = jie[i-1] * i % MOD;
    }
}
ll T;
ll n;
vector<ll> g[N];
ll f[N][3]; 
bool vis[N];
bool ok[N];
void dfs(ll u)
{
    if(ok[u]) return;
    vis[u] = true;
    int amt = 0;

    f[u][0] = 1;
    f[u][1] = 0;
    f[u][2] = 0;
    ll cnt = 0;
    if(f[u][1] == 0) f[u][1] = 1; 


    for(ll i=0;i<g[u].size();i++)
    {
        ll v = g[u][i];
        if(!vis[v]) dfs(v);
        if(ok[v])
        {
            f[u][0] = (f[u][0]*f[v][0]) % MOD;
            
            f[u][1] += (f[v][1]);
            f[u][2] ++;
        }

        if(ok[v] && f[v][1] >= 2) cnt++;
        if(cnt >= 3) 
        {
            f[u][0] = 0;
            ok[u] = true;
            return;
        }
    }
    
    if(cnt >= 3)
	{
		f[u][0] = 0;
	}
    else if(cnt == 0) 
    {
        if(f[u][2] == 0) 
		{
			f[u][0] = f[u][0];
		}
        else 
		{
        	f[u][0] = f[u][0] * 2 * jie[f[u][2]] % MOD;
		}
    }
    else if(cnt == 1) 
	{
		f[u][0] = 2 * f[u][0] * jie[f[u][2] - 1] % MOD;
	}
    else if(cnt == 2) 
	{
		f[u][0] =  f[u][0] * jie[f[u][2] - 2] % MOD;
	}
    ok[u] = true;
     
}

int main()
{
    ll C = 1;
    init();
    scanf("%I64d",&T);
    while(T --)
    {
        scanf("%I64d",&n);
        for(ll i=1;i<=n;i++)
        {
            vis[i] = 0;
        }
        ll u,v;
        for(ll i=0;i<=n;i++)
        {
            g[i].clear();
        }
        for(ll i=0;i<n-1;i++)
        {
            scanf("%I64d%I64d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        memset(f,-1,sizeof(f));
        memset(vis,0,sizeof(vis));
        memset(ok,0,sizeof(ok));
        dfs(1);
        printf("Case #%I64d: %I64d\n",C++,f[1][0] % MOD);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值