Mahjong tree (hdu 5379 dfs)

Source: Internet
Author: User

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


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
Recommendwange2014 | We have carefully selected several similar problems for you:5379 5378 5377 5376 5374

Test instructions: An N-node n-1 tree, now to each node label (1~N), requires: (1) Each layer of the sibling node label if continuous (2) each subtree of all the node label is continuous. Ask how many kinds of labeling schemes.

Idea: For each layer can only exist 2 non-leaf nodes, otherwise no solution; For each layer there are x leaf nodes, y non-leaf nodes, then ans= (ans * x!) %mod, in addition if y!=0, still have to ans=2*ans%mod.

Code:

#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include < algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include < vector> #include <set> #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define Pi  ACOs ( -1.0) #define EPS 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A; I <= b;  i++) #define FREE (i,a,b) for (i = A, I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A i > B; i--) #define MEM (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d         ", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d ", &a, &b, &c) #define PF Printf#define DBG PF ("hi\n") typedef long long ll;using namespace std; #define INF 0x3f3f3f3f#define mod 1000000007const int maxn = 1005;co NST int MAXN = 100001;conSt int MAXM = 200002;const int N = 1005;struct edge{int u,v,next;}    Edge[maxm];int tot,head[maxn],n;ll fn[maxn],ans;bool flag;void INIT () {fn[0]=1; for (int i=1;i<=100000;i++) fn[i]= (fn[i-1]* (ll) i)%mod;}    void Init () {Tot=0;flag=true;ans=1; memset (head,-1,sizeof (Head));}    void Addedge (int u,int v) {edge[tot].u=u;    Edge[tot].v=v;    Edge[tot].next=head[u]; head[u]=tot++;}    ll DFS (int u,int pre) {if (!flag) return 0;    ll S=1,all=0,ss=0;        for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v;        if (v==pre) continue;        ll X=dfs (v,u);        S+=x;        if (x==1) all++;    else if (x>1) ss++;    } if (ss>2) Flag=false;        else{ans= (Ans*fn[all])%mod;    if (ss!=0) ans= ((LL) 2*ans)%mod; } return s;}    int main () {#ifndef Online_judge freopen ("C:/users/lyf/desktop/in.txt", "R", stdin), #endif int i,j,t,u,v,cas=0;    INIT ();    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);   Init ();     for (i=0;i<n-1;i++) {scanf ("%d%d", &u,&v);            Addedge (U,V);        Addedge (V,u);        } dfs (1,-1);        if (n>1) ans= ((LL) 2*ans)%mod;        if (!flag) ans=0;    printf ("Case #%d:%lld\n", ++cas,ans); } return 0;}



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

Mahjong tree (hdu 5379 dfs)

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.