HDU 5379 Mahjong Tree dp Getting Started

Source: Internet
Author: User

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 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.

Input

The 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.

Output

For each test case, the output one line. The output format is ' case #x: ans ' (without quotes), X is the case number, starting from 1.

Sample Input

Sample Output

The main topic: there is a tree of n points, the number of 1~n in these n points, to meet the following conditions of the maximum number of fill

1. All sons nodes of any node should be filled with successive numbers.

2. All nodes in any subtree should be filled with successive numbers.

Idea: Define DP[U] to indicate the number of successive numbers that are filled with the subtree of the root of U.

For the current node U, discuss the classification of its son node:

1, you son node if there are more than two nodes is not a leaf node, then dp[u]=0

2, u only one son node v,dp[u] = dp[v]*2;

3, u have two sons node V1,v2, and these two sons nodes are not leaf nodes, dp[u] = Dp[v1]*dp[v2]

...... The general idea is this, the rest of the classification is omitted.

#include <vector>#include<cstdio>#include<cstring>using namespacestd;Const intMAXN =100005;Const intMoD = 1e9 +7;intN;Long LongDP[MAXN];BOOLVis[maxn];vector<int>ADJ[MAXN];Long LongSolveintu) {    if(dp[u]!=-1)returnDp[u]; Vis[u]=true; intx=0, v1=-1, v2=-1; Dp[u]=1; intlen=adj[u].size (); intson=0;  for(intI=0; i<len; i++)    {        intv =Adj[u][i]; if(Vis[v])Continue;        Solve (v); Dp[u]*=Dp[v]; Dp[u]%=MoD; if(dp[v]==1) x + +; Son++; }    if(son-x>2|| dp[u]==0)returndp[u]=0; Else if(son==1) {Dp[u]*=2; returndp[u]%=MoD; }    if(x) {if(son==x| | son-x==1) dp[u]*=2; Dp[u]%=MoD;  while(x) {Dp[u]*=x; Dp[u]%=MoD; X--; }    }    returndp[u]%=MoD;}intMain () {intT; scanf ("%d",&T); intKase=1;  while(t--) {scanf ("%d",&N);  for(intI=1; i<n; i++)        {            intx, y; scanf ("%d%d",&x,&X);            Adj[x].push_back (y);        Adj[y].push_back (x); }        intRoot =1; Memset (DP,-1,sizeof(DP)); memset (Vis,0,sizeof(VIS)); printf ("Case #%d:%i64d\n", kase++, solve (root));  for(intI=1; i<=n; i++) adj[i].clear (); //for (int i=1;i<=n;i++) printf ("%d%d\n", I,dp[i]);    }    return 0;}

HDU 5379 Mahjong Tree dp Getting Started

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.