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 要卡常数的说卧槽... 发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了 一开 ...
随机推荐
- 【MM系列】SAP 创建工厂
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 创建工厂 前言部分 大家 ...
- 【MM系列】SAP MM模块-BAPI:BAPI_GOODSMVT_CREATE的CODE分析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-BAPI:BAPI ...
- cocos2dx基础篇(15) 列表视图CCTableView
[3.x] (1)去掉 "CC" (2)TableViewCell 中: > unsigned int getIdx() 返回类型改为 ssize_t(类型为 long) ( ...
- 6.文件所有权和权限----免费设置匿名----Windows键盘记录器----简介和python模块
文件所有权和权限 touch --help cd Desktop mkdir Folder cd Folder clear touch Test1 Test2 Test3 Test4 ls ls -l ...
- 使用powershell管理Docker Toolbox
打开PowerShell,输入: docker-machine ls 我们可以看到我们当前的Docker虚拟机的状态.如果什么都没有的话,那么我们可以使用以下命令创建一个Docker虚拟机. dock ...
- 使用itchat完成微信自动回复
import itchat from itchat.content import * # 微信自动回复 @itchat.msg_register([TEXT]) def text_reply(msg) ...
- mybatis使用map传递多参数报错:A query was run and no Result Maps were found for the Mapped Statement
在使用mybatis进行多参数传递时,报错: A query was run and no Result Maps were found for the Mapped Statement 'xx.xx ...
- kubeadm搭建K8s集群及Pod初体验
基于Kubeadm 搭建K8s集群: 通过上一篇博客,我们已经基本了解了 k8s 的基本概念,也许你现在还是有些模糊,说真的我也是很模糊的.只有不断地操作去熟练,强化自己对他的认知,才能提升境界. 我 ...
- mysql分组统计后将结果顺序排列(union实现)
工作中用到统计12月份通话记录,统计号码拨打次数,但是问题出在一个号码可以拨打多次,每次可能接通也可能不接通,如果用主叫号码caller字段group by分组后count(*)统计数目,这样会导致不 ...
- Vue:Elementui中的Tag与页面其它元素相互交互的两三事
前言 公司系统在用elementui做后台开发,不免遇到一些需要自己去根据原有的功能上,加一些交互的功能.今天来介绍下我在用elementUi里的Tag标签与多选框交互的过程,东西听上去很简单,但就是 ...