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. 用virtualbox+模拟串口+CDT调试linux内核 TCP/IP协议栈-起步

    经常有人问一台机器如何将hello经网络发送给另一台机器,我确实是不知道,只能看代码了. 说明:本人对内核的研究学习也是刚刚起步,有很多不了解的,所以文中可能会有一些"一本正经的胡扯&quo ...

  2. jade和ejs两者的特点

    jade特点 1超强的可读性 2灵活易用的缩进 3块扩展 4代码默认进过编码处理,以增强安全性 5编译及运行时的上下文错误报告 6命令行编译支持 7html5模式(使用 !!!5文档类型) 8可选的内 ...

  3. jQuery实现当按下回车键时绑定点击事件

    jQuery实现当按下回车键时绑定点击事件 <script> $(function(){ $(document).keydown(function(event){ if(event.key ...

  4. classpath路径

    在项目被集成开发环境编译后,src目录下的东西会编译到WEB-INF/classes目录下,而WEB-INF/classes目录就是所谓的classpath. 将数据库连接配置dataSource.x ...

  5. 【Luogu】P3203弹飞绵羊(分块)

    题目链接 正解是LCT但我不会呀蛤蛤蛤蛤蛤 (分块我也没想出来 把区间分成根n个块,每个块内记录两个东西,就是该位置弹多少次能够弹出这个块,以及该位置弹到最后弹出去了之后能够弹到哪里. 然后查询就一个 ...

  6. 【Luogu】P2053修车(费用流)

    题目链接 早上状态不好,虚树搞崩只好来刷网络流了qwq. (然后我犹豫几秒之后看了题解) 使用拆点大法把工人拆成n*m个点,然后每个点代表每个时间段的工人, 然后从车到每个工人点连一条边,权值是耽误的 ...

  7. kb-07线段树--10--dfs序建树

    /* hdu3974 dfs序建树,然后区间修改查询: */ #include<iostream> #include<cstdio> #include<cstring&g ...

  8. php中 ob_start()有什么作用

    <?php ob_start(); //开启缓冲区 echo "这是第一次输出内容!\n"; $ff[1] = ob_get_contents() ; //获取当前缓冲区内容 ...

  9. jQuery全屏滚动插件fullPage.js使用中遇到的问题(滑动轮播效果无效)

    1.fullPage.js是什么? fullPage.js 是一个基于 jQuery ,用来制作全屏网站的插件. 2.兼容性: - jquery兼容:兼容 jQuery 1.7+ - 浏览器兼容: I ...

  10. 刷题总结——coneology(poj2932 扫描线)

    题目: Description A student named Round Square loved to play with cones. He would arrange cones with d ...