HDU 5379 (Mahjong tree-two times palindrome string)

Source: Internet
Author: User
Tags mul



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


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 | We have carefully selected several similar problems for you:5421 5420 5419 5418 5417

Obviously, if there are more than 2 non-leaf nodes definitely no solution,

L1 leaf nodes can be fully arranged l1!

If there is a non-leaf node, probably at both ends, the answer is

If there are two non-leaf nodes, it must be at one end, the answer is

Finally, because the current root node is either in the first or the end of the answer * *, but because of these 2 cases in the parent node is determined, so no tube





#include <bits/stdc++.h> using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (i,k,n) for (int i=k;i<=n;i++) #define REP (I,n) for (int. i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (i,n) for (int i =n;i>=0;i--) #define FORP (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) # Define Lson (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (a,127 , sizeof (a)), #define MEMI (a) memset (A,128,sizeof (a)), #define INF (2139062143) #define F (1000000007) #define MAXN (200000 +10) #define MAXM (200000+10) typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (A-b+llabs (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} int n;int pre[maxn],next[maxm],edge[maxm],siz=1;void Addedge (int u,int v) {edge[++siz]=v; Next[siz]=pre[u];p Re[u]=siz;} void Addedge2 (int u,int v) {Addedge (u,v), Addedge (v,u);} int Sz[maxn];ll Jie[maxn];vOID init () {jie[0]=1; for (i,200000) jie[i]= (jie[i-1]*i)%F;} ll dfs (int x,int fa) {int l1=0,l2=0;ll ans=1; Forp (x) {int v=edge[p];if (V==FA) Continue;ans=mul (Ans,dfs (v,x)); Sz[x]+=sz[v];if (sz[v]==1) L1++;else l2++;} Sz[x]++;if (l2>2) {return 0;} if (l1+l2==0) return 1;if (l2==0) return Ans=mul (Ans,mul (jie[l1],1)); if (l2==1) return an S=mul (Ans,mul (jie[l1],2)); if (l2==2) return Ans=mul (Ans,mul (jie[l1],2));} int main () {//freopen ("k.in", "R", stdin); init (); int t;cin>>t; for (kcase,t) {mem (PRE) mem (Next) mem (edge) siz=1; MEM (SZ) scanf ("%d", &n); for (i,n-1) {int a,b;scanf ("%d%d", &a,&b); Addedge2 (A, b);} if (n==1) {printf ("Case #%d:1\n", kcase); continue;} printf ("Case #%d:%i64d\n", Kcase,mul (Dfs (1,0), 2));} return 0;}






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


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 | We have carefully selected several similar problems for you:5421 5420 5419 5418 5417

Obviously, if there are more than 2 non-leaf nodes definitely no solution,

L1 leaf nodes can be fully arranged l1!

If there is a non-leaf node, probably at both ends, the answer is

If there are two non-leaf nodes, it must be at one end, the answer is

Finally, because the current root node is either in the first or the end of the answer * *, but because of these 2 cases in the parent node is determined, so no tube






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

HDU 5379 (Mahjong tree-two times palindrome string)

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.