考虑令$a_{i}$为i的位置,$p_{i}=0/1$表示第i个点的贡献,那么$p_{x}=0$当且仅当存在与其相邻的点$y$满足$a_{y}<a_{x}$且$p_{y}=1$
树形dp,定义状态$g[k][j][0/1/2]$表示以$k$为根的子树中选择了j个点,$p_{k}=1$或$p_{k}=0$或还没有参与,$f[k][j][0/1/2]$表示这样的$cost$之和
暴力转移即可(转移方程详见代码),注意:1.要乘上组合数表示不同子树之间的顺序;2.要另开一个数组来讨论;3.讨论时要对儿子和根的位置关系分类

  1. 1 #include<bits/stdc++.h>
  2. 2 using namespace std;
  3. 3 #define N 2005
  4. 4 #define mod 998244353
  5. 5 struct ji{
  6. 6 int nex,to;
  7. 7 }edge[N];
  8. 8 int E,t,n,x,fac[N],inv[N],head[N],sz[N],g[N][N][3],f[N][N][3];
  9. 9 int c(int n,int m){
  10. 10 return 1LL*fac[n]*inv[m]%mod*inv[n-m]%mod;
  11. 11 }
  12. 12 void add(int x,int y){
  13. 13 edge[E].nex=head[x];
  14. 14 edge[E].to=y;
  15. 15 head[x]=E++;
  16. 16 }
  17. 17 void dfs(int k){
  18. 18 sz[k]=g[k][0][0]=g[k][0][2]=1;
  19. 19 for(int i=head[k];i!=-1;i=edge[i].nex){
  20. 20 int u=edge[i].to;
  21. 21 dfs(u);
  22. 22 for(int j=1;j<sz[u];j++)
  23. 23 for(int p=0;p<3;p++){
  24. 24 g[u][j][p]=(g[u][j][p]+g[u][j-1][p])%mod;
  25. 25 f[u][j][p]=(f[u][j][p]+f[u][j-1][p])%mod;
  26. 26 }
  27. 27 for(int j=0;j<sz[k];j++)
  28. 28 for(int jj=0;jj<=sz[u];jj++){
  29. 29 int C=1LL*c(j+jj,j)*c(sz[k]-j-1+sz[u]-jj,sz[u]-jj)%mod;
  30. 30 for(int p1=0;p1<3;p1++)
  31. 31 for(int p2=0;p2<3;p2++){
  32. 32 int s1=0,s2=0;
  33. 33 if (jj){
  34. 34 s1=1LL*g[u][jj-1][p2]*g[k][j][p1]%mod*C%mod;
  35. 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. 36 }
  37. 37 if ((p1==0)&&(p2==1)){
  38. 38 g[0][j+jj][0]=(g[0][j+jj][0]+s1)%mod;
  39. 39 f[0][j+jj][0]=(f[0][j+jj][0]+s2)%mod;
  40. 40 }
  41. 41 if ((p1==1)&&(p2<2)||(p1==2)&&(p2==0)){
  42. 42 g[0][j+jj][1]=(g[0][j+jj][1]+s1)%mod;
  43. 43 f[0][j+jj][1]=(f[0][j+jj][1]+s2)%mod;
  44. 44 }
  45. 45 if ((p1==2)&&(p2==1)){
  46. 46 g[0][j+jj][2]=(g[0][j+jj][2]+s1)%mod;
  47. 47 f[0][j+jj][2]=(f[0][j+jj][2]+s2)%mod;
  48. 48 }
  49. 49 if (!jj)s1=g[u][sz[u]-1][p2];
  50. 50 else s1=(g[u][sz[u]-1][p2]-g[u][jj-1][p2]+mod)%mod;
  51. 51 if (!jj)s2=f[u][sz[u]-1][p2];
  52. 52 else s2=(f[u][sz[u]-1][p2]-f[u][jj-1][p2]+mod)%mod;
  53. 53 s2=(1LL*s1*f[k][j][p1]+1LL*s2*g[k][j][p1])%mod*C%mod;
  54. 54 s1=1LL*s1*g[k][j][p1]%mod*C%mod;
  55. 55 if ((p1==0)&&(p2)){
  56. 56 g[0][j+jj][0]=(g[0][j+jj][0]+s1)%mod;
  57. 57 f[0][j+jj][0]=(f[0][j+jj][0]+s2)%mod;
  58. 58 }
  59. 59 if ((p1==1)&&(p2<2)){
  60. 60 g[0][j+jj][1]=(g[0][j+jj][1]+s1)%mod;
  61. 61 f[0][j+jj][1]=(f[0][j+jj][1]+s2)%mod;
  62. 62 }
  63. 63 if ((p1==2)&&(p2<2)){
  64. 64 g[0][j+jj][2]=(g[0][j+jj][2]+s1)%mod;
  65. 65 f[0][j+jj][2]=(f[0][j+jj][2]+s2)%mod;
  66. 66 }
  67. 67 }
  68. 68 }
  69. 69 sz[k]+=sz[u];
  70. 70 for(int j=0;j<sz[k];j++)
  71. 71 for(int p=0;p<3;p++){
  72. 72 g[k][j][p]=g[0][j][p];
  73. 73 f[k][j][p]=f[0][j][p];
  74. 74 g[0][j][p]=f[0][j][p]=0;
  75. 75 }
  76. 76 }
  77. 77 for(int i=0;i<sz[k];i++)f[k][i][0]=(f[k][i][0]+g[k][i][0])%mod;
  78. 78 }
  79. 79 int main(){
  80. 80 fac[0]=inv[0]=inv[1]=1;
  81. 81 for(int i=1;i<N-4;i++)fac[i]=1LL*fac[i-1]*i%mod;
  82. 82 for(int i=2;i<N-4;i++)inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
  83. 83 for(int i=2;i<N-4;i++)inv[i]=1LL*inv[i-1]*inv[i]%mod;
  84. 84 scanf("%d",&t);
  85. 85 while (t--){
  86. 86 scanf("%d",&n);
  87. 87 E=0;
  88. 88 memset(head,-1,4*(n+1));
  89. 89 for(int i=1;i<=n;i++){
  90. 90 memset(g[i],0,sizeof(g[i]));
  91. 91 memset(f[i],0,sizeof(f[i]));
  92. 92 }
  93. 93 for(int i=2;i<=n;i++){
  94. 94 scanf("%d",&x);
  95. 95 add(x+1,i);
  96. 96 }
  97. 97 dfs(1);
  98. 98 int ans=0;
  99. 99 for(int i=0;i<n;i++)ans=(ans+0LL+f[1][i][0]+f[1][i][1])%mod;
  100. 100 printf("%d\n",ans);
  101. 101 }
  102. 102 }

[nowcoder5668J]Operating on the Tree的更多相关文章

  1. Linux and the Device Tree

    来之\kernel\Documentation\devicetree\usage-model.txt Linux and the Device Tree ----------------------- ...

  2. POJ 2420:A Star not a Tree?

    原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS   Memory Limi ...

  3. Device Tree Usage( DTS文件语法)

    http://elinux.org/Device_Tree_Usage Device Tree Usage     Top Device Tree page This page walks throu ...

  4. 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 ...

  5. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  6. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

  7. Device Tree Usage(理解DTS文件语法)

    Basic Data Format The device tree is a simple tree structure of nodes and properties. Properties are ...

  8. How to Make a Computer Operating System

    How to Make a Computer Operating System 如何制作一个操作系统(翻译版) 原文地址:Github:How to Make a Computer Operating ...

  9. Spanning Tree Protocol (STP) in NetScaler Appliance

    Spanning Tree Protocol (STP) in NetScaler Appliance 来源 https://support.citrix.com/article/CTX112341 ...

随机推荐

  1. Ysoserial Commons Collections3分析

    Ysoserial Commons Collections3分析 写在前面 CommonsCollections Gadget Chains CommonsCollection Version JDK ...

  2. 题解 CF762D Maximum path

    题目传送门 Description 给出一个 \(3\times n\) 的带权矩阵,选出一个 \((1,1)\to (3,n)\) 的路径使得路径上点权之和最大. \(n\le 10^5\) Sol ...

  3. NOI2016区间bzoj4653(线段树,尺取法,区间离散化)

    题目描述 在数轴上有 \(N\) 个闭区间 \([l_1,r_1],[l_2,r_2],...,[l_n,r_n]\) .现在要从中选出 \(M\) 个区间,使得这 \(M\) 个区间共同包含至少一个 ...

  4. selenium 4.0 发布

    我们非常高兴地宣布Selenium 4的发布.这适用于Java..net.Python.Ruby和Javascript.你可以从你最喜欢的包管理器或GitHub下载它! https://github. ...

  5. 使用CSS选择器(第一部分)

    目录 使用CSS选择器(第一部分) 使用CSS基本选择器 选择所有元素 通用选择器 代码清单1 使用通用选择器 根据类型选择元素 元素类型选择器 代码清单2 使用元素类型选择器 提示 根据类选择元素 ...

  6. Java---String和StringBuffer类

    Java---String和StringBuffer类 Java String 类 字符串在Java中属于对象,Java提供String类来创建和操作字符串. 创建字符串 创建字符串常用的方法如下: ...

  7. 【UE4】GAMES101 图形学作业1:mvp 模型、视图、投影变换

    总览 到目前为止,我们已经学习了如何使用矩阵变换来排列二维或三维空间中的对象.所以现在是时候通过实现一些简单的变换矩阵来获得一些实际经验了.在接下来的三次作业中,我们将要求你去模拟一个基于CPU 的光 ...

  8. Map中getOrDefault()与数值进行比较

    一般用哈希表计数时,value类型通常为Integer.如果想比较某个key出现的次数,使用get(key)与某个数值进行比较是有问题的.当哈希表中并不包含该key时,因为此时get方法返回值是nul ...

  9. 为什么阿里巴巴开发手册中强制要求 POJO 类使用包装类型?NPE问题防范

    封面:学校内的秋天 背景:写这个的原因,也是我这两天凑巧看到的,虽然我一直有 alibaba Java 开发手册,也看过不少次,但是一直没有注意过这个问题 属于那种看过,但又没完全看过 一起来看看吧冲 ...

  10. Spring Cloud Alibaba 的服务注册与发现

    Spring Cloud Alibaba 服务发现例子 一.需求 1.提供者完成的功能 2.消费者完成的功能 3.可以附加的额外配置 二.实现步骤 1.总的依赖引入 2.服务提供者和发现者,引入服务发 ...