HDU 5379——Mahjong tree——————【搜索】
Mahjong tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 768 Accepted Submission(s): 241
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.

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn=1e6+200;
vector<int>G[maxn];
const int MOD=1e9+7;
typedef long long INT;
#pragma comment(linker,"/STACK:1024000000,1024000000") //加上扩栈,同时语言选C++,不然爆栈
INT fact(int x){
if(x==0)
return 1;
INT ret=1;
for(int i=2;i<=x;i++){
ret=((ret%MOD)*(i%MOD))%MOD;
}
return ret;
}
INT dfs(int u,int fa){ int lef=0,ulef=0; //子节点是叶子的个数,子节点不是叶子的个数
int v;
INT tmp=0 , ret=1; //ret保存方案数
for(int i=0;i<G[u].size();i++){
v=G[u][i];
if(v==fa)
continue;
if(G[v].size()==1){
lef++;
}else{
ulef++;
tmp=dfs(v,u);
if(tmp==-1)
return -1;
ret=((ret%MOD)*(tmp%MOD))%MOD;
}
}
if(ulef<2){ //如果非叶子子节点个数小于2,叶子节点全排列,树根可以为最大值或最小值(之所以乘2)
return ((2*ret%MOD)*(fact(lef)%MOD))%MOD;
}else if(ulef==2){ //如果非叶子子节点等于2,只能让叶子节点全排列
return ((ret%MOD)*(fact(lef)%MOD))%MOD;
}else return -1; //不合法
}
int main(){
int t,n,u,v,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(n==1){
printf("Case #%d: 1\n",++cnt);
continue;
}
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
G[v].push_back(u);
G[u].push_back(v);
}
INT ans=dfs(1,-1);
if(ans==-1)
ans=0;
printf("Case #%d: %lld\n",++cnt,ans);
for(int i=0;i<=n;i++)
G[i].clear();
}
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(树的遍历&组合数学)
本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...
- 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
题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...
- HDU 5379 Mahjong tree dfs+组合数学
题意:给你一棵树来分配号码,要求是兄弟节点连续并且每一棵子树连续. 思路:因为要求兄弟和子树都是连续的,所以自己打下草稿就可以发现如果一个节点有3个或3个以上的非叶子结点,那么就无论如何也不能达到目的 ...
- HDU - 4431 Mahjong (模拟+搜索+哈希+中途相遇)
题目链接 基本思路:最理想的方法是预处理处所有胡牌的状态的哈希值,然后对于每组输入,枚举每种新加入的牌,然后用哈希检验是否满足胡牌的条件.然而不幸的是,由于胡牌的状态数过多(4个眼+一对将),预处理的 ...
- Mahjong tree (hdu 5379 dfs)
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
随机推荐
- linux 进程间通信机制(IPC机制)一消息队列
消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法.每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问题.但是消息 ...
- Owin password
一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...
- spring分布式事务学习笔记(2)
此文已由作者夏昀授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Model类如下:package com.xy.model 1 package com.xy.model; ...
- Python中dataframe\ array\ list相互转化
import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...
- Codeforces Global Round 1D(DP,思维)
#include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){ ...
- bootstrap学习(四)输入框、导航
输入框组: 基本用法: //form-control 占满 //input-group:输入框组//input-group-addon:输入框前加入一个前缀 <div class="i ...
- Nginx——1.基础知识
Nginx——1.基础知识 作为高速.轻量.高性能等优点集于一身的服务器,Nginx在近些年迅速发展并不断扩大市场份额,甚至在最近其市场份额一举超过微软的IIS,跃身到第二位,仅次于Apache. 但 ...
- 关于jquery.extend()的坑:我的数组变成相同元素了?
首先呢我有一个数组,存放了多个json对象.这些json对象的属性有缺失,我设置了一个对象模板来存放默认值 先来看一段代码 var source = [ { name: 'dapianzi', bor ...
- 给label添加点击事件
后台代码: lb1.Attributes.Add("onclick", "getSN('" + lb1.Text.Trim() + "')" ...
- Chrome 67 以后版本无法离线安装crx插件
原文链接:https://blog.csdn.net/wanwuguicang/article/details/80716178 升级了Chrome后无法离线安装扩展 如图: 谷歌自Chrome 67 ...