ICPC2024成都 E. Disrupting Communications 题解
zhengrunzhe · · 题解
题目链接
题目大意:给定一棵树,每次询问树上包含询问路径上任意至少一点的连通块数。
考虑朴素做法,如果我们把询问路径提取出来,以其上每个点为根向非询问路径的点做 dfs 树形 dp,可以求出单独包含该点的方案数,dp 方程为:
f[p]=\prod_{q\in son(p)} (f[q]+1)
其中,
获取了询问路径上单独包含每个点的 dp 值,那么路径总的合法方案可以看做是在这条路径上选取子路径的方案之和,一条子路径的方案为选取子路径上的所有点的 dp 值的乘积。简单来说,把路径看做刚刚序列,答案即为所有区间的乘积之和。
暴力计算答案可以递推得来,
按照路径起点到终点的方式累加,因为本人在对拍的时候写了暴力代码,故把这段暴力代码贴出来方便理解:
inline const void dfs(int u,const int &fa)
{
f[u]=1;
for (const int &v:G[u])
if (v!=fa&&!vis[v])
dfs(v,u),
f[u]=mul(f[u],add(f[v],1));
}
int solve(int u,int v)
{
if (dep[u]<dep[v])swap(u,v);
vector<int>path1,path2;
while (dep[u]>dep[v])path1.push_back(u),u=fa[u];
while (u!=v)path1.push_back(u),path2.push_back(v),u=fa[u],v=fa[v];
reverse(path2.begin(),path2.end());
path1.push_back(u);
for (const int &p:path2)path1.push_back(p);
for (const int &p:path1)vis[p]=1;
for (const int &p:path1)dfs(p,0);
int ans(0),suf(0);
for (const int &p:path1)suf=mul(add(suf,1),f[p]),ans=add(ans,suf);
for (const int &p:path1)vis[p]=0;
return ans;
}
如果我们知道了区间的答案,也可以通过合并的方式计算(类似线段树信息合并):
代表
为了规避除法,于是我们请出了 Top Tree,笔者使用的是 Self-Adjusting Top Trees。
于是可以使用 SATT 通过此题,时间复杂度
#include<cstdio>
#include<algorithm>
using namespace std;
template<class T>inline const void read(T &in)
{
in=0;char ch(getchar());
while (ch<48||ch>57)ch=getchar();
while (ch>47&&ch<58)in=in*10+(ch&15),ch=getchar();
}
template<class T,class...Args>inline const void read(T &t,Args&...args)
{
read(t);read(args...);
}
constexpr int N(1e5+5),Mod(998244353);
inline const int add(const int &a,const int &b){return a+b>=Mod?a+b-Mod:a+b;}
inline const int mul(const int &a,const int &b){return 1ll*a*b%Mod;}
namespace SelfAdjustingTopTrees
{
constexpr bool Compress(0),Rake(1);
struct Tree
{
bool rev;
int lcnt,rcnt,cnt,f;
Tree *son[3],*fa;
static Tree *Null;
inline void *operator new(size_t size);
inline void *operator new[](size_t size);
inline void operator delete(void *ptr);
inline Tree():rev(0),lcnt(1),rcnt(1),cnt(1),f(1)
{
son[0]=son[1]=son[2]=fa=Null;
}
inline const void reverse()
{
swap(son[0],son[1]);
swap(lcnt,rcnt);
rev^=1;
}
template<const bool type>inline const void pushdown();
template<const bool type>inline const void pushup();
inline const bool isroot()
{
return fa->son[0]!=this&&fa->son[1]!=this;
}
inline const bool id()
{
return fa->son[1]==this;
}
inline const void set(Tree *p,const int &d)
{
(son[d]=p)->fa=this;
}
template<const bool type>inline const void rotate()
{
Tree *fa(this->fa);
fa->pushdown<type>();pushdown<type>();
const bool f(id());
if (fa->fa!=Null)fa->fa->son[fa->fa->son[2]==fa?2:fa->id()]=this;
this->fa=fa->fa;
fa->set(son[f^1],f);
set(fa,f^1);
fa->pushup<type>();pushup<type>();
}
template<const bool type>inline const void splay(Tree *goal=Null)
{
for (pushdown<type>();fa!=goal&&!isroot();rotate<type>())
if (fa->fa!=goal&&!fa->isroot())
fa->fa->pushdown<type>(),
(fa->id()^id()?this:fa)->rotate<type>();
}
template<const bool type,const bool d>inline const void splay_m()
{
Tree *p(this);
while (p->pushdown<type>(),p->son[d]!=Null)p=p->son[d];
p->splay<type>(fa);
}
}*Tree::Null,*node0;
#define Null Tree::Null
#define node(x) (node0+x)
char memoryPool[N*sizeof(Tree)<<1],*tail;
void *recycle[N],**top;
inline void *Tree::operator new(size_t size){return top!=recycle?*--top:tail-=size;}
inline void *Tree::operator new[](size_t size){return tail-=size;}
inline void Tree::operator delete(void *ptr){*top++=ptr;}
inline const void init(const int &n)
{
tail=memoryPool+sizeof(memoryPool);
top=recycle;
Null=new Tree;
Null->cnt=Null->lcnt=Null->rcnt=0;
Null->son[0]=Null->son[1]=Null->son[2]=Null->fa=Null;
node0=new Tree[n+1];
}
template<>inline const void Tree::pushup<Compress>()
{
f=mul(son[0]->f,mul(son[2]->f,son[1]->f));
lcnt=add(son[0]->lcnt,mul(mul(son[0]->f,son[2]->f),add(son[1]->lcnt,1)));
rcnt=add(son[1]->rcnt,mul(mul(son[1]->f,son[2]->f),add(son[0]->rcnt,1)));
cnt=add(add(son[0]->cnt,son[1]->cnt),mul(mul(add(son[0]->rcnt,1),son[2]->f),add(son[1]->lcnt,1)));
}
template<>inline const void Tree::pushup<Rake>()
{
f=mul(mul(son[0]->f,son[1]->f),add(son[2]->lcnt,1));
}
template<>inline const void Tree::pushdown<Compress>()
{
if (rev)
son[0]->reverse(),
son[1]->reverse(),
rev=0;
}
template<>inline const void Tree::pushdown<Rake>(){}
inline const void splice(Tree *p)
{
p->splay<Rake>();
(p=p->fa)->splay<Compress>();
Tree *q(p->son[2]);
q->pushdown<Rake>();
if (p->son[1]!=Null)
swap(p->son[1]->fa,q->son[2]->fa),
swap(p->son[1],q->son[2]);
else
{
p->set(q->son[2],1);
if (q->son[0]!=Null)
q->son[0]->splay_m<Rake,1>(),
q->son[0]->set(q->son[1],1),
p->son[2]=q->son[0];
else
q->son[1]->pushdown<Rake>(),
p->son[2]=q->son[1];
delete q;q=p->son[2];q->fa=p;
}
q->pushup<Rake>();p->pushup<Compress>();
p->son[1]->rotate<Compress>();
}
inline const void access(Tree *p)
{
p->splay<Compress>();
if (p->son[1]!=Null)
{
Tree *q(new Tree);
q->set(p->son[2],0);
q->set(p->son[1],2);
q->pushup<Rake>();
p->son[1]=Null;
p->set(q,2);
p->pushup<Compress>();
}
while (p->fa!=Null)splice(p->fa);
}
inline const void evert(Tree *p)
{
access(p);p->reverse();
}
inline const void expose(Tree *p,Tree *q)
{
evert(p);access(q);
}
inline const int query(Tree *p,Tree *q)
{
expose(p,q);return q->cnt;
}
inline const void link(Tree *p,Tree *q)
{
access(p);evert(q);p->set(q,1);p->pushup<Compress>();
}
}
using namespace SelfAdjustingTopTrees;
inline const void work()
{
int n,m;
read(n,m);
init(n);
for (int p,i(2);i<=n;i++)
read(p),link(node(p),node(i));
for (int u,v;m--;)
read(u,v),printf("%d\n",query(node(u),node(v)));
}
int main()
{
int T;read(T);while (T--)work();
return 0;
}