Comet OJ - Contest #6 D. 另一道树题 并查集 + 思维 + 计数
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. 另一道树题 并查集 + 思维 + 计数的更多相关文章
- [Comet OJ - Contest #7 D][52D 2417]机器学习题_斜率优化dp
机器学习题 题目大意: 数据范围: 题解: 学长说是决策单调性? 直接斜率优化就好了嘛 首先发现的是,$A$和$B$的值必定是某两个$x$值. 那么我们就把,$y$的正负分成两个序列,$val1_i$ ...
- Comet OJ - Contest #2 简要题解
Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...
- Comet OJ - Contest #2简要题解
Comet OJ - Contest #2简要题解 前言: 我没有小裙子,我太菜了. A 因自过去而至的残响起舞 https://www.cometoj.com/contest/37/problem/ ...
- Comet OJ - Contest #4--前缀和
原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...
- Comet OJ - Contest #11 题解&赛后总结
Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...
- Comet OJ - Contest #8
Comet OJ - Contest #8 传送门 A.杀手皇后 签到. Code #include <bits/stdc++.h> using namespace std; typede ...
- Comet OJ - Contest #13-C2
Comet OJ - Contest #13-C2 C2-佛御石之钵 -不碎的意志-」(困难版) 又是一道并查集.最近做过的并查集的题貌似蛮多的. 思路 首先考虑,每次处理矩形只考虑从0变成1的点.这 ...
- Comet OJ - Contest #13 「火鼠的皮衣 -不焦躁的内心-」
来源:Comet OJ - Contest #13 芝士相关: 复平面在信息学奥赛中的应用[雾 其实是道 sb 题??? 发现原式貌似十分可二项式定理,然后发现确实如此 我们把 \(a^i\) 替换成 ...
- Comet OJ - Contest #13 「佛御石之钵 -不碎的意志-」(hard)
来源:Comet OJ - Contest #13 一眼并查集,然后发现这题 tmd 要卡常数的说卧槽... 发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了 一开 ...
随机推荐
- JS图片宽度自适应移动端
$(function(){ $("#d-intro").find("img").each(function () { $(this ...
- [Web 前端] 025 js 的对象、数组和数学对象
1. Javascript 对象 1.1 创建对象 1.1.1 使用原始的方式创建内置对象 var myObject = new Object(); myObject.name = "lij ...
- PL/SQL基本操作
1.常规过程化形式 declare o_booking_flag ); begin -- Call the procedure destine_ticket(', , 'E', , o_booking ...
- nrm安装与配置
nrm安装与配置:https://blog.csdn.net/anway12138/article/details/79455224
- Python入门之format()方法
在此列出format()方法的一些基本使用: >>> '{}{}{}'.format('圆周率是',3.1415926,'...') '圆周率是3.1415926...' >& ...
- UVA 11354 Bond 最小生成树 + lca
题意 给出一张图,q个询问,每次询问给出uv,找出一条路径,使这条路径上的最大边权是两点所有路径中最小,输出这个值 思路 很显然要先求出最小生成树,任意两点在最小生成树上有唯一路径,并且这条路径上的最 ...
- letsencrypt 免费SSL证书申请, 自动更新
Let's Encrypt 泛域名 证书申请 及自动更新 关键字:SSL证书.HTTPS 初次申请 1. 下载certbot wget https://dl.eff.org/certbot-auto ...
- 深入了解RabbitMQ工作原理及简单使用
深入了解RabbitMQ工作原理及简单使用 RabbitMQ系列文章 RabbitMQ在Ubuntu上的环境搭建 深入了解RabbitMQ工作原理及简单使用 RabbitMQ交换器Exchange介绍 ...
- maven联通网络下中央仓库不能访问的解决办法
最近刚开始学习maven工具,下载解压完毕,环境变量配置完毕,运行如下命令尝试快速构建一个maven项目: mvn archetype:generate 结果就有问题: [INFO] Scanning ...
- vuex实现数据共享
1.store.js结构 import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Sto ...