HDU 5379 Mahjong tree(树的遍历&组合数学)
本文纯属原创,转载请注明出处。谢谢。
http://blog.csdn.net/zip_fan
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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.
For each test case, output one line. The output format is "Case #x: ans"(without quotes), x is the case number, starting from 1.
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
要保证1、兄弟节点之间号是连续的,2、每棵子树号自身是连续的,求一共同拥有多少种可能。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#define moo 1000000007//10^9+7
#define PI acos(-1.0)
#define eps 1e-5
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
long long jie[100000+100];//存阶乘。即全排列情况
vector<int>tre[100000+100];//存点i能到的点,相当于邻接表建图。
int vis[100000+100];//存第i个点的訪问情况
void init()
{
jie[0]=1;
for(int i=1;i<=100000;i++)
{
jie[i]=jie[i-1]*i;
jie[i]%=moo;
}
}//n个无子节点的兄弟节点能够有n!种排列
void dfs1(int num)//先遍历一遍树,将多余的边删除
{
vis[num]=1;
int nn=tre[num].size();
int now=0;
while(now<nn)
{
if(vis[tre[num][now]]==1)
{
tre[num].erase(tre[num].begin()+now);
nn--;
continue;
}
dfs1(tre[num][now]);
now++;
}
}
long long dfs(int num,int has)//统计num节点的情况,has代表num是否有兄弟节点
{
if(tre[num].size()==0)//假设num没有子节点,直接return 1。表示仅仅有1种排列情况
return 1;
long long ans=1;
int num1=0;//存num点的子节点中无子节点的节点个数
int num2=0;//存num点的子节点中有子节点的节点个数
int nn=tre[num].size();
for(int i=0;i<nn;i++)
{
if(tre[tre[num][i]].size()==0)
num1++;
else
num2++;
}
if(num2>2)
return 0;//假设有超过2个点有子节点。那么一定无法满足条件。return 0
if(has==0)//假设num点没有兄弟节点,那么num点能够在子节点的最左或者最右,即2种选择
ans*=2;
ans*=jie[num1];//num的子节点中无子节点的节点能够组成全排列
ans%=moo;
if(num2==0)//假设没有有子节点的节点。那么该点统计完成
return ans;
if(tre[num].size()!=1)//假设不止有一个有子节点的子节点。那么该子节点能够在最左或最右
ans*=2;
int go;
if(tre[num].size()==1)//推断其子节点是否有兄弟节点,为其子节点的统计做准备
go=0;
else
go=1;
for(int i=0;i<nn;i++)
{
if(tre[tre[num][i]].size()!=0)
{
long long la=ans;
ans*=dfs(tre[num][i],go);//假设该点有子节点,继续统计
ans%=moo;
}
}
return ans%moo;
}
int main()
{
int T;
cin>>T;
int dd=T;
init();
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
tre[i].clear();
memset(vis,0,sizeof(vis));
for(int i=1;i<=n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
tre[y].push_back(x);
tre[x].push_back(y);
}
dfs1(1);
printf("Case #%d: %I64d\n",dd-T,dfs(1,0));
}
return 0;
}
HDU 5379 Mahjong tree(树的遍历&组合数学)的更多相关文章
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- HDU 5379 Mahjong tree(dfs)
题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...
- HDU 5379——Mahjong tree——————【搜索】
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- 2015多校第7场 HDU 5379 Mahjong tree 构造,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...
- HDU 5379 Mahjong tree dfs+组合数学
题意:给你一棵树来分配号码,要求是兄弟节点连续并且每一棵子树连续. 思路:因为要求兄弟和子树都是连续的,所以自己打下草稿就可以发现如果一个节点有3个或3个以上的非叶子结点,那么就无论如何也不能达到目的 ...
- HDU 5379 Mahjong tree
题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (J ...
随机推荐
- 《手把手教你学C语言》学习笔记(7)---程序的输入与输出
程序设计中,为了观察程序的运行状态和结构,需要输出指定的内容:为了让程序能够更加灵活,可以根据需求输入内容,让计算机处理和运行:所以程序的输入输出就显的尤为重要.主要包括printf和scanf函数. ...
- linux私房菜-读书笔记
第零章:计算机概论 计算机:接受用户输入指令和数据,经过中央处理器的数据和逻辑单元运算处理器处理后,以产生或存储成有用的信息. 计算机硬件的五大单元:输入单元.输出单元.CPU内部控制单元.算术逻辑单 ...
- c# automapper 使用
一.最简单的用法 有两个类User和UserDto 1 public class User 2 { 3 public int Id { get; set; } 4 public string Name ...
- javascript 动态添加城市
匿名函数的使用 createTextnode 创建文本 createElement 创建元素 appendChild 将文本或元素追加 <!DOCTYPE html> < ...
- Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...
- Codeforces 509E(思维)
...
- encodeURI 解码 编码
var uriStr = "http://www.baidu.com?name=张三&num=001 zs"; var uriec = encodeURI(uriStr); ...
- luogu P1592 互质
题目描述 输入两个正整数n和k,求与n互质的第k个正整数. 输入输出格式 输入格式: 仅一行,为两个正整数n(≤10^6)和k(≤10^8). 输出格式: 一个正整数,表示与n互质的第k个正整数. 输 ...
- QQ聊天窗口上的广告与QQ弹窗广告的完美屏蔽去除
涉及的软件 1. QQ (笔者的为v8.2版本) 2. Win7 3. ADSafe(3.13.308.9900正式版) 前言 QQ广告十分讨人厌,除了QQ弹窗的广告,让人十分反感外,最近发现QQ聊 ...
- java加载类的方法1.classloader 2.class.forName()
java加载类的方法1.classloader 2.class.forName() 加载一个类后,是在方法去创建这个类的元信息class对象,在方法区立刻创建.在方法区创建.