hdu5379Mahjong tree

题意:给出一棵树,用[1,n]里的n个不同的数去给结点标号,要求任意结点的儿子标号都是连续的,任意子树里的标号也都是连续的。求这样标号的方法有多少种。

做法:很显然,若一个结点有大于两个的儿子必然无解。

若当前有x个连续的数可以用来分配,那么这个结点要么取最小的那个数,要么取最大的那个数,

当该结点刚好有两个不为叶子的儿子,然后剩下的那x-1个数要么把前面的部分给左边的儿子要么给右边的儿子,后面的部分一样,这就有2种可能。

当刚好有一个不为叶子的儿子,那么剩下的那x-1个数要么把前面的部分给儿子,要么把后面的部分给儿子,这就有2种可能。

若没有上述的儿子,则全是叶子,则就是(x-1)!可能,上述两种情况考虑儿子为叶子的时候同理。

接下来就是很裸的树形dp了。

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
struct Edge
{
    int to,next;
}edge[200010];
int head[100010],tail;
void add(int from,int to)
{
    edge[tail].to=to;
    edge[tail].next=head[from];
    head[from]=tail++;
}
bool vis[100010];
int num[100010];
void seek(int from)
{
    vis[from]=1;
    num[from]=1;
    for(int i=head[from];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(vis[to])
            continue;
        seek(to);
        num[from]+=num[to];
    }
}
ll fac[100010];
ll dfs(int from)
{
    vis[from]=1;
    int sum1=0,sum2=0;
    vector<int>bx;
    for(int i=head[from];i!=-1;i=edge[i].next)
    {
        int to=edge[i].to;
        if(vis[to])
            continue;
        if(num[to]==1)
            sum1++;
        else
        {
            sum2++;
            bx.push_back(to);
        }
    }
    if(sum2>2)
    {
        return 0;
    }
    if(sum2==0)
        return fac[sum1];
    ll sum=1;
    for(int i=0;i<sum2;i++)
    {
        ll t=dfs(bx[i]);
        if(t==0)
            return 0;
        sum=(sum*t)%mod;
    }
    return fac[sum1]*sum*2%mod;
}
int main()
{
    fac[0]=1;
    for(int i=1;i<=100000;i++)
        fac[i]=fac[i-1]*i%mod;
    int T;
    scanf("%d",&T);
    for(int cs=1;cs<=T;cs++)
    {
        int n;
        scanf("%d",&n);
        tail=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        if(n==1)
        {
            printf("Case #%d: 1\n",cs);
            continue;
        }
        memset(vis,0,sizeof(vis));
        seek(1);
        memset(vis,0,sizeof(vis));
        printf("Case #%d: %d\n",cs,int(2*dfs(1)%mod));
    }
    return 0;
}

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 999    Accepted Submission(s): 318

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

Source

2015 Multi-University Training Contest 7

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-31 14:17:44

hdu5379Mahjong tree的相关文章

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

SPOJ375 Query on a tree

https://vjudge.net/problem/SPOJ-QTREE 题意: 一棵树,每条边有个权值 两种操作 一个修改每条边权值 一个询问两点之间这一条链的最大边权 点数<=10000 多组测试数据,case<=20 Example Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 #include<cstdio> #include<iostream> #include&

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in

命令-tree

tree命令 tree - list contents of directories in a tree-like format. 显示目录的层级结构: tree 命令英文理解为树的意思,其功能是创建文件列表,将目录所有文件以树状的形式列出来.linux中的tree命令默认并不会安装,所以需要通过yum install tree -y来安装此命令. [SYNOPSIS] tree [options] [directory] [OPTIONS] -L level:指定要显示的层级: -d:仅列出目

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[hihoCoder#1381]Little Y&#39;s Tree

[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一