hdu5379||2015多校联合第7场1011 树形统计
pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379
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.
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.
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
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 树形统计的更多相关文章
- 2015多校联合训练第一场Tricks Device(hdu5294)
题意:给一个无向图,给起点s,终点t,求最少拆掉几条边使得s到不了t,最多拆几条边使得s能到t 思路: 先跑一边最短路,记录最短路中最短的边数.总边数-最短边数就是第二个答案 第一个答案就是在最短路里 ...
- 2015 多校赛 第七场 1011 (hdu 5379)
题意:给定一棵树,树上有 n 个节点.问有多少种方案,使得在每个节点上依次放置数 1~n 后,每个节点的儿子节点上的数连续(比如 1 为根,有1-2,1-3,1-4,则令2,3,4上的数连续),每个子 ...
- hdu 5288||2015多校联合第一场1001题
pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...
- hdu5294||2015多校联合第一场1007 最短路+最大流
http://acm.hdu.edu.cn/showproblem.php? pid=5294 Problem Description Innocent Wu follows Dumb Zhang i ...
- 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 ...
- HDU OJ 5326 Work( 2015多校联合训练第3场) 并查集
题目连接:戳ME #include <iostream> #include <cstdio> #include <cstring> using namespace ...
- hdu5289 2015多校联合第一场1002 Assignment
题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...
- 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)的 ...
- HDU 5358(2015多校联合训练赛第六场1006) First One (区间合并+常数优化)
pid=5358">HDU 5358 题意: 求∑i=1n∑j=in(⌊log2S(i,j)⌋+1)∗(i+j). 思路: S(i,j) < 10^10 & ...
随机推荐
- Vue简单了解
目录 1. 前端概览 2. 现代前端开发方式 3. MVVM开发核心 4. Vue核心 5. Vue优点 6. Vue难点 7. Vue与非Vue项目结合 8. Vue调试 9. Vue与SEO 今天 ...
- 微信小程序开发 -- 获取当前页面路径
Page.prototype就是this: 你在任何一个Page里面都可以使用route字段和setData()函数: 示例代码: /** * 生命周期函数--监听页面加载 */ onLoad: fu ...
- C++ POST方式访问网页
bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &s ...
- [Offer收割]编程练习赛48
题目1 : 折线中点 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定平面上N个点P1, P2, ... PN,将他们按顺序连起来,形成一条折线. 请你求出这条折线的 ...
- 【java基础 14】锁的粒度:ThreadLocal、volatile、Atomic和Synchronized
导读:题目中提到的几个关键字,分别是解决并发问题中,加锁所使用到的几个关键字,每个关键字代表的锁的粒度 不同,本篇博客,主要是从概念定义上,区分这几个关键字的应用场景.(PS:睡梦中,依稀记得有回面试 ...
- 【bzoj2563】阿狸和桃子的游戏 贪心
题目描述 阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进行的,设节点权值为w(v),边权为c(e).游戏规则是这样的:1. 阿狸和桃子轮流将图中的顶点染色,阿狸会将顶点染成红色,桃子 ...
- ESXi 给虚拟机添加网络串口
之前的有点儿小问题,我再更新下: Notice: 要看配没配对,能不能通,得先把虚拟机开开,在关机状态下,这种telnet方式一直是连不通的. 1. 先将ESXi的SSH开启(不知道不开行不行): 2 ...
- setsockopt等高级使用
参考: setsockopt函数使用http://hi.baidu.com/yelangdefendou/item/74161d0f384abd3c4ac4a316http://blog.csdn.n ...
- eclipse中纯java配置log4j日志
1.新建java项目log4Test 2.新建目录lib,把log4j-1.2.9.jar包放入lib目录 3.右键工程,选择Properties->Java Build Path->Li ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...