Mahjong tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 388 Accepted Submission(s): 125
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.
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.
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
解题:。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #pragma comment(linker, "/STACK:102400000,102400000") 6 using namespace std; 7 const int maxn = 100010; 8 typedef long long LL; 9 const LL mod = 1e9 + 7; 10 vector<int>g[maxn]; 11 int son[maxn]; 12 LL fac[maxn] = {1}; 13 LL dfs(int u,int fa){ 14 son[u] = 1; 15 LL ret = 1; 16 int a = 0,b = 0; 17 for(int i = g[u].size()-1; i >= 0; --i){ 18 if(g[u][i] == fa) continue; 19 ret = ret*dfs(g[u][i],u)%mod; 20 if(son[g[u][i]] > 1) b++; 21 else a++; 22 if(!ret || b > 2) return 0; 23 son[u] += son[g[u][i]]; 24 } 25 if(b) ret = ret*2%mod; 26 ret = ret*fac[a]%mod; 27 return ret; 28 } 29 void init(){ 30 for(int i = 1; i < maxn; ++i) 31 fac[i] = i*fac[i-1]%mod; 32 } 33 int main(){ 34 int kase,n,u,v,cs = 1; 35 init(); 36 scanf("%d",&kase); 37 while(kase--){ 38 scanf("%d",&n); 39 for(int i = 0; i < maxn; ++i) {g[i].clear();son[i] = 0;} 40 for(int i = 1; i < n; ++i){ 41 scanf("%d%d",&u,&v); 42 g[u].push_back(v); 43 g[v].push_back(u); 44 } 45 LL ret = dfs(1,-1); 46 if(son[1] > 1) ret = ret*2%mod; 47 printf("Case #%d: %I64d ",cs++,ret); 48 } 49 return 0; 50 }