Hdoj 5379 Mahjong Tree DP

Source: Internet
Author: User


Mark all points in a tree, requiring that the points in any subtree be numbered consecutively, and that each point's son is numbered consecutively. Well, a point of the non-leaf son should be continuous, that is, a point of non-leaf son up to only two. For each point, we take the number of its leaf son as S, and all of the sons ' schemes count as T. When the number of non-leaf son nodes is less than 2, the scheme number is 2t* (s!). When the number of non-leaf sons equals 2, the point is the subtree of the root of the legal scheme number t* (s!). This way Dfs can handle the number of scenarios for the whole tree over and over.

Mahjong TreeTime limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 742 Accepted Submission (s): 227


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

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

Source2015 multi-university Training Contest 7

/* ***********************************************author:ckbosscreated time:2015 August 12 Wednesday 10:23 35 seconds file Name : hdoj5379.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map> #pragma comment (linker, "/ stack:1024000000,1024000000 ") using namespace std;typedef long long int ll;const int maxn=100100;const LL mod=1e9+7;int n; LL Jc[maxn];int SONS[MAXN],S[MAXN]; ll dp[maxn];vector<int> g[maxn];void JC () {jc[0]=1ll;for (LL i=1;i<maxn;i++) jc[i]= (jc[i-1]*i)%mod;} void Init () {for (int i=1;i<=n;i++) {g[i].clear (); sons[i]=0; s[i]=0; dp[i]=0;}} void Check_dfs (int u,int fa) {sons[u]=g[u].size (); if (U!=FA) sons[u]--;if (sons[u]==0) return; LL ts=0;for (int i=0,sz=g[u].size (); i<sz;i++) {int v=g[u][i];if (V==FA) Continue;check_dfs (v,u); if(sons[v]==0) ts++;} S[u]=sons[u]-ts;} void Dfs (int u,int fa) {if (sons[u]==0) {Dp[u]=1ll;return;} LL t=1;for (int i=0,sz=g[u].size (); i<sz;i++) {int v=g[u][i];if (v==fa| | v==0) Continue;dfs (v,u); T= (T*dp[v])%mod;} if (s[u]==2) {dp[u]=t*jc[sons[u]-2]%mod;} Else{dp[u]=2*t*jc[sons[u]-s[u]]%mod;}} int main () {//freopen ("In.txt", "R", stdin),//freopen ("OUT.txt", "w", stdout); JC (); int cas=1,t_t;scanf ("%d", &t_t), while (t_t--) {scanf ("%d", &n); init (); for (int i=0,u,v;i<n-1;i++) { scanf ("%d%d", &u,&v); G[u].push_back (v); G[v].push_back (u);} Check_dfs, bool fg=true;for (int i=1;i<=n&&fg;i++) if (s[i]>2) fg=false;if (fg==false) {printf ("case #%d:0\n ", cas++); continue;} DFS (//for) (i=1;i<=n;i++) printf ("%d:dp:%lld sons:%d S:%d\n", I,dp[i],sons[i],s[i]);p rintf ("Case #%d:%d\n"        , cas++, (int) dp[1]);} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 5379 Mahjong Tree DP

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.