Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 200004
#define mod 998244353
#define ll long long
using namespace std;
inline ll add(ll a,ll b) {
return (a+b)%mod;
}
inline ll de(ll a,ll b) {
return ((a%mod)-(b%mod)+mod)%mod;
}
int n,edges;
int hd[maxn],to[maxn],nex[maxn],dep[maxn],f[maxn],siz[maxn],son[maxn],top[maxn],p[maxn],sz[maxn];
ll inv[maxn],mul,g[maxn],ans;
struct Node {
int u,v;
Node(int u=0,int v=0):u(u),v(v){}
};
vector<Node>vi[maxn];
vector<int>G[maxn];
ll qpow(ll base,ll k) {
ll re=1;
while(k) {
if(k&1) re=re*base%mod;
base=base*base%mod;
k>>=1;
}
return re;
}
void add(int u,int v) {
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v;
}
void dfs1(int u) {
dep[u]=dep[f[u]]+1,siz[u]=1,G[dep[u]].push_back(u);
for(int i=hd[u];i;i=nex[i]) {
int v=to[i];
if(v==f[u]) continue;
dfs1(v),siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp) {
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(int i=hd[u];i;i=nex[i]) {
int v=to[i];
if(v==f[u]||v==son[u]) continue;
dfs2(v,v);
}
}
int LCA(int x,int y) {
while(top[x]^top[y]) {
dep[top[x]]>dep[top[y]]?x=f[top[x]]:y=f[top[y]];
}
return dep[x]<dep[y]?x:y;
}
void init() {
for(int i=0;i<maxn;++i) p[i]=i,sz[i]=1;
}
int find(int x) {
return p[x]==x?x:p[x]=find(p[x]);
}
void merge(int x,int y) {
int fx=find(x),fy=find(y);
if(fx!=fy) {
mul=mul*inv[sz[fx]+1]%mod*inv[sz[fy]+1]%mod;
p[fx]=fy, sz[fy]+=sz[fx];
mul=mul*(sz[fy]+1)%mod;
}
}
void Initialize() {
init(),inv[1]=1,mul=qpow(2,n);
for(int i=2;i<maxn;++i) inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod;
}
int main() {
// setIO("input");
scanf("%d",&n);
for(int i=2;i<=n;++i) scanf("%d",&f[i]), add(f[i],i);
Initialize(),dfs1(1),dfs2(1,1);
for(int i=2;i<=n;++i) vi[dep[i]-1].push_back(Node(i,f[i]));
for(int i=1;i<=n;++i) {
for(int j=1;j<G[i].size();++j) {
vi[dep[G[i][j]] - dep[LCA(G[i][j], G[i][j-1])]].push_back(Node(G[i][j], G[i][j-1]));
}
}
ans=de(mul,n+1);
for(int i=1;i<=n;++i) {
for(int j=0;j<vi[i].size();++j)
merge(vi[i][j].u, vi[i][j].v);
ans=add(ans,de(mul,n+1));
}
printf("%lld\n",ans);
return 0;
}

  

Comet OJ - Contest #6 D. 另一道树题 并查集 + 思维 + 计数的更多相关文章

  1. [Comet OJ - Contest #7 D][52D 2417]机器学习题_斜率优化dp

    机器学习题 题目大意: 数据范围: 题解: 学长说是决策单调性? 直接斜率优化就好了嘛 首先发现的是,$A$和$B$的值必定是某两个$x$值. 那么我们就把,$y$的正负分成两个序列,$val1_i$ ...

  2. Comet OJ - Contest #2 简要题解

    Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...

  3. Comet OJ - Contest #2简要题解

    Comet OJ - Contest #2简要题解 前言: 我没有小裙子,我太菜了. A 因自过去而至的残响起舞 https://www.cometoj.com/contest/37/problem/ ...

  4. Comet OJ - Contest #4--前缀和

    原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...

  5. Comet OJ - Contest #11 题解&赛后总结

    Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...

  6. Comet OJ - Contest #8

    Comet OJ - Contest #8 传送门 A.杀手皇后 签到. Code #include <bits/stdc++.h> using namespace std; typede ...

  7. Comet OJ - Contest #13-C2

    Comet OJ - Contest #13-C2 C2-佛御石之钵 -不碎的意志-」(困难版) 又是一道并查集.最近做过的并查集的题貌似蛮多的. 思路 首先考虑,每次处理矩形只考虑从0变成1的点.这 ...

  8. Comet OJ - Contest #13 「火鼠的皮衣 -不焦躁的内心-」

    来源:Comet OJ - Contest #13 芝士相关: 复平面在信息学奥赛中的应用[雾 其实是道 sb 题??? 发现原式貌似十分可二项式定理,然后发现确实如此 我们把 \(a^i\) 替换成 ...

  9. Comet OJ - Contest #13 「佛御石之钵 -不碎的意志-」(hard)

    来源:Comet OJ - Contest #13 一眼并查集,然后发现这题 tmd 要卡常数的说卧槽... 发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了 一开 ...

随机推荐

  1. 请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码

     一.请求转发  请求包含  请求重定向 Demo5.java   注意:doPost()方法中别忘写doGet(request, response); public void doGet(HttpS ...

  2. Java ——集合框架 list lambda set map 遍历方法 数据结构

    本节重点思维导图 集合框架 有序无序:元素放入的顺序与取出的顺序是否一致,一致即为有序,不一致即无序. List:允许重复.有序 ArrayList:长度可变的数组,遍历速度快 LinkedList: ...

  3. 应用安全 - CMS - Discuz漏洞汇总

    SSV-90861 Date:2012 类型:敏感信息泄露 影响范围:DZ x2.5  POC:http://www.xx.xx/uc_server/control/admin/db.php http ...

  4. 15.永恒之蓝exp----

    永恒之蓝exp 2017年,影响于全世界 SMB ripid7官网获取ms17-010的exp信息 Rapid7: https://www.rapid7.com/db/modules/exploit/ ...

  5. 20191118 Spring Boot官方文档学习(4.9)

    4.9.安全 如果Spring Security在类路径上,则默认情况下Web应用程序是采用的.Spring Boot依靠Spring Security的内容协商策略来确定使用httpBasic还是f ...

  6. Python解释器判断整数相加溢出

    溢出,则和的最高位(即符号位)与两个加数都不相同,例如 1)非负数+非负数=负数 2)负数+负数=非负数 那么,假设x为a与b的和,((a^b)>=0 && (x^a)<0 ...

  7. qt undefined reference to `vtable for subClass'

    1. 建立一个console工程 QT -= gui CONFIG += c++ console CONFIG -= app_bundle # The following define makes y ...

  8. Redis集群,备份,哨兵机制

    原文:https://blog.csdn.net/zy345293721/article/details/87536144 1.集群        先来简单了解下redis中提供的集群策略, 虽然re ...

  9. px-em-pt等字体的不同

  10. 该项目不知道如何运行配置文件 IIS Express。The project doesn’t know how to run the profile IIS Express

    原文:该项目不知道如何运行配置文件 IIS Express. 方案1(推荐). 可能原因是:禁用掉Microsft ASP.NET和Web工具扩展和微软Azure的应用程序服务工具扩展,恢复启用即可. ...