Hdu 5379 Mahjong tree (constructed)

Source: Internet
Author: User
Tags bitset cmath

Hdu 5379 Mahjong tree (constructed)

Question:

 

Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 1471 Accepted Submission (s): 448



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
292 13 14 35 36 27 48 79 382 13 14 35 16 47 58 4

Sample Output
Case #1: 32Case #2: 16

Author UESTC
Source 2015 Multi-University Training Contest 7
Recommend wange2014

 

Question: assign a value to a fixed tree with n nodes. Each node must have a number ranging from 1 to n, which cannot be repeated and meets the following requirements: 1. the values of all subnodes of any node must be 2 consecutive times. the value of the subtree of any node must be continuous. How many assignment methods are available.

 

Idea: due to the limitations of these two rules, there cannot be more than two non-leaf nodes, and non-leaf nodes must be endpoints with continuous intervals. In this case, there are two methods for obtaining non-leaf nodes, and k for the remaining leaf nodes! Because the order is arbitrary. Therefore, if a root node has a child, there are two ways to obtain the maximum or minimum value of the range, and then the leaf node in the child has k! If the node is not a leaf node, run dfs.

 

Code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                # Include
               
                 # Include
                
                  # Include
                 
                   # Include
                  
                    # Include
                   
                     # Include
                    
                      # Include
                     
                       # Include
                      
                        # Include
                       
                         Using namespace std; # define PB push_back # define MP make_pair # define REP (I, x, n) for (int I = x; I <(n); ++ I) # define FOR (I, l, h) for (int I = (l); I <= (h); ++ I) # define FORD (I, h, l) for (int I = (h); I> = (l); -- I) # define SZ (X) (int) (X ). size () # define ALL (X ). begin (), (X ). end () # define RI (X) scanf (% d, & (X) # define RII (X, Y) scanf (% d, & (X ), & (Y) # define RIII (X, Y, Z) scanf (% d, & (X), & (Y), & (Z )) # define DRI (X) int (X); scanf (% d, & X) # define DRII (X, Y) int X, Y; scanf (% d, & X, & Y) # define DRIII (X, Y, Z) int X, Y, Z; scanf (% d, & X, & Y, & Z) # define OI (X) printf (% d, X); # define RS (X) scanf (% s, (X) # define MS0 (X) memset (X), 0, sizeof (X) # define MS1 (X) memset (X),-1, sizeof (X ))) # define LEN (X) strlen (X) # define F first # define S second # define Swap (a, B) (a ^ = B, B ^ =, a ^ = B) # define Dpoint strcut node {int x, y} # define cmpd int cmp (const int & a, const int & B) {return a> B ;} /* # ifdef HOME freopen(in.txt, r, stdin); # endif */const int MOD = 1e9 + 7; typedef vector
                        
                          VI; typedef vector
                         
                           VS; typedef vector
                          
                            VD; typedef long LL; typedef pair
                           
                             PII; // # define HOMEint Scan () {int res = 0, ch, flag = 0; if (ch = getchar () = '-') // determine plus and minus flag = 1; else if (ch> = '0' & ch <= '9') // obtain the complete number res = ch-'0 '; while (ch = getchar ()> = '0' & ch <= '9') res = res * 10 + ch-'0'; return flag? -Res: res ;} /* -------------- PLEASE---DO---NOT---HACK--ME -------------------- */# define MAXN 100000 const int mod = 1e9 + 7; vector
                            
                              G [MAXN + 5]; int sz [MAXN + 5]; int fact [MAXN + 5]; int dfs (int u, int f) {int ans = 1; sz [u] = 1; int son1 = 0; int son2 = 0; for (int I = 0; I
                             
                               1) son2 ++; else son1 ++; sz [u] + = sz [v];} if (son2> 2) return 0; if (son2! = 0) ans = (long) ans * 2) % mod; ans = (long) ans * fact [son1]) % mod; return ans ;} int main () {int T; RI (T); fact [0] = 1; for (int I = 1; I <= MAXN; I ++) fact [I] = (long) fact [I-1] * I) % mod; for (int t = 1; t <= T; t ++) {int n; RI (n); for (int I = 0; I <= n; I ++) g [I]. clear (); for (int I = 0; I
                              
                                1) ans = (long) ans * 2) % mod; printf (Case # % d: % d, t, ans);} return 0 ;}
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
          
         
        
       
      
     
    
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.