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

解题:

可以分析一下当每个节点分别有几个叶子和几个继续向下的孩子时的情况,全部累乘就好。详细可以见代码。

总结:

这题犯了两个致命错误.

1.天真的以为给定的边都是儿子连向父亲的,本来也觉得不妥,题目没说,可是还是天真的写了下去...不可断章取义啊!!

2.分析出若某个点有三个可以继续往下的节点,那么这种情况是不合法的,居然,就不管了,当时真是晕了头了,应该输出0才对啊!!

综上,自己实在是太天真了....

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define MOD 1000000007
using namespace std;
long long ans;
struct node
{
    int id;
	//存真正的孩子
    vector <int> child;
	//存相邻的边
	vector <int> edge;
    int num;
}store[100010];
queue <int> qe;
bool flag;
bool vis[100010];
void bfs(int x)
{
	while(!qe.empty())
		qe.pop();
	//先访问的是父亲
	memset(vis,0,sizeof(vis));
    int sz,yezi,qujian;
    qe.push(x);
    int tmp;
    while(!qe.empty())
    {
		//叶子和区间数
        yezi=qujian=0;
        tmp=qe.front();
        qe.pop();
		//标记为访问过
		vis[tmp]=1;
        sz=store[tmp].num;
		for(int i=0;i<sz;i++)
		{
			//如果没访问过,说明是孩子
			if(!vis[store[tmp].edge[i]])
			{
				vis[store[tmp].edge[i]]=1;
				store[tmp].child.push_back(store[tmp].edge[i]);
			}
		}
		sz=store[tmp].child.size();
        for(int i=0;i<sz;i++)
        {
			//不是叶子节点,是还能往下的节点
			//叶子节点只有其父亲连向它的边,而根节点虽然也是,但不会出现在这里
            if(store[store[tmp].child[i]].num>1)
            {
                qujian++;
                qe.push(store[tmp].child[i]);
            }
            else
            {
                yezi++;
            }
        }
		//开始这里没特判,太理想化了,一个节点有超过三个点还能继续向下,那么答案为0
		if(qujian>=3)
		{
			flag=true;
			return;
		}
		//全是叶子节点,全排列
        if(yezi==sz)
        {
            for(int i=1;i<=sz;i++)
              ans=ans*i%MOD;
        }
		//一个区间,其他全叶子,只能叶子一边,区间一边
		//两边互换,叶子全排列
        else if(yezi==sz-1)
        {
            ans=ans*2%MOD;
            for(int i=1;i<sz;i++)
              ans=ans*i%MOD;
        }
		//两个区间,其他全叶子,只能叶子在中间,区间在两边
		//叶子全排列,左右俩区间互换
        else if(yezi==sz-2)
        {
            ans=ans*2%MOD;
            for(int i=1;i<sz-1;i++)
            ans=ans*i%MOD;
        }
    }
}
int main()
{
    int t,n,fa,son;
    scanf("%d",&t);
    for(int i=1;i<=100000;i++)
    {
        store[i].id=i;
    }
    for(int i=1;i<=t;i++)
    {
		//初始化
        scanf("%d",&n);
        for(int j=1;j<=n;j++)
        {
            store[j].child.clear();
			store[j].edge.clear();
            store[j].num=0;
        }
        for(int j=1;j<n;j++)
        {
            scanf("%d%d",&son,&fa);
			//连双向边,搜索后才能决定谁是父亲,谁是儿子
            store[fa].edge.push_back(son);
			store[son].edge.push_back(fa);
			//连边的数量
            store[fa].num++;
			store[son].num++;
        }
		//一个节点特判
        if(n==1)ans=1;
        else
		{
		  flag=false;
          ans=2;
          bfs(1);
		  if(flag)
			  ans=0;
		}
        printf("Case #%d: %lld\n",i,ans);
    }
    return 0;
}

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

时间: 2024-10-22 15:07:28

HDU 5379 Mahjong tree (详解,构造+思维)的相关文章

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(构造)

题目: 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 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 树形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

2015多校第7场 HDU 5379 Mahjong tree 构造,DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的标号要是连续的(2)每一颗子树的所有节点标号是连续的.问有多少种标号方案. 解法:对于每一层顶多只能存在2个非叶子节点,否则无解:对于每一层有x个叶子节点,y个非叶子节点,那么ans=(ans * x!)%mod,另外如果y!=0,还得ans=2*ans%mod. #include <bits/st

hdu 5379 Mahjong tree 树形dp

链接 题意:给定一棵树 把1-n填到树的节点上,使得: 1:儿子节点上填的数字是连续的. 2:子树节点上填的数字是连续的. 把儿子节点分成两种,一种是叶子节点,一种是非叶子节点. 显然非叶子节点个数不能超过2个,不然就不存在这样的方案了. 然后分类讨论一下非叶子节点个数即可. #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #i

HDU 5379 Mahjong tree

题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有一个儿子的时候,如果儿子是叶子节点则只有一种放法,如果儿子不是叶子节点则有两种放法. 当一个节点有两个儿子的时候,一定有两种放法. 当一个儿子有三个儿子及以上的时候,假设有k个儿子,如果非叶子节点m的个数为0则有k!种放法,如果m为1,则有2 × (k - 1)!种放法,如果m为2,则有2 × (k