HDU 5379 Mahjong Tree (construction)

Source: Internet
Author: User

Topic:

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

Authoruestc
Source2015 multi-university Training Contest 7
Recommendwange2014

Test instructions: To a fixed-form tree, there are N nodes, now to assign values to these n nodes, each node takes 1 to n a certain number, cannot repeat, and satisfies: 1. The value of all child nodes of any node must be 2 consecutive. The values of the subtree of any node must be contiguous. Ask how many methods of assignment are available.


Idea: Due to the limitations of these two rules, there can be no more than two non-leaf nodes, and the non-leaf node must be the end of the continuous interval. In this case, there are two types of non-leaf nodes, and the remaining leaf nodes have k! Because the order is arbitrary. So for a root node, if it has a child, then first he has two ways to take the interval maximum or minimum, then the child's leaf node has k! The non-leaf node is then asked to go on DFS.


Code:

#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> 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) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%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#def  Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &AMP;A,CONST int    &AMP;B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*---------------------Do----------HACK-----ME--------------------*/#define MAXN 100000const int mod=1e9    +7;vector<int>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<g[u].size (); i++) {int v=g[u][i];        if (v==f) continue;        Ans= (Long Long) Ans*dfs (v,u))%mod;        if (sz[v]>1) son2++;        else son1++;    SZ[U]+=SZ[V];    } if (son2>2) return 0;    if (son2!=0) ans= ((Long Long) ans*2)%mod;    Ans= (Long 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 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<n-1;i++) {int u,v;        RII (U,V);        G[u].push_back (v);    G[v].push_back (U);    } MS0 (SZ);    int Ans=dfs (1,0); if (sz[1]>1) ans= ((Long Long) ans*2)%mod;        printf ("Case #%d:%d\n", T,ans);} return 0;}


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

HDU 5379 Mahjong Tree (construction)

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.