Counting of Trees(模拟)

Counting of Trees(模拟)

Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300 points

Problem Statement

Given is an integer sequence
D1,…,DNof N elements. Find the number, modulo 998244353, of trees with N vertices numbered 1 to N that satisfy the following condition:

·For every integer i from 1 to N, the distance between Vertex 1 and Vertex i is Di.

Notes

·A tree of N vertices is a connected undirected graph with
N vertices and N−1 edges, and the distance between two vertices are the number of edges in the shortest path between them.

·Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.

Constraints

1≤N≤100000
0≤Di≤N−1

Input

Input is given from Standard Input in the following format:
N
D1 D2 … DN

Output

Print the answer.

Sample Input 1

4
0 1 1 2

Sample Output 1

2
For example, a tree with edges (1,2), (1,3), and (2,4) satisfies the condition.

Sample Input 2

4
1 1 1 1

Sample Output 2

0

Sample Input 3

7
0 3 2 1 2 2 1

Sample Output 3

24

题意:给定一个N,代表N个数,N个数代表到位置1的距离,利用这个距离画出满足这个条件的树,结果为输出有多少个满足题意的树。根据题意能了解,有多少棵这样的树,只需要画出一种情况,就能通过每一层有多少个数,算出所有的结果。大概思路是,从这棵树的第三层开始,到最后一层,结果为本层数的上层数的次方级相乘即为最终结果。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
long long int a[100005];
int main()
{
	long long int N;
	while (scanf("%lld", &N) != EOF)
	{
		memset(a, 0, sizeof(a));
		for (long long int i = 1; i <= N; i++)
			scanf("%lld", &a[i]);
		if (a[1] != 0)            //首先判断这棵树的第一位到1距离是否为0,不为零则此树不存在
		{
			printf("0\n");
			continue;
		}
		sort(a + 1, a + N + 1);
		long long int flog = 0;
		for (long long int i = 2; i <= N; i++) 
		{
			if (abs(a[i] - a[i - 1]) != 0 && abs(a[i] - a[i - 1]) != 1)   //判断相邻节点距离是否差0或1
			{
				flog = 1;
				break;
			}
		}
		if (flog==1) 
		{
			printf("0\n");
			continue;
		}
		long long int i = 2, cnt = 1, n = 0;
		for (; a[i] == 1; i++)
			n++;
		for (; i <= N;)       
		{
			long long int d = a[i], s = 1, nn = 0;
			for (; a[i] == d && i <= N; i++)    //模拟本层树的上层树的次方数
			{
				nn++;
				s = (s * n) % 998244353;
			}
			cnt = (cnt * s) % 998244353;
			n = nn;
		}
		printf("%lld\n", cnt);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值