Mahjong tree

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

Total Submission(s): 190    Accepted Submission(s): 58

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

题意:一颗n个节点n-1条边的树,如今要给每一个节点标号(1~n),要求:(1)每一层的兄弟节点的标号要是连续的(2)每一颗子树的全部节点标号是连续的。问有多少种标号方案。

思路:对于每一层顶多仅仅能存在2个非叶子节点。否则无解;对于每一层有x个叶子节点,y个非叶子节点,那么ans=(ans * x!)%mod,另外假设y!=0,还得ans=2*ans%mod。

代码:

#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 1000000007
const int maxn = 1005;
const 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;
}

Mahjong tree (hdu 5379 dfs)的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...

  2. HDU 5379 Mahjong tree(dfs)

    题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...

  3. Hdu 5379 Mahjong tree (dfs + 组合数)

    题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...

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

    本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...

  5. HDU 5379——Mahjong tree——————【搜索】

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. 2015 Multi-University Training Contest 7 hdu 5379 Mahjong tree

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. 【HDOJ 5379】 Mahjong tree

    [HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...

  8. S - Query on a tree HDU - 3804 线段树+dfs序

    S - Query on a tree HDU - 3804   离散化+权值线段树 题目大意:给你一棵树,让你求这棵树上询问的点到根节点直接最大小于等于val的长度. 这个题目和之前写的那个给你一棵 ...

  9. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

随机推荐

  1. angular4自定义组件非input元素实现ngModel双向数据绑定

    在angular里我们一般都是给input元素添加[(ngModel)]="value"实现数据双向绑定,如果想实现自定义的组件上实现ngModel双向数据绑定应该怎么办呐... ...

  2. java之 ------ 几种常见的简单设计模式

    前言: 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.用于解决特定环境下.反复出现的特定问题的解决方式.使用设计模式是为了可重用代码.让代 ...

  3. 机房收费系统——UML类图

    在对一个软件系统进行设计和建模的时候,一般是从构造系统的基本词汇開始,包含构造这些词汇的基本属性和行为. 系统分析师假设要对所设计的系统清晰认识.还有考虑这些基本词汇之间的关系.而假设把这些行为可视化 ...

  4. javescript定时器demo

    本来认为这个事情还是挺easy的不值得写上去,今天同事突然问我.我心想曾经写过,可是就是想不起函数的名称的,于是翻了一下原来的代码. function run() { interval = setIn ...

  5. HDU 1429 胜利大逃亡(续)(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. ZOJ 1654 Place the Robots(放置机器人)------最大独立集

    Place the Robots http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1654 Time Limit: 5 Sec ...

  7. 黑马程序猿——JAVA面向对象的特性:封装,继承,多态

                                           - ----------android培训.java培训.java学习型技术博客.期待与您交流!------------  ...

  8. Split Shape by Plane in OpenCASCADE

    Split Shape by Plane in OpenCASCADE eryar@163.com Abstract. Sometimes you want to split a shape by p ...

  9. 关于jquery的事件命名空间

    在多人协作的前端开发中,或许会遇到一个问题:几个开发人员都给一个元素添加了同样的事件,但是又有开发人员想要解除掉元素身上的某个事件效果,这时候操作起来就有些麻烦,需要用到jquery给我们提供的事件命 ...

  10. 【Uva 1543】Telescope

    [Link]: [Description] 给你一个圆和圆周上的n(3≤n≤40)个不同点.请选择其中的m(3≤m≤n)个,按照在圆 周上的顺序连成一个m边形,使得它的面积最大. [Solution] ...