hdu5379| | 2015 Multi-school Joint 7th 1011 Tree Statistics

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=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 Input
292 13 14 35 36 27 48 79 382 13 14 35 16 47 58 4

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

/**hdu5379| | 2015 Multi-school joint 7th game 1011 Tree Statistics The main idea: given a tree n nodes, with 1~n this n number to each node dyeing, brother node staining value to continuous, to the current node is the root node of the subtree of all the nodes of the dyeing value to continuous, ask total how many kinds of dyeing solution thinking: For a section Point u:1. If there are more than two nodes in the sum>=2 node of his brother, the whole tree will have no solution, output 0; 2. If there are exactly 2, then the two can be left or right, and the rest of the Sum==1 's nodes are all lined up. The answer: 2*a[son[fa[u]-          2], if FA[U] has no sibling node then Fa[u] will be in the sub-tree with its node in the leftmost or rightmost, then the answer will be multiplied by 2, for: 2*2*a[son[fa[u]-2]*dfs (son);          3. If there are 1, then he himself can be at both ends of the answer: 2*a[son[fa[u]-1]*dfs (son), the same should be considered fa[u] no sibling node 4. If there are 0, then A[son[fa[u]], the same should be considered fa[u] no sibling node situation It is worth mentioning that: for the sum of 1 of the number of nodes to be considered, see code comments */#pragma comment (linker, "/stack:102400000,102400000") #include <stdio.h># Include <string.h> #include <algorithm> #include <iostream>using namespace std;typedef long LL; const int Maxn=100005;const LL mod=1e9+7; LL A[maxn];int Head[maxn],ip,n;int son[maxn],sum[maxn],bigson[maxn],fa[maxn];///The number of sons, the node tree of the subtree of the root node I, the number of sons with more than 2 child tree nodes,    Father node void init () {memset (head,-1,sizeof (head)); Ip=0;} struct note{int v,next;} edge[maxn*2];void addedge (int u,int v) {EDge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}    void Dfs (int u,int pre)///For tree preprocessing, find various parameters {son[u]=0;    bigson[u]=0;    Sum[u]=1;    Fa[u]=pre;        for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;        if (v==pre) continue;        DFS (V,U);        son[u]++;        if (sum[v]>1) {bigson[u]++;    } Sum[u]+=sum[v];    }//printf ("sum%d:%d\n", U,sum[u]); printf ("son%d:%d\nbigson%d\n", U,son[u],bigson[u].size ());}    ll DFS1 (int u,int pre) {ll cnt=1; if (bigson[u]==1)///The number of sons with more than 1 child tree nodes is 1 {if (son[u]==1)///No sibling node at this time in the leftmost and rightmost belong to the same situation, do not need to multiply 2, the same as {cnt= (CNT        *a[son[u]-1]%mod)%mod;        } else {cnt= (cnt*a[son[u]-1]%mod*2)%mod;    } if (son[fa[u]]==1) cnt= (cnt*2)%mod;        } else if (bigson[u]==2)///The number of sons with more than 1 child tree nodes is 2 {cnt= (cnt*a[son[u]-2]%mod*2)%mod;    if (son[fa[u]]==1) cnt= (cnt*2)%mod;    } else if (BIGSON[U]&GT;2)///The number of sons with more than 1 child tree nodes is greater than 2 {return 0; } elseNumber of sons with no child tree nodes greater than 1 {if (son[u]>0) {if (son[fa[u]]==1) return (2*a[sum[u]-1])%            MoD        return a[sum[u]-1];    } else if (son[u]==0) return 1;        } for (int i=head[u]; i!=-1; i=edge[i].next)///recursion into son node {int v=edge[i].v;        if (v==pre) continue;    Cnt= (CNT*DFS1 (v,u))%mod;    }//printf ("dfs%d:%i64d\n", u,cnt); return CNT;}    int main () {a[0]=1;        for (int i=1; i<100005; i++) {a[i]= (a[i-1]*i)%mod;    printf ("%i64d\n", A[i]);    } int t,tt=0;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Init ();            for (int i=1; i<=n-1; i++) {int u,v;            scanf ("%d%d", &u,&v);            Addedge (U,V);        Addedge (V,u);        } son[0]=1;        DFS (1,0);    printf ("Case #%d:%i64d\n", ++TT,DFS1 (1,0)); } return 0;}


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

hdu5379| | 2015 Multi-school Joint 7th 1011 Tree Statistics

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.