[nowcoder5668J]Operating on the Tree

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 2005
4 #define mod 998244353
5 struct ji{
6 int nex,to;
7 }edge[N];
8 int E,t,n,x,fac[N],inv[N],head[N],sz[N],g[N][N][3],f[N][N][3];
9 int c(int n,int m){
10 return 1LL*fac[n]*inv[m]%mod*inv[n-m]%mod;
11 }
12 void add(int x,int y){
13 edge[E].nex=head[x];
14 edge[E].to=y;
15 head[x]=E++;
16 }
17 void dfs(int k){
18 sz[k]=g[k][0][0]=g[k][0][2]=1;
19 for(int i=head[k];i!=-1;i=edge[i].nex){
20 int u=edge[i].to;
21 dfs(u);
22 for(int j=1;j<sz[u];j++)
23 for(int p=0;p<3;p++){
24 g[u][j][p]=(g[u][j][p]+g[u][j-1][p])%mod;
25 f[u][j][p]=(f[u][j][p]+f[u][j-1][p])%mod;
26 }
27 for(int j=0;j<sz[k];j++)
28 for(int jj=0;jj<=sz[u];jj++){
29 int C=1LL*c(j+jj,j)*c(sz[k]-j-1+sz[u]-jj,sz[u]-jj)%mod;
30 for(int p1=0;p1<3;p1++)
31 for(int p2=0;p2<3;p2++){
32 int s1=0,s2=0;
33 if (jj){
34 s1=1LL*g[u][jj-1][p2]*g[k][j][p1]%mod*C%mod;
35 s2=(1LL*g[u][jj-1][p2]*f[k][j][p1]+1LL*f[u][jj-1][p2]*g[k][j][p1])%mod*C%mod;
36 }
37 if ((p1==0)&&(p2==1)){
38 g[0][j+jj][0]=(g[0][j+jj][0]+s1)%mod;
39 f[0][j+jj][0]=(f[0][j+jj][0]+s2)%mod;
40 }
41 if ((p1==1)&&(p2<2)||(p1==2)&&(p2==0)){
42 g[0][j+jj][1]=(g[0][j+jj][1]+s1)%mod;
43 f[0][j+jj][1]=(f[0][j+jj][1]+s2)%mod;
44 }
45 if ((p1==2)&&(p2==1)){
46 g[0][j+jj][2]=(g[0][j+jj][2]+s1)%mod;
47 f[0][j+jj][2]=(f[0][j+jj][2]+s2)%mod;
48 }
49 if (!jj)s1=g[u][sz[u]-1][p2];
50 else s1=(g[u][sz[u]-1][p2]-g[u][jj-1][p2]+mod)%mod;
51 if (!jj)s2=f[u][sz[u]-1][p2];
52 else s2=(f[u][sz[u]-1][p2]-f[u][jj-1][p2]+mod)%mod;
53 s2=(1LL*s1*f[k][j][p1]+1LL*s2*g[k][j][p1])%mod*C%mod;
54 s1=1LL*s1*g[k][j][p1]%mod*C%mod;
55 if ((p1==0)&&(p2)){
56 g[0][j+jj][0]=(g[0][j+jj][0]+s1)%mod;
57 f[0][j+jj][0]=(f[0][j+jj][0]+s2)%mod;
58 }
59 if ((p1==1)&&(p2<2)){
60 g[0][j+jj][1]=(g[0][j+jj][1]+s1)%mod;
61 f[0][j+jj][1]=(f[0][j+jj][1]+s2)%mod;
62 }
63 if ((p1==2)&&(p2<2)){
64 g[0][j+jj][2]=(g[0][j+jj][2]+s1)%mod;
65 f[0][j+jj][2]=(f[0][j+jj][2]+s2)%mod;
66 }
67 }
68 }
69 sz[k]+=sz[u];
70 for(int j=0;j<sz[k];j++)
71 for(int p=0;p<3;p++){
72 g[k][j][p]=g[0][j][p];
73 f[k][j][p]=f[0][j][p];
74 g[0][j][p]=f[0][j][p]=0;
75 }
76 }
77 for(int i=0;i<sz[k];i++)f[k][i][0]=(f[k][i][0]+g[k][i][0])%mod;
78 }
79 int main(){
80 fac[0]=inv[0]=inv[1]=1;
81 for(int i=1;i<N-4;i++)fac[i]=1LL*fac[i-1]*i%mod;
82 for(int i=2;i<N-4;i++)inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
83 for(int i=2;i<N-4;i++)inv[i]=1LL*inv[i-1]*inv[i]%mod;
84 scanf("%d",&t);
85 while (t--){
86 scanf("%d",&n);
87 E=0;
88 memset(head,-1,4*(n+1));
89 for(int i=1;i<=n;i++){
90 memset(g[i],0,sizeof(g[i]));
91 memset(f[i],0,sizeof(f[i]));
92 }
93 for(int i=2;i<=n;i++){
94 scanf("%d",&x);
95 add(x+1,i);
96 }
97 dfs(1);
98 int ans=0;
99 for(int i=0;i<n;i++)ans=(ans+0LL+f[1][i][0]+f[1][i][1])%mod;
100 printf("%d\n",ans);
101 }
102 }
[nowcoder5668J]Operating on the Tree的更多相关文章
- Linux and the Device Tree
来之\kernel\Documentation\devicetree\usage-model.txt Linux and the Device Tree ----------------------- ...
- POJ 2420:A Star not a Tree?
原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS Memory Limi ...
- Device Tree Usage( DTS文件语法)
http://elinux.org/Device_Tree_Usage Device Tree Usage Top Device Tree page This page walks throu ...
- POJ 2420 A Star not a Tree? 爬山算法
B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
- Device Tree Usage(理解DTS文件语法)
Basic Data Format The device tree is a simple tree structure of nodes and properties. Properties are ...
- How to Make a Computer Operating System
How to Make a Computer Operating System 如何制作一个操作系统(翻译版) 原文地址:Github:How to Make a Computer Operating ...
- Spanning Tree Protocol (STP) in NetScaler Appliance
Spanning Tree Protocol (STP) in NetScaler Appliance 来源 https://support.citrix.com/article/CTX112341 ...
随机推荐
- 从零入门 Serverless | 企业级 CI/CD 工具部署 Serverless 应用的落地实践
背景知识 通过以往几节课程的学习,相信大家对于 SAE 平台已经有了一定的了解.SAE 为客户免除了很多复杂的运维工作,开箱即用.按用量付费:与此同时 SAE 提供了丰富的 Open API,可以很容 ...
- 【MySQL】MySQL(三)存储过程和函数、触发器、事务
MySQL存储过程和函数 存储过程和函数的概念 存储过程和函数是 事先经过编译并存储在数据库中的一段 SQL 语句的集合 存储过程和函数的好处 存储过程和函数可以重复使用,减轻开发人员的工作量.类似于 ...
- noj加1乘2平方
广度优先搜索典例 00 题目 描述: 最简单的队列的使用#include <iostream>#include <queue>using namespace std;queue ...
- SingleR如何使用自定义的参考集
在我之前的帖子单细胞分析实录(7): 差异表达分析/细胞类型注释里面,我已经介绍了如何使用SingleR给单细胞数据做注释,当时只讲了SingleR配套的参考集.这次就讲讲如何使用自己定义/找到的基因 ...
- 指标统计:基于流计算 Oceanus(Flink) 实现实时 UVPV 统计
作者:吴云涛,腾讯 CSIG 高级工程师导语 | 最近梳理了一下如何用 Flink 来实现实时的 UV.PV 指标的统计,并和公司内微视部门的同事交流.然后针对该场景做了简化,并发现使用 Flink ...
- 【UE4 设计模式】工厂方法模式 Factory Method Pattern 及自定义创建资源
概述 描述 又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式,或者多态工厂(Polymorphic Factory)模式 工厂父类负责定义创建产品对象的公共接口,而工厂子类 ...
- Linux argc,argv详解
来源:微信公众号「编程学习基地」 @ 目录 argc,argv是什么 如何解析程序参数 "选项"是什么? "选项字符串"是什么 解析参数 argc,argv是什 ...
- Noip模拟77 2021.10.15
T1 最大或 $T1$因为没有开$1ll$右移给炸掉了,调了一年不知道为啥,最后实在不懂了 换成$pow$就过掉了,但是考场上这题耽误了太多时间,后面的题也就没办法好好打了.... 以后一定要注意右移 ...
- linux 内核源代码情景分析——i386 的页式内存管理机制
可以看出,在页面目录中共有210 = 1024个目录项,每个目录项指向一个页面表,而在每个页面表中又共有1024个页面描述项. 由图看出来,从线性地址到物理地址的映射过程为: 1)从CR3取得页面目录 ...
- MySql数据库索引-聚集索引和辅助索引
InnoDB存储引擎索引: B+树索引:不能找到一个给定键值的具体行,能找到的只是被查找数据行所在的页.然后把页加载到内存,在查询所要的数据. 全文索引: 哈希索引:InnoDB会根据表的使用情况自动 ...