• 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 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

    题目大意:有一颗n个点的树,在这n个点中填上1~n的数,求满足下列条件的最多填法数量

    1、任意节点的所有儿子节点都应填上连续的数。

    2、任意子树里面的所有结点都应填上连续的数。

    思路:定义dp[u]表示以u为根的子树填上连续的数的填法数量。

    对于当前节点u,对于它的儿子节点进行分类讨论:

    1、u的儿子节点中如果有超过两个节点不是叶子节点,那么dp[u]=0

    2、u只有一个儿子节点v,dp[u] = dp[v]*2;

    3、u有两个儿子节点v1,v2,且这两个儿子节点都不是叶子节点,dp[u] = dp[v1]*dp[v2]

    ……大致思路就是这样,剩下的分类情况略去。

    #include <vector>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn = 100005;
    const int mod = 1e9 + 7;
    int n;
    long long dp[maxn];
    bool vis[maxn];
    vector<int> adj[maxn];
    long long solve(int u)
    {
        if(dp[u]!=-1) return dp[u];
        vis[u] = true;
        int x=0,v1=-1,v2=-1;
        dp[u] = 1;
        int len=adj[u].size();
        int son=0;
        for(int i=0; i<len; i++)
        {
            int v = adj[u][i];
            if(vis[v]) continue;
            solve(v);
            dp[u]*=dp[v];
            dp[u]%=mod;
            if(dp[v]==1) x++;
            son++;
        }
        if(son-x>2||dp[u]==0) return dp[u]=0;
        else if(son==1)
        {
            dp[u]*=2;
            return dp[u]%=mod;
        }
        if(x)
        {
            if(son==x||son-x==1) dp[u]*=2;
            dp[u]%=mod;
            while(x)
            {
                dp[u]*=x;
                dp[u]%=mod;
                x--;
            }
        }
        return dp[u]%=mod;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        int kase=1;
        while(T--)
        {
            scanf("%d",&n);
            for (int i=1; i<n; i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                adj[x].push_back(y);
                adj[y].push_back(x);
            }
    
            int root = 1;
            memset(dp,-1,sizeof(dp));
            memset(vis,0,sizeof(vis));
            printf("Case #%d: %I64d
    ",kase++,solve(root));
            for(int i=1; i<=n; i++) adj[i].clear();
            //for(int i=1;i<=n;i++) printf("%d %d
    ",i,dp[i]);
        }
        return 0;
    }
  • 相关阅读:
    模仿jquery的一些实现
    使按钮失效的方法
    类似jquery的一个demo
    oracle 集合变量以及自定义异常的用法
    java的for循环问题的解决,以及安卓中ListView插入数据的问题
    Spring AOP基于xml配置实例
    plsql 的循环之 goto
    Spring AOP报错
    补全aaz288 可能有问题的过程 P_COMPL_AAZ288
    Spring注解配置
  • 原文地址:https://www.cnblogs.com/lastone/p/5351962.html
Copyright © 2020-2023  润新知