D. Tree Jumps
题目链接👈
题目描述🥰

题目思路😀
由于这个题目思路比较简单,所以就简单讲下,详细可以看代码理解
今天的题实际上就是一个典型的bfs序问题
根据题目上的定义,我们把1(即是根节点)的层数为1,每一层的节点除了不能接在父母的节点上面,都可以接在上一层的每一个节点的序列的后面,所以这个就有点bfs的思想,我们就从第一层开始推即可,一直推到最大层数即可输出答案。
AC代码🧠
// Problem: D. Tree Jumps
// Contest: Codeforces - Educational Codeforces Round 175 (Rated for Div. 2)
// URL: https://codeforces.com/contest/2070/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define dev1(a) cout << #a << '=' << a << endl;
#define dev2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
#define dev3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl;
#define dev4(a, b, c, d) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << endl;
#define dev5(a, b, c, d, e) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << " " << #e << " = " << e << endl;
#define vec(a) \
for (int i = 0; i < a.size(); i++) \
cout << a[i] << ' '; \
cout << endl;
#define darr(a, _i, _n) \
cout << #a << ':'; \
for (int ij = _i; ij <= _n; ij++) \
cout << a[ij] << ' '; \
cout << endl;
#define cin(a,n) \
for(int i=0;i<n;i++) \
cin>>a[i];
#define endl "\n"
#define pow pim
int pim(int a,int k)
{
int res=1;
if(a==0)return 0;
while(k)
{
if(k&1)res=(int)res*a;
k>>=1;
a=(int)a*a;
}
return res;
}
#define fi first
#define se second
#define caseT \
int T; \
cin >> T; \
while (T--)
#define int long long
// #define int __int128
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int N = 3e5+10;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int dep[N];
void solve()
{
int n;
cin>>n;
int mx=0;
dep[1]=1;
map<int,int>hash;
for(int i=2;i<=n;i++){
int x;
cin>>x;
dep[i]=dep[x]+1;
hash[dep[i]]++;
mx=max(mx,dep[i]);
}
int ans=1;
int pre=1;
for(int i=2;i<=mx;i++)
{
ans+=(pre*hash[i]);
pre*=(hash[i]-1);
pre%=MOD;
ans%=MOD;
}
cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
caseT
solve();
return 0;
}
/*
*/

posted on
浙公网安备 33010602011771号