Counting of Trees AtCoder - 5633

Problem Statement
Given is an integer sequence D1,…,DN of 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≤105
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

题意;
这道题是真的妙啊,题意秒杀一群人;就是给你一个树上点1到每个点之间距离(最短距离),让你求共有多少个符合要求的树;
思路;
自己a完之后看了网上的代码,怎么会用到快速幂呢(完全用不到啊);只需要统计出每个路的长度出现了几次,然后遍历一遍路的长度,举个栗子,当到1的距离为2的点有2个,那么让出现一个到1距离为3的点时,只需要考虑距离为2的点有几个就好;一次类推连乘;

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
int a[100010];
const int mod=998244353;
map<int,int> mp; 
int main()
{
	int n;
	cin >>n;
	int maxx=-1;
	for(int i=0;i<n;i++)
	{
		cin >>a[i];
		maxx=max(maxx,a[i]);
		mp[a[i]]++;
	}
	 ll sum=1;
	if(a[0]!=0||maxx>n-1) printf("0\n");
	else
	{
		for(int i=1;i<n;i++)
		{
			if(a[i]!=1)
			{
				int y=a[i];
				//while(mp[y--]==0);
				int x=mp[y-1];
				sum=sum%mod*(x%mod)%mod;
			}
		}
	printf("%llu",sum%mod);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值