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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std;
#define MAXN 500006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; const int P = 998244353; const int iv2 = P + 1 >> 1; int n , m; vi G[MAXN];
int J[MAXN] , iJ[MAXN] , p2[MAXN] , pw[MAXN] , pa[MAXN]; int Pow( int x , int a ) { int ret = 1; while( a ) { if( a & 1 ) ret = ret * 1ll * x % P; x = x * 1ll * x % P , a >>= 1; } return ret; }
int C( int a , int b ) { if( b < 0 || a < 0 || a < b ) return 0; return J[a] * 1ll * iJ[b] % P * iJ[a - b] % P; }
inline void Inc( int& a , int b ) { a = ( a + b < P ? a + b : a + b - P ); }
int Wn[2][550006] , rev[MAXN]; void getwn( int len ) { for( int mid = 1 ; mid < len ; mid <<= 1 ) { int w0 = Pow( 3 , ( P - 1 ) / ( mid << 1 ) ) , w1 = Pow( 3 , P - 1 - ( P - 1 ) / ( mid << 1 ) ); Wn[0][mid] = Wn[1][mid] = 1; for( int i = 1 ; i < mid ; ++ i ) Wn[0][mid + i] = 1ll * Wn[0][mid + i - 1] * w0 % P, Wn[1][mid + i] = 1ll * Wn[1][mid + i - 1] * w1 % P; } } void getr( int len ) { int t = __builtin_ctz( len ) - 1; for( int i = 1 ; i < len ; ++ i ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << t ); } void NTT( int* A , int len , int typ ) { rep( i , 0 , len - 1 ) if( i < rev[i] ) swap( A[i] , A[rev[i]] ); for( int mid = 1 ; mid < len ; mid <<= 1 ) for( int i = 0 ; i < len ; i += ( mid << 1 ) ) for( int k = 0 ; k < mid ; ++ k ) { int t1 = A[i + k] , t2 = 1ll * A[i + k + mid] * Wn[typ][mid + k] % P; A[i + k] = (t1 + t2 < P ? t1 + t2 : t1 + t2 - P) , A[i + k + mid] = (t1 < t2 ? t1 + P - t2 : t1 - t2); } if( typ == 1 ) for( int inv = Pow( len , P - 2 ) , i = 0 ; i < len ; ++ i ) A[i] = 1ll * A[i] * inv % P; }
int vis[MAXN];
int sz[MAXN]; void afs( int u , int f ) { sz[u] = 1; rep( i , 0 , G[u].size() - 1 ) { int v = G[u][i]; if( v == f || vis[v] ) continue; afs( v , u ); sz[u] += sz[v]; } } int tmn , trt , RT; void cfs( int u , int f ) { int s = sz[RT] - sz[u]; for( int v : G[u] ) if( v != f && !vis[v] ) { cfs( v , u ); s = max( s , sz[v] ); } if( s < tmn ) tmn = s , trt = u; } int getit( int u ) { tmn = 0x3f3f3f3f , trt = RT = u , afs( u , u ) , cfs( u , u ); return trt; }
int a , iva1 , blo;
int Fa[MAXN] , Siz[MAXN] , re , tds[MAXN]; void efs( int u , int f ) { tds[u] = 1; Fa[u] = f , Siz[u] = 1; for( int v : G[u] ) if( v != f ) { efs( v , u ); Siz[u] += Siz[v]; } }
int siz[MAXN]; void dfs$( int u , int f ) { if( u == f ) siz[u] = n; sz[u] = 1; for( int v : G[u] ) if( v != f && !vis[v] ) { dfs$( v , u ); if( v == Fa[u] ) siz[v] = n - Siz[u]; else siz[v] = Siz[v]; sz[u] += sz[v]; } }
int am , ivtt;
vi nod , cur; int RR; int xs[MAXN] , dis[MAXN]; void pfs( int u , int f ) { dis[u] = dis[f] + 1; cur.pb( u ); for( int v : G[u] ) if( v != f && !vis[v] ) { xs[v] = xs[u] * 1ll * a % P; pfs( v , u ); } }
void Juan( int A[] , int n ) { int len = 1; while( len <= n ) len <<= 1; getr( len ); NTT( A , len , 0 ); rep( i , 0 , len - 1 ) A[i] = A[i] * 1ll * A[i] % P; NTT( A , len , 1 ); rep( i , n , len - 1 ) A[i] = 0; } void Juan( int A[] , int B[] , int n ) { int len = 1; while( len <= n ) len <<= 1; getr( len ); NTT( A , len , 0 ) , NTT( B , len , 0 ); rep( i , 0 , len - 1 ) A[i] = A[i] * 1ll * B[i] % P; NTT( A , len , 1 ) , NTT( B , len , 1 ); rep( i , n , len - 1 ) A[i] = 0; }
int A[MAXN] , B[MAXN] , qd[MAXN] , tm[MAXN] , pt[MAXN] , kt[MAXN] , X[MAXN] , rX[MAXN] , xx[MAXN] , xp[MAXN];
ll res = 0;
void solve( int u ) { if( vis[u] ) return; dfs$( u , u ); int dg = 0; RR = u; for( int v : G[u] ) if( !vis[v] ) ++ dg;
if( sz[u] <= blo ) { dis[u] = 0; for( int v : G[u] ) if( !vis[v] ) { xs[v] = am; cur.clear(); pfs( v , u ); for( int x : cur ) { for( int y : nod ) res = ( res + pw[n - 1] * 1ll * xs[x] % P * xs[y] % P * C( siz[x] + siz[y] , siz[x] ) ) % P; if( x != v ) res = ( res + C( n - siz[v] + siz[x] , siz[x] ) * 1ll * xs[x] % P * pw[n - 1] % P * m ) % P; else res = ( res + C( n , siz[x] ) * 1ll * xs[x] % P * ( P - 1 ) % P * pw[n - 1] ) % P; for( int y : nod ) Inc( pt[dis[x] + dis[y]] , 1 ); Inc( pt[dis[x]] , 1 );
} nod.insert( nod.end() , cur.begin() , cur.end() ); } nod.clear(); } else { dis[u] = 0; for( int v : G[u] ) if( !vis[v] ) { xs[v] = am; cur.clear(); pfs( v , u ); if( sz[v] <= 1000 ) { for( int x : cur ) for( int y : cur ) { Inc( pt[dis[x] + dis[y]] , P - iv2 ); Inc( rX[siz[x] + siz[y]] , P - iJ[siz[x]] * 1ll * xs[x] % P * xs[y] % P * iJ[siz[y]] % P * iv2 % P ); } } else { for( int x : cur ) Inc( xp[dis[x]] , 1 ) , Inc( xx[siz[x]] , iJ[siz[x]] * 1ll * xs[x] % P ); Juan( xp , n + 1 ) , Juan( xx , n * 2 + 1 ); rep( i , 1 , n ) Inc( pt[i] , P - xp[i] * 1ll * iv2 % P ) , xp[i] = 0; rep( i , 1 , n * 2 ) Inc( rX[i] , P - xx[i] * 1ll * iv2 % P ) , xx[i] = 0; } for( int x : cur ) { if( x != v ) res = ( res + C( n - siz[v] + siz[x] , siz[x] ) * 1ll * xs[x] % P * pw[n - 1] % P * m ) % P; else res = ( res + C( n , siz[x] ) * 1ll * xs[x] % P * ( P - 1 ) % P * pw[n - 1] ) % P; tm[dis[x]] ++; X[siz[x]] = ( X[siz[x]] + xs[x] * 1ll * iJ[siz[x]] ) % P; Inc( pt[dis[x]] , 1 ); } } Juan( X , n * 2 + 1 ) , Juan( tm , n + 1 ); rep( i , 1 , n * 2 ) Inc( rX[i] , X[i] * 1ll * iv2 % P ) , res = ( res + rX[i] % P * 1ll * J[i] % P * pw[n - 1] ) % P , rX[i] = X[i] = 0; rep( i , 1 , n ) Inc( pt[i] , tm[i] * 1ll * iv2 % P ) , tm[i] = 0; } vis[u] = 1; for( int v : G[u] ) if( !vis[v] ) solve( getit( v ) ); }
int rp[MAXN] , pfm[MAXN];
void solve() { getwn( 1 << 19 ); cin >> n >> m; J[0] = iJ[0] = pw[0] = p2[0] = pfm[0] = 1; a = Pow( m + 1 , P - 2 ); am = a * 1ll * m % P; rep( i , 1 , n ) J[i] = J[i - 1] * 1ll * i % P , iJ[i] = Pow( J[i] , P - 2 ) , pw[i] = pw[i - 1] * 1ll * ( m + 1 ) % P , p2[i] = p2[i - 1] * 2 % P , pfm[i] = pfm[i - 1] * 1ll * ( P - m ) % P; rep( i , 2 , n ) { int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , G[v].pb( u ); } if( m == P - 1 ) { int as = 1 , noc = 0; rep( i , 1 , n ) if( G[i].size() > 2 ) { noc = 1; break; } if( !noc ) as = as * 2 % P; cout << as << endl; return; } blo = 6000; efs( 1 , 1 ); solve( getit( 1 ) ); rep( t , 0 , n - 2 ) tm[t] = pt[t + 2] * 1ll * J[t] % P; rep( t , 0 , n ) X[t] = iJ[n - t]; int len = 1; while( len <= n + n ) len <<= 1; getr( len ); NTT( tm , len , 0 ) , NTT( X , len , 0 ); rep( i , 0 , len - 1 ) tm[i] = tm[i] * 1ll * X[i] % P; NTT( tm , len , 1 ); rep( i , 0 , n ) tm[i] = tm[i + n] * 1ll * iJ[i] % P; res = ( res + p2[n] * 1ll * pw[n - 1] ) % P;
rep( i , 2 , n - 1 ) { res = ( res + ( C( n - 1 , i ) + P - tm[i - 2] ) % P * 1ll * pfm[i] % P * pw[n - 1 - i] % P ) % P; } cout << res << endl; }
signed main() {
solve(); }
|