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;
    }
  • 相关阅读:
    python基础之包、模块、命名空间和作用域
    python基础之函数式编程
    python基础之文件操作
    python基础之psutil模块和发邮件(smtplib和yagmail)
    【面试题21】包含min函数的栈
    【面试题20】顺时针打印矩阵
    【面试题19】二叉树的镜像
    【面试题18】树的子结构
    【面试题17】合并两个排序的链表
    【面试题16】反转链表
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4722593.html
Copyright © 2011-2022 走看看