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>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 100005;
const int mod = 1e9 + 7;

int N, son[maxn], tson[maxn], out[maxn], fac[maxn];
vector<int> G[maxn];

int dfs (int u, int f) {
	son[u] = tson[u] = 0;
	ll ret = out[u] = 1;

	//printf("%d %I64d\n", u, ret);
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (v == f)
			continue;
		ret = ret * dfs(v, u) % mod;

		son[u]++;
		out[u] += out[v];
		if (out[v] > 1)
			tson[u]++;
	}

	ret = ret * fac[son[u] - tson[u]] % mod;

	if (tson[u])
		ret = ret * 2 % mod;

	//printf("%d:%d %d %I64d\n", u, son[u], tson[u], ret);
	if (tson[u] > 2)
		ret = 0;
	return ret;
}

void init () {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++)
		G[i].clear();
	/*
	memset(son, 0, sizeof(son));
	memset(out, 0, sizeof(out));
	memset(tson, 0, sizeof(tson));
	*/

	int u, v;
	for (int i = 1; i < N; i++) {
		scanf("%d%d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
}

int main () {
	fac[0] = fac[1] = 1;
	for (int i = 2; i <= 100000; i++)
		fac[i] = 1LL * i * fac[i-1] % mod;
	int cas;
	scanf("%d", &cas);
	for (int kcas = 1; kcas <= cas; kcas++) {
		init ();
		if (N == 1)
			printf("Case #%d: 1\n", kcas);
		else
			printf("Case #%d: %I64d\n", kcas, 2LL * dfs(1, 0) % mod);
	}
	return 0;
}

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

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

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

hdu 5379 Mahjong tree 树形dp

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

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 (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 求出. 这里的所说的单独考虑每种颜色,指的