Counting of Trees(模拟)-JavierWu

发布于 2019-11-12  9 次阅读


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("0n")
			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("0n")
			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("%lldn", cnt)
	}
	return 0
}
届ける言葉を今は育ててる
最后更新于 2019-11-12