DP---Mahjong tree

Source: Internet
Author: User
Tags cas

HDU 5379

Problem Descriptionlittle 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 had n Vertexs, indexed from 1 to n.)
Thought for a long time, and finally he decides the mahjong to decorate the tree.
His mahjong are strange because all of the mahjong tiles had a distinct index. (Little Sun have 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 was an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules is as follows:

(1) Place exact one mahjong tiles on each vertex.
(2) The Mahjong Tiles ' index must be continues which is placed on the son Vertexs of a vertex.
(3) The Mahjong Tiles ' index must be continues which is placed on the vertexs of any subtrees.

Now he want to know so he can obtain how many different beautiful mahjong tree using these rules, because of the answer Can is very large, you need output the answer modulo 1e9 + 7. Inputthe first line of the input was 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 the integers UI and VI, which describes a edge of the tree, and Vertex 1 is The root of the tree. Outputfor each test case, output one line. The output format is ' case #x: ans ' (without quotes), X is the case number, starting from 1. Sample Input292-382 4 Sample Outputcase #1:32Case #2: AUTHORUESTC Source2015 Mu (+)   Lti-university Training Contest 7 Recommendwange2014 | We have a carefully selected several similar problems for you:5659 5658 5657 5656 5655 Test Instructions: There are n points, there are n-1 bars connected to these points as trees, nodes are from 1 to N, respectively, Node number 1th is the root of the tree. Populate the N nodes with 1~n numbers, three-point limit: 1, each node fills only one number, 2, the number of all child nodes of each node is contiguous (sorted), 3, all points in the subtree are contiguous (sorted), and how many methods of filling are available. Idea: Observe the law, a node, there are more than two subtrees tree, you can not fill out the corresponding tree, there is one or two sub-tree, the result is multiplied by 2, and then the result will be multiplied by the number of sub-leaf factorial, because the sub-leaf can be arbitrarily filled, it is the whole arrangement. The code is as follows:
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<vector>using namespacestd;Const intn=100005;Const Long Longmod=1e9+7; Vector<int>G[n];Long LongA[n],res;intDfsintUintf) {intNt=0;///all nodes that contain the root node, direct child nodes, number of subtrees    intn=g[u].size (); intns=n-1; if(u==1) ns++;  for(intI=0; i<n;i++)     {        intv=G[u][i]; if(v==f)Continue;///prevent the adjacent two nodes from repeating recursively;        intnum=DFS (V,U); if(num>1) nt++; }    if(nt>2)///There are more than two subtrees that cannot make continuous after cutting        returnres =0; if(NT)///There are only two of one or two subtree cutting methodsRes=res*2%MOD; Res=res*a[ns-nt]%mod;///when there are multiple (non-subtree) nodes free to sort;    returnns+1;}intMain () {a[0]=1;  for(intI=1; i<n;i++) A[i]=a[i-1]*i%MOD; intt,cas=1, N; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=0; i<=n;i++) g[i].clear ();  for(intI=0; i<n-1; i++)         {              intb; scanf ("%d%d",&a,&b);            G[a].push_back (b);        G[b].push_back (a); }        if(n==1) {printf ("Case #%d:1\n", cas++); Continue; } Res=1; DFS (1,0); printf ("Case #%d:%lld\n", cas++,res*2%MOD);//two kinds of cutting method of root node    }    return 0;}

DP---Mahjong tree

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.