1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; #define Mod1(x) ((x>=P)&&(x-=P)) #define Mod2(x) ((x<0)&&(x+=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO; int rd(){ int s=0,f=0; while(!isdigit(IO=getchar())) if(IO=='-') f=1; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
bool Mbe; const int N=1<<18|10,P=998244353;
int n,m,k; int A[N]; ll qpow(ll x,ll k=P-2) { ll res=1; for(;k;k>>=1,x=x*x%P) if(k&1) res=res*x%P; return res; } int Pow[N]; struct Edge{ int to,nxt; }e[N]; int head[N],ecnt,deg[N]; void AddEdge(int u,int v) { e[++ecnt]=(Edge){v,head[u]}; head[u]=ecnt,deg[v]++; } #define erep(u,i) for(int i=head[u];i;i=e[i].nxt)
int w[N]; void Init() { int R=1<<18; int t=qpow(3,(P-1)/R); w[R/2]=1; rep(i,R/2+1,R-1) w[i]=1ll*w[i-1]*t%P; drep(i,R/2-1,1) w[i]=w[i<<1]; }
int rev[N]; void NTT(int n,int *a,int f) { static int e[N>>1]; rep(i,0,n-1) if(i<rev[i]) swap(a[i],a[rev[i]]); for(int i=e[0]=1,t;i<n;i<<=1) { int *e=w+i; for(int l=0;l<n;l+=i*2) { for(int j=l;j<l+i;++j) { t=1ll*a[j+i]*e[j-l]%P; a[j+i]=a[j]-t,Mod2(a[j+i]); a[j]+=t,Mod1(a[j]); } } } if(f==-1) { reverse(a+1,a+n); ll base=qpow(n); rep(i,0,n-1) a[i]=a[i]*base%P; } } int Init(int n) { int R=1,c=-1; while(R<=n) R<<=1,c++; rep(i,0,R-1) rev[i]=(rev[i>>1]>>1)|((i&1)<<c); return R; }
int Q[N],L,R,vis[N];
namespace pt1{ const int N=1010; int dis[N]; void Bfs(int u) { rep(i,1,n) dis[i]=-1; dis[Q[L=R=1]=u]=0; while(L<=R) { u=Q[L++]; erep(u,i){ int v=e[i].to; if(~dis[v]) continue; dis[v]=dis[u]+1,Q[++R]=v; } } } void Solve() { int ans=0; rep(i,2,n) { Bfs(i); rep(j,1,i-1) ans=(ans+Pow[dis[j]])%P; } ans=ans*qpow(n*(n-1)/2)%P; printf("%d\n",ans); } }
int Ans[N],sz[N]; namespace pt2{ int mi=1e9,rt; void FindRt(int n,int u,int f) { int ma=0; sz[u]=1; erep(u,i) { int v=e[i].to; if(v==u || v==f || vis[v]) continue; FindRt(n,v,u),sz[u]+=sz[v],cmax(ma,sz[v]); } cmax(ma,n-sz[u]); if(mi>ma) mi=ma,rt=u; }
int F[N],A[N],B[N]; void Solve(int n,int k) { int R=Init(n*2+1); rep(i,0,R) F[i]=0; rep(i,0,n) F[i]=A[i]; NTT(R,F,1); rep(i,0,R-1) F[i]=1ll*F[i]*F[i]%P; NTT(R,F,-1); if(k==1) rep(i,0,n*2) Ans[i]+=F[i],Mod1(Ans[i]); else rep(i,0,n*2) Ans[i]-=F[i],Mod2(Ans[i]); } int maxd; void dfs(int u,int f,int d=0) { A[d]++,sz[u]=1,cmax(maxd,d); erep(u,i) { int v=e[i].to; if(v==u || v==f || vis[v]) continue; dfs(v,u,d+1),sz[u]+=sz[v]; } } void Divide(int n,int u) { mi=1e9,FindRt(n,u,0),u=rt; vis[u]=1; int D=0;B[0]=1; erep(u,i) { int v=e[i].to; if(vis[v]) continue; maxd=0,dfs(v,u,1); Solve(maxd,-1); rep(j,0,maxd) B[j]+=A[j],A[j]=0; cmax(D,maxd); } rep(i,0,D) A[i]=B[i],B[i]=0; Solve(D,1); rep(i,0,D) A[i]=0; erep(u,i) { int v=e[i].to; if(vis[v]) continue; Divide(sz[v],v); } } void Solve() { rep(i,1,n) vis[i]=0; Divide(n,1); int ans=0; rep(i,1,n) ans=(ans+1ll*Ans[i]*Pow[i])%P; ans=ans*qpow(1ll*n*(n-1)%P)%P; printf("%d\n",ans); } }
int QL[N<<2],QR[N<<2]; void Add(int p,int l,int r,int ql,int qr,int x) { if(ql<=l && r<=qr) { if(!QL[p]) QL[p]=x; QR[p]=x; return; } int mid=(l+r)>>1; if(ql<=mid) Add(p<<1,l,mid,ql,qr,x); if(qr>mid) Add(p<<1|1,mid+1,r,ql,qr,x); }
int typ; int X[N],Y[N],D;
void dfs(int *C,int u,int f,int d) { cmax(D,d),C[d]++; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(v==f || vis[v]) continue; dfs(C,v,u,d+1); } }
void Mark(int i,int k) { int l=A[i==1?m:i-1],r=A[i==m?1:i+1]; vis[l]=vis[r]=k; }
void Get(int p,int l,int r) { if(QL[p]) { if(typ==0) { int qr=QR[p]; rep(x,QL[p],QR[p]) Mark(x,1),dfs(X,A[x],0,qr-x),Mark(x,0); int T=D; D=0; rep(x,l,r) Mark(x,1),dfs(Y,A[x],0,x-l),Mark(x,0); int R=Init(T+D+1); NTT(R,X,1),NTT(R,Y,1); rep(i,0,R-1) X[i]=1ll*X[i]*Y[i]%P; NTT(R,X,-1); rep(i,0,T+D) Ans[i+l-qr]+=X[i],Mod1(Ans[i+l-qr]); rep(i,0,R) X[i]=Y[i]=0; } else { int ql=QL[p]; rep(x,QL[p],QR[p]) Mark(x,1),dfs(X,A[x],0,x-ql),Mark(x,0); int T=D; D=0; rep(x,l,r) Mark(x,1),dfs(Y,A[x],0,r-x),Mark(x,0); int R=Init(T+D+1); NTT(R,X,1),NTT(R,Y,1); rep(i,0,R-1) X[i]=1ll*X[i]*Y[i]%P; NTT(R,X,-1); int d=ql+m-r; rep(i,0,T+D) Ans[i+d]+=X[i],Mod1(Ans[i+d]); rep(i,0,R) X[i]=Y[i]=0; } QL[p]=QR[p]=0; } if(l==r) return; int mid=(l+r)>>1; Get(p<<1,l,mid),Get(p<<1|1,mid+1,r); }
int main() { freopen("path.in","r",stdin),freopen("path.out","w",stdout); n=rd(),k=rd(); rep(i,1,n) Pow[i]=qpow(i,k); rep(i,1,n) { int u=rd(),v=rd(); AddEdge(u,v),AddEdge(v,u); } if(n<=1000) return pt1::Solve(),0; Init(),L=1; rep(i,1,n) if(deg[i]==1) sz[Q[++R]=i]=1; while(L<=R) { int u=Q[L++]; vis[u]=1; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(deg[v]<=1) sz[u]+=sz[v]; if(--deg[v]==1) Q[++R]=v; } } for(int u=1;u<=n;++u) if(!vis[u]) { while(1) { vis[u]=1,A[++m]=u; int nxt=-1; for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(!vis[v]) nxt=v; } if(nxt==-1) break; u=nxt; } break; } if(m==1) return pt2::Solve(),0; fprintf(stderr,"Circle Length =%d\n",m); rep(i,1,n) vis[i]=0;
k=m/2; rep(i,1,m) { Mark(i,1); pt2::Divide(sz[A[i]],A[i]); Mark(i,0); } rep(i,1,n) Ans[i]=1ll*Ans[i]*(P+1)/2%P; rep(i,1,n) vis[i]=0; rep(i,1,m-1) Add(1,1,m,i+1,min(i+k,m),i); typ=0,Get(1,1,m); rep(i,1,m-k-1) Add(1,1,m,i+k+1,m,i); typ=1,Get(1,1,m); int ans=0; rep(i,1,n) ans=(ans+1ll*Ans[i]*Pow[i])%P; ans=ans*qpow(1ll*n*(n-1)/2%P)%P; printf("%d\n",ans); }
|