[ABC264Ex] Perfect Binary Tree
Problem Statement
We have a rooted tree with vertices numbered .
The tree is rooted at Vertex , and the parent of Vertex is Vertex .
For each integer , solve the following problem:
There are ways to choose some of the vertices numbered between and so that Vertex is chosen.
How many of them satisfy the following condition: the subgraph induced by the set of chosen vertices forms a perfect binary tree (with vertices for a positive integer ) rooted at Vertex ?
Since the count may be enormous, print the count modulo .
What is an induced subgraph?
Let be a subset of the vertex set of a graph . The subgraph induced by this vertex set is constructed as follows:
- Let the vertex set of equal .
- Then, we add edges to as follows:
- For all vertex pairs such that , if there is an edge connecting and in , then add an edge connecting and to .
What is a perfect binary tree?
A perfect binary tree is a rooted tree that satisfies all of the following conditions:
- Every vertex that is not a leaf has exactly children.
- All leaves have the same distance from the root.
Here, we regard a graph with vertex and edges as a perfect binary tree, too.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$ $P_2$ $P_3$ $\dots$ $P_N$
Output
Print lines. The -th () line should contain the answer as an integer when .
Sample Input 1
10 1 1 2 1 2 5 5 5 1
Sample Output 1
1 1 2 2 4 4 4 5 7 10
The following ways of choosing vertices should be counted:
- when
- when
- when
- when
- when
- when
Sample Input 2
1
Sample Output 2
1
If , the -nd line of the Input is empty.
Sample Input 3
10 1 2 3 4 5 6 7 8 9
Sample Output 3
1 1 1 1 1 1 1 1 1 1
Sample Input 4
13 1 1 1 2 2 2 3 3 3 4 4 4
Sample Output 4
1 1 2 4 4 4 4 4 7 13 13 19 31
先考虑如果不带实时询问怎么做?定义 为以 为节点,深度为 的导出完全二叉树有多少个。发现 是 级别的,因为一个 层完全二叉树的节点数量是 级别,而节点数量要 。
用 表示 的所有儿子的集合, 初始化
这个可以化简为
这个东西就可以实现树上 转移了,最终答案为 ,总复杂度
现在要求加点,询问。那么思路很简单,首先如果一个点的层数大过 ,他影响不到答案。然后考虑不断往上爬,去更改会改变的答案。
要直接维护 值不容易,考虑维护所有 值的和还有平方和,有这两个更改我们可以推出 值的更改。然后发现,如果现在加入点 的时候,点 与点 的层数差为 ,那么只有 有可能更改,加上 的层数 ,所以这样子改的复杂度是 的。具体更改时就是减去旧的加上新的就行了。
#include<cstdio>
const int N=3e5+5,P=998244353,inv2=499122177;
typedef long long LL;
long long s[N][25],f[N][25],dp[N][25],ans,dep[N];//s表示和,f表示平方和
int n,k,fa[N];
void dfs(int x,int y,LL a,LL b)//a表示原来的,b表示新的
{
LL k=dp[x][y];
(s[x][y]+=b-a+P)%=P;
(f[x][y]+=b*b%P-a*a%P+P)%=P;
dp[x][y]=(s[x][y]*s[x][y]%P-f[x][y]+P)*inv2%P;
if(x-1)
dfs(fa[x],y+1,k,dp[x][y]);
}
int main()
{
scanf("%d",&n);
dp[1][0]=1;
puts("1");
for(int i=2;i<=n;i++)
{
scanf("%d",fa+i),dep[i]=dep[fa[i]]+1;
f[i][0]=dp[i][0]=s[i][0]=1;
if(dep[i]<=20)
dfs(fa[i],1,0,1);
// puts("qzmakioi");
ans=0;
for(int j=0;j<=20;j++)
(ans+=dp[1][j])%=P;
printf("%lld\n",ans);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】