首页 > 其他分享 >HDU 5379 Mahjong tree

HDU 5379 Mahjong tree

时间:2022-11-09 20:41:15浏览次数:48  
标签:HDU 5379 int tree mahjong ans include he


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个,否则就无解,然后分类讨论一下,一次dfs可以解决

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100005;
const LL base=1e9+7;
vector<int> tree[maxn];
int T,n,x,y,tt=0;
LL ans[maxn],f[maxn],flag;

void dfs(int x,int fa)
{
if (!flag) return ;
LL a=tree[x].size(),b=0;
ans[x]=1;
for (int i=0;i<a;i++)
if (tree[x][i]!=fa)
{
dfs(tree[x][i],x);
if (!flag) return ;
ans[x]=(ans[x]*ans[tree[x][i]])%base;
b+=(tree[tree[x][i]].size()>1)?1:0;
}
if (b>2) {flag=0; return ;}
ans[x]=ans[x]*f[a-1-b]%base;
if (b) ans[x]=(ans[x]<<1)%base;
}

int main()
{
for (LL i=f[0]=1;i<=100000;i++) f[i]=f[i-1]*i%base;
scanf("%d",&T);
while (T--)
{
flag=1;
scanf("%d",&n);
if (n==1){ printf("Case #%d: 1\n",++tt); continue;}
for (int i=1;i<=n;i++) tree[i].clear();
tree[1].push_back(1);
for (int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
tree[x].push_back(y);
tree[y].push_back(x);
}
dfs(1,1);
printf("Case #%d: %lld\n",++tt,flag?((ans[1]<<1)%base):flag);
}
return 0;
}



标签:HDU,5379,int,tree,mahjong,ans,include,he
From: https://blog.51cto.com/u_15870896/5838715

相关文章

  • HDU 3397 Sequence operation
    ProblemDescriptionlxhgwwgotasequencecontainsncharacterswhichareall'0'sor'1's.Wehavefiveoperationshere:Changeoperations:0ab......
  • HDU 1255 覆盖的面积
    ProblemDescription给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试......
  • HDU 2665 Kth number
    ProblemDescriptionGiveyouasequenceandaskyouthekthbignumberofainteval.InputThefirstlineisthenumberofthetestcases. F......
  • HDU 2715 Herd Sums
    DescriptionThecowsinfarmerJohn'sherdarenumberedandbrandedwithconsecutiveintegersfrom1toN(1<=N<=10,000,000).Whenthecowscometot......
  • HDU 5339 Untitled
    ProblemDescriptiona and n integers b1,…,bn.Afterselectingsomenumbersfrom b1,…,bn inanyorder,say c1,…,cr,wewanttom......
  • HDU 3760 Ideal Path
    ProblemDescriptionNewlabyrinthattractionisopeninNewLostlandamusementpark.Thelabyrinthconsistsofnroomsconnectedbympassages.Eachpass......
  • HDU 4101 Ali and Baba
    ProblemDescriptionThereisarectanglearea(withNrowsandMcolumns)infrontofAliandBaba,eachgridmightbeoneofthefollowing:1.Empty......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......