game
枚举被所有人经过的路径(u,v)(u,v)(u,v)(显然只有一条连续路径被所有人经过)。
考虑LCA(u,v)LCA(u,v)LCA(u,v)不为uuu或vvv时,fu,fvf_u,f_vfu,fv分别表示在u,vu,vu,v子树中给KKK个人安排出起始点(可重合)的方案数。
发现fxf_xfx就是生成函数K!(∑i=0∞1i!)∏y∈sonx(1+szy⋅x)K!(\sum \limits_{i=0}^{\infty}\dfrac{1}{i!})\prod\limits_{y\in son_x}(1+sz_y·x)K!(i=0∑∞i!1)y∈sonx∏(1+szy⋅x)的xKx^KxK项的系数。
分治NTT求出fxf_xfx的复杂度是degreexlog2degreexdegree_x\log ^2 degree_xdegreexlog2degreex的,故总复杂度为nlog2nn\log^2 nnlog2n。
dfsdfsdfs一遍处理出不为祖先关系的点对答案。
对于LCA(u,v)=uLCA(u,v)=uLCA(u,v)=u的情况,设v∈x,x∈sonuv\in x,x\in son_uv∈x,x∈sonu则fuf_ufu需要乘上1+(n−szu)x1+szx\frac{1+(n-sz_u)x}{1+sz_{x}}1+szx1+(n−szu)x,多项式乘或除另一个一次多项式的复杂度显然是线性的,而nnn个点的树中szxsz_xszx的值最多只有n\sqrt nn种。复杂度是nnn\sqrt nnn的。
总复杂度O(nn+nlog2n)O(n\sqrt n+n\log^2 n)O(nn+nlog2n)
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define sc second
using namespace std;
typedef long long ll;
const int N=1e5+100;
const int mod=998244353,g=3,nvg=332748118;
int num,n,m,ans,frac,nv[N];
int rep[N<<3],sz[N],sum[N],val[N];
int head[N],to[N<<1],nxt[N<<1],tot;
vector<int>xs[N];
map<int,int>mp[N];
char cp;
inline void rd(int &x)
{
cp=getchar();x=0;
for(;!isdigit(cp);cp=getchar());
for(;isdigit(cp);cp=getchar()) x=x*10+(cp^48);
}
inline int fp(int x,int y)
{
int re=1;
for(;y;y>>=1,x=(ll)x*x%mod)
if(y&1) re=(ll)re*x%mod;
return re;
}
inline void ad(int &x,int y){
x+=y;if(x>=mod) x-=mod;}
inline void dc(int &x,int y){
x-=y;if(x<0) x+=mod;}
inline int inc(int x,int y){
x+=y;return x>=