pid=5379">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 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

/**
hdu5379||2015多校联合第7场1011 树形统计
题目大意:给定一棵树n个节点,用1~n这n个数给每个节点染色,兄弟节点染色值要连续。以当前节点为根节点的子树中全部节点的染色值要连续。问总共同拥有
多少种染色的方案
解题思路:对于一个节点u:
1.假设他的兄弟节点中sum>=2的节点假设有两以上,则整棵树无解。输出0。
2.假设正好有2个那么二者能够分别在最左或最右,剩下的sum==1的节点全排列那么答案:2*A[son[fa[u]-2],假设fa[u]无兄弟节点那么fa[u]将在
以其为节点的子树中能够在最左或最右边,那么答案还要乘2,为:2*2*A[son[fa[u]-2]*dfs(son);
3.假设有1个,那么他自己可在两头答案:2*A[son[fa[u]-1]*dfs(son);。相同要考虑fa[u]无兄弟节点的情况
4.假设有0个,那么A[son[fa[u]]],相同要考虑fa[u]无兄弟节点的情况
值得一提的是:对于sum为1的节点的个数也要考虑,详细见代码凝视
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long 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];
///儿子的数目。根节点为i的子树的节点树。子树节点数大于2的儿子个数,父亲节点 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)///对树的预处理,求出各种參数
{
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)///子树节点数大于1的儿子个数为1
{
if(son[u]==1)///无兄弟节点此时在最左边和最右边属于同一种情况,无须乘2,下同
{
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)///子树节点数大于1的儿子个数为2
{
cnt=(cnt*A[son[u]-2]%mod*2)%mod;
if(son[fa[u]]==1)cnt=(cnt*2)%mod;
}
else if(bigson[u]>2)///子树节点数大于1的儿子个数大于2
{
return 0;
}
else///无子树节点数大于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)///递归进入儿子节点
{
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;
}

hdu5379||2015多校联合第7场1011 树形统计的更多相关文章

  1. 2015多校联合训练第一场Tricks Device(hdu5294)

    题意:给一个无向图,给起点s,终点t,求最少拆掉几条边使得s到不了t,最多拆几条边使得s能到t 思路: 先跑一边最短路,记录最短路中最短的边数.总边数-最短边数就是第二个答案 第一个答案就是在最短路里 ...

  2. 2015 多校赛 第七场 1011 (hdu 5379)

    题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子 ...

  3. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  4. hdu5294||2015多校联合第一场1007 最短路+最大流

    http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...

  5. HDU OJ 5317 RGCDQ( 2015多校联合训练第3场) 暴力打表+小技巧

    题目连接:Click here 题意:在一个[L,R]内找到最大的gcd(f[i],f[j])其中L<=i<j<=R,f[x]表示i分解质因数后因子的种类数.eg:f[10]=2(1 ...

  6. HDU OJ 5326 Work( 2015多校联合训练第3场) 并查集

    题目连接:戳ME #include <iostream> #include <cstdio> #include <cstring> using namespace ...

  7. hdu5289 2015多校联合第一场1002 Assignment

    题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...

  8. 2015年多校联合训练第一场OO’s Sequence(hdu5288)

    题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个相应的j使得a[i]%a[j]=0.那么l,r内有多少个i,f(l,r)就是几. 问全部f(l,r)的 ...

  9. HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)

    pid=5358">HDU 5358 题意: 求∑​i=1​n​​∑​j=i​n​​(⌊log​2​​S(i,j)⌋+1)∗(i+j). 思路: S(i,j) < 10^10 & ...

随机推荐

  1. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

  2. 配置hibernate常见问题

    连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time z ...

  3. RF操作滚动条(竖拉)

    方式一:window.scrollBy(0, document.body.scrollHeight) 方式二:window.scrollTo(0, document.body.scrollHeight ...

  4. 大数据学习——scala数组

    package com import scala.collection.mutable.ArrayBuffer /** * Created by Administrator on 2019/4/8. ...

  5. Nginx从入门到放弃-第5章 Nginx架构篇

    5-1 Nginx常见问题_架构篇介绍 5-2 Nginx常见问题_多个server中虚拟主机读取的优先级 5-3 Nginx常见问题_多个location匹配的优先级1 5-4 Nginx常见问题_ ...

  6. 51nod1832 先序遍历与后序遍历

    对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树.两棵二叉树不同当且仅当对于某个x,x的左儿子编号不同或x的右儿子编号不同. Input 第一行一个正整数n(3<=n< ...

  7. 面向对象编程(二)封装--构造方法,this关键字,static关键字,方法重载

    面向对象三大特点:封装.继承.多态 封装概念 ①   将东西包装在一起,然后以新的完整形式呈现出来: 将方法和字段一起包装到一个单元中,单元以类的形式实现; ②   信息隐藏,隐藏对象的实现细节,不让 ...

  8. oracle主键自增长

    这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下: create table simon_example ( id number(4) not null primary key, nam ...

  9. ACM程序设计选修课——1018: Common Subsequence(DP)

    问题 L: Common Subsequence 时间限制: 1 Sec  内存限制: 32 MB 提交: 70  解决: 40 [提交][状态][讨论版] 题目描述 A subsequence of ...

  10. mysql 游标的使用总结

    一.游标的基本概念 游标:游标是一个存储在Mysql服务器上的数据库查询,它不是一条select语句,而是被该语句检索出来的结果集. 本人,学习游标中,曾遇到一个问题,循环总是最后多执行一次.下面分析 ...