zoukankan      html  css  js  c++  java
  • HUD-5379

    Mahjong tree

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 506    Accepted Submission(s): 161


    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
     
     

    3
    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
    1

    Sample Output

    Case #1: 32
    Case #2: 16
    Case #3: 1

    Source
    /**
        题意:给一个树,现在要求一个树的叶子结点之间的数十连续的,并且一个树的所有的
              子数节点的数也是连续的
        做法:搜索
        如果当前节点头n个节点,如果没有非叶子的节点那么它的可能是n!
        否则 如果存在一个非叶子节点  那么就是2*(n-1)!  一段区间的开始和结束
             如果存在两个非叶子节点  那么就是2*(n-2)!
             如果非叶子节点大于2     那么该树没有解
    **/
    #pragma comment(linker, "/STACK:102400000,102400000")
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <string.h>
    #include <stdio.h>
    #include <vector>
    using namespace std;
    #define maxn  100000 + 10
    const int  mod = 1e9 + 7;
    vector<int>G[maxn];
    long long res = 0;
    int vis[maxn];
    bool flag = true;
    void dfs(int u)
    {
        vis[u] = 1;
        int tt = G[u].size();
        int cet = 0;
        for(int i = 0; i < tt; i++)
        {
            int v = G[u][i];
            if(vis[v]) {
                continue;
            }
            if(G[v].size() == 1 && vis[v] == 0) {
                cet++;
            }
            else if(vis[v] == 0) {
                dfs(v);
            }
        }
        if(u != 1) {
            tt--;
        }
        if(tt - cet > 2)
        {
            flag = false;
            return;
        }
        if(tt - cet > 0)
        {
            res *= 2;
            res %= mod;
        }
        while(cet > 0)
        {
            res *= cet;
            res %= mod;
            cet--;
        }
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        int Case = 1;
        while(T--)
        {
            int n;
            scanf("%d", &n);
            int u, v;
            memset(vis, 0, sizeof(vis));
            for(int i = 0; i <= n; i++)
            {
                G[i].clear();
            }
            for(int i = 0; i < n - 1; i++)
            {
                scanf("%d %d", &u, &v);
                G[u].push_back(v);
                G[v].push_back(u);
            }
            res = 1;
            flag = true;
            dfs(1);
            if(n == 1) {
                res = 1;
            }
            else {
                res *= 2;
            }
            if(flag == false) {
                printf("Case #%d: 0
    ", Case++);
            }
            else {
                printf("Case #%d: %lld
    ", Case++, res % mod);
            }
        }
        return 0;
    }
  • 相关阅读:
    2018年强化学习领域十篇重要论文(附源码)[转]
    【转载】 再励学习面试真题 (强化学习面试真题)
    枸杞常泡水喝好吗?
    亲历亚马逊、华为机器学习面试,原来考官想听到这些回答[转]
    阿里面试 深度学习[转]
    强化学习(四)用蒙特卡罗法(MC)求解
    初识kaggle,以及记录 kaggle的使用
    强化学习(三)用动态规划(DP)求解
    CORTEX-M3中断的现场保护问题
    地球是圆的怎么还分东西方
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4722593.html
Copyright © 2011-2022 走看看