hdu 5379 Mahjong tree 树形dp

链接

题意:给定一棵树

把1-n填到树的节点上,使得:

1:儿子节点上填的数字是连续的。

2:子树节点上填的数字是连续的。

把儿子节点分成两种,一种是叶子节点,一种是非叶子节点。

显然非叶子节点个数不能超过2个,不然就不存在这样的方案了。

然后分类讨论一下非叶子节点个数即可。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) pt(x / 10);
    putchar(x % 10 + '0');
}
typedef pair<int, int> pii;
typedef long long ll;
const int N = 100000+10;
const int mod = 1e9 + 7;
struct Edge {
    int to, nex;
}edge[N << 1];
int head[N], edgenum;
void add(int u, int v) {
    Edge E = { v, head[u] };
    edge[edgenum] = E;
    head[u] = edgenum++;
}
int n;
int siz[N], A[N];
int ans;
void mul(int x) {
    ans = (ll)ans*x%mod;
}
void dfs(int u, int fa) {
    siz[u] = 1;
    int num = 0, num2 = 0;
    for (int i = head[u]; ~i; i = edge[i].nex) {
        int v = edge[i].to; if (v == fa)continue;
        dfs(v, u);
        siz[u] += siz[v];
        if (siz[v] == 1)num2++;
        else num++;
    }
    if (siz[u] == 1)return;
    if (num > 2)ans = 0;
    else if (num == 0)
    {
        mul(A[num2]);
    }
    else
    {
        mul(2);
        mul(A[num2]);
    }
}
int main() {
    A[0] = 1;
    for (int i = 1; i < N; i++)A[i] = (ll)A[i - 1] * i%mod;
    int T, Cas = 1; rd(T);
    while (T--) {
        rd(n);
        for (int i = 1; i <= n; i++)head[i] = -1;edgenum = 0;
        for (int i = 1, u, v; i < n; i++) {
            rd(u); rd(v); add(u, v); add(v, u);
        }
        ans = 1;
        dfs(1, 1);
        if (n != 1)mul(2);
        printf("Case #%d: ", Cas++);
        pt(ans); puts("");
    }
    return 0;
}
/*
99
7
1 2
1 5
2 3
2 4
5 6
5 7
10
1 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
4 10

*/

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

时间: 2024-10-10 16:10:20

hdu 5379 Mahjong tree 树形dp的相关文章

hdu 5379 Mahjong tree 树形DP入门

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

HDOJ 5379 Mahjong tree 树形DP

在一棵树上给所有点标号,要求任意一个子树里的点编号连续,每一个点的儿子编号连续. 那么,一个点的非叶子儿子应该是连续的,即一个点的非叶子儿子最多只有两个. 对于每一个点,我们把它的叶子儿子的个数记作S,所有儿子的方案数积为T.当非叶子儿子节点个数小于2的时候,方案数为2T*(S!). 当非叶子儿子节点数等于2的时候,这个点为根的子树合法方案数位T*(S!). 这样dfs一遍即可以处理整棵树的方案数. Mahjong tree Time Limit: 6000/3000 MS (Java/Othe

hdu 5379 Mahjong tree(树形dp)

题目链接:hdu 5379 Mahjong tree 树形dp,每个节点最多有2个子节点为一棵节点数大于1的子树的根节点,而且要么后代的节点值都大于,要么都小于本身(所以tson不为0是,要乘2).对于K个单一节点的子节点,种类数即为全排K!.当一个节点没有兄弟节点时,以这个节点为根结点的子树,根可以选择最大或者最小. #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

Hdu 5379 Mahjong tree (dfs + 组合数)

题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号? 解题思路: 对于一个节点来讲,非叶子儿子节点最多有两个才能满足要求,否则满足子树节点连续的话就无法满足兄弟节点连续.然后有dfs计算每棵子树的贡献值,每棵子树的子节点可以分为叶子节点X和非叶子节点Y,叶子节点可以分配到一组连续的编号,非叶子节点只能分配到兄弟节点中最大或者最小编号两种情况,叶子节

HDU 5379 Mahjong tree(dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5379 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 verte

HDU 5379 Mahjong tree(树的遍历&amp;amp;组合数学)

本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=5379 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Little sun is an artist. Today he is playing

HDU 5379 Mahjong tree (详解,构造+思维)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题面: Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1148    Accepted Submission(s): 351 Problem Description Little sun is an art

hdu 5379 Mahjong tree(构造)

题目: Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1471    Accepted Submission(s): 448 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly

hdu 6035 Colorful Tree(树形dp+技巧)

题目链接:hdu 6035 Colorful Tree 题意: 给你一棵树,每个节点有一种颜色,现在让你求所有点对的路径上不同的颜色数量的总和. 题解: 下面是官方题解: 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可.直接做可以采用虚树的思想(不用真正建出来),对每种颜色的点按照 dfs 序列排个序,就能求出这些点把原来的树划分成的块的大小.这个过程实际上可以直接一次 dfs 求出. 这里的所说的单独考虑每种颜色,指的