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 & ...
随机推荐
- Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)
题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...
- ccna 闫辉单臂路由 和 acl access control list
ccna 闫辉单臂路由 和 acl access control list 一单臂路由 当前园区网设计很少用到 成本低 小型的.局域网可用 二ACL acc ...
- [整理]菜鸟教程:docker使用笔记
- docker # 查看 docker stats 指令的具体使用方法 - docker stats --help # 运行一个web应用 - docker pull training/webapp ...
- javascript学习笔记 - 引用类型 Function
五 Function类型 每个函数都时Function类型的实例.函数也是对象. 声明函数: function func_name () {} //javascript解析器会在程序执行时率先读取函数 ...
- JAVA接口与抽象类区别
接口 1.接口可以继承多个接口,extends 接口1,接口2,接口3 2.接口的成员变量默认是public static abstract,必须初始化的: 3.接口只能有抽象方法,继承接口的类必须实 ...
- 【基础操作】2-sat
$2-sat$ 是一个很不怎么考的内容($NOI2017$ 除外) 例题
- javascript进阶一
一 window对象 http://www.w3school.com.cn/jsref/dom_obj_window.asp 二 setInterval的应用 模拟计时器 <!DOCTYPE h ...
- app_data中的数据库使用
原文发布时间为:2008-07-24 -- 来源于本人的百度文章 [由搬家工具导入] ASP.NET中利用VWD操作数据库 建立本地数据库 你可以轻易地在Visual Studio的Web应用程序 ...
- StarUML 系列,静态图与动态图,用例图,类图【ps:熟悉一下starUML】
大部分: 静态图部分,即静态不动的图 1.用例图, Use case diagram 1.展示系统核心功能及与其交互的用户ACTOR 表示:椭圆
- vue slot 插槽备忘
老是记不住插槽咋回事 记录下来备忘 父组件 <tab><template slot="boy" slot-scope="test">{{ ...