您的位置:首页 > 其它

hdu 5379 Mahjong tree dfs

2015-08-11 18:06 183 查看


Mahjong tree

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 455 Accepted Submission(s): 143



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!的影响,dfs一遍,另外如果有一个节点有多于两个节点有儿子,那是不合法的,只有一个节点是1

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
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++;
else dfs(q[a][i]);
}
if(a!=1)k--;
if(k-cnt>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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: