程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 5379(Mahjong tree-樹形dp統計標號)

HDU 5379(Mahjong tree-樹形dp統計標號)

編輯:C++入門知識

HDU 5379(Mahjong tree-樹形dp統計標號)



 

Mahjong tree

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



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

Author UESTC
Source 2015 Multi-University Training Contest 7
Recommend wange2014 | We have carefully selected several similar problems for you: 5421 5420 5419 5418 5417

 

 

顯然,如果有大於2個非葉子節點肯定無解,

l1葉子節點可以全排列l1!

如果有一個非葉子節點,可能在兩端,答案*2

如果有兩個非葉子節點,必然各在一端,答案*2

最後由於當前根節點要麼在首要麼在尾答案*2,但由於這2種情況在父節點的時候才被決定,所以不用管

 

 

 

 

 

#include 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i=0;i--)
#define Forp(x) for(int p=pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (200000+10)
#define MAXM (200000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n;
int pre[MAXN],Next[MAXM],edge[MAXM],siz=1;
void addedge(int u,int v) {
	edge[++siz]=v;
	Next[siz]=pre[u];
	pre[u]=siz;
} 
void addedge2(int u,int v){addedge(u,v),addedge(v,u);}
int sz[MAXN];

ll jie[MAXN];
void init(){
	jie[0]=1;
	For(i,200000) jie[i]=(jie[i-1]*i)%F;
}
 


ll dfs(int x,int fa)
{
	int l1=0,l2=0;
	ll ans=1;
	Forp(x) {
		int v=edge[p];
		if (v==fa) continue;
		ans=mul(ans,dfs(v,x));
		sz[x]+=sz[v];
		if (sz[v]==1) l1++;else l2++;
	}
	sz[x]++;	
	if (l2>2) {
		return 0;
	} 
	if (l1+l2==0) return 1;
	if (l2==0) return ans=mul(ans,mul(jie[l1],1));
	if (l2==1) return ans=mul(ans,mul(jie[l1],2));
	if (l2==2) return ans=mul(ans,mul(jie[l1],2));
	
	
}
int main()
{
//	freopen(K.in,r,stdin);
	init(); 
	int T;cin>>T;
	For(kcase,T) {
		MEM(pre) MEM(Next) MEM(edge) siz=1;
		MEM(sz)
		scanf(%d,&n);
		For(i,n-1) {
			int a,b;
			scanf(%d%d,&a,&b);
			addedge2(a,b);
		}
		if (n==1) {
			printf(Case #%d: 1
,kcase); continue;
		}	
		
		
		printf(Case #%d: %I64d
,kcase,mul(dfs(1,0),2));
	}
	
	return 0;
}


 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved