HDU 5379(Mahjong tree-二次回文串)

Mahjong tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1460    Accepted Submission(s): 445

Problem Description

Little 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 has n vertexs, indexed from 1 to n.)

Thought for a long time, finally he decides to use the mahjong to decorate the tree.

His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has 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 is an artist. So he want to decorate the tree as beautiful as possible.

His decoration rules are as follows:

(1)Place exact one mahjong tile on each vertex.

(2)The mahjong tiles‘ index must be continues which are placed on the son vertexs of a vertex.

(3)The mahjong tiles‘ index must be continues which are placed on the vertexs of any subtrees.

Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.

Input

The first line of the input is 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 two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.

Output

For 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

2
9
2 1
3 1
4 3
5 3
6 2
7 4
8 7
9 3
8
2 1
3 1
4 3
5 1
6 4
7 5
8 4

Sample Output

Case #1: 32
Case #2: 16

Author

UESTC

Source

2015 Multi-University Training Contest 7

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417

显然,如果有大于2个非叶子节点肯定无解,

l1叶子节点可以全排列l1!

如果有一个非叶子节点,可能在两端,答案*2

如果有两个非叶子节点,必然各在一端,答案*2

最后由于当前根节点要么在首要么在尾答案*2,但由于这2种情况在父节点的时候才被决定,所以不用管

#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-b)/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];
	pre[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 ans=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 tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1460    Accepted Submission(s): 445

Problem Description

Little 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 has n vertexs, indexed from 1 to n.)

Thought for a long time, finally he decides to use the mahjong to decorate the tree.

His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has 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 is an artist. So he want to decorate the tree as beautiful as possible.

His decoration rules are as follows:

(1)Place exact one mahjong tile on each vertex.

(2)The mahjong tiles‘ index must be continues which are placed on the son vertexs of a vertex.

(3)The mahjong tiles‘ index must be continues which are placed on the vertexs of any subtrees.

Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.

Input

The first line of the input is 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 two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.

Output

For 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

2
9
2 1
3 1
4 3
5 3
6 2
7 4
8 7
9 3
8
2 1
3 1
4 3
5 1
6 4
7 5
8 4

Sample Output

Case #1: 32
Case #2: 16

Author

UESTC

Source

2015 Multi-University Training Contest 7

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5421 5420 5419 5418 5417

显然,如果有大于2个非叶子节点肯定无解,

l1叶子节点可以全排列l1!

如果有一个非叶子节点,可能在两端,答案*2

如果有两个非叶子节点,必然各在一端,答案*2

最后由于当前根节点要么在首要么在尾答案*2,但由于这2种情况在父节点的时候才被决定,所以不用管

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 03:24:41

HDU 5379(Mahjong tree-二次回文串)的相关文章

hdu 5379 Mahjong tree(树形dp)

题目链接:hdu 5379 Mahjong tree 树形dp,每个节点最多有2个子节点为一棵节点数大于1的子树的根节点,而且要么后代的节点值都大于,要么都小于本身(所以tson不为0是,要乘2).对于K个单一节点的子节点,种类数即为全排K!.当一个节点没有兄弟节点时,以这个节点为根结点的子树,根可以选择最大或者最小. #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

Hdu 5379 Mahjong tree (dfs + 组合数)

题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号? 解题思路: 对于一个节点来讲,非叶子儿子节点最多有两个才能满足要求,否则满足子树节点连续的话就无法满足兄弟节点连续.然后有dfs计算每棵子树的贡献值,每棵子树的子节点可以分为叶子节点X和非叶子节点Y,叶子节点可以分配到一组连续的编号,非叶子节点只能分配到兄弟节点中最大或者最小编号两种情况,叶子节

HDU 5371(Hotaru&#39;s problem-2次回文串)

Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2432    Accepted Submission(s): 841 Problem Description Hotaru Ichijou recently is addicated to math problems. Now she is playin

HDU 5379 Mahjong tree(dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little 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 has n verte

HDU 5379 Mahjong tree(树的遍历&amp;amp;组合数学)

本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=5379 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Little sun is an artist. Today he is playing

HDU 5379 Mahjong tree (详解,构造+思维)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题面: Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1148    Accepted Submission(s): 351 Problem Description Little sun is an art

hdu 5379 Mahjong tree(构造)

题目: 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 Description Little sun is an artist. Today he is playing mahjong alone. He suddenly

hdu 5379 Mahjong tree 树形DP入门

Description Little 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 has n vertexs, indexed from 1 to n.) Thought for a long time, finally he

2015多校第7场 HDU 5379 Mahjong tree 构造,DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的标号要是连续的(2)每一颗子树的所有节点标号是连续的.问有多少种标号方案. 解法:对于每一层顶多只能存在2个非叶子节点,否则无解:对于每一层有x个叶子节点,y个非叶子节点,那么ans=(ans * x!)%mod,另外如果y!=0,还得ans=2*ans%mod. #include <bits/st

hdu 5379 Mahjong tree 树形dp

链接 题意:给定一棵树 把1-n填到树的节点上,使得: 1:儿子节点上填的数字是连续的. 2:子树节点上填的数字是连续的. 把儿子节点分成两种,一种是叶子节点,一种是非叶子节点. 显然非叶子节点个数不能超过2个,不然就不存在这样的方案了. 然后分类讨论一下非叶子节点个数即可. #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #i