树上操作 题解

· · 个人记录

题意

我们保持初始时一个点 x 向根的方向为正方向,无论发生什么,正方向不会发生改变,原题的操作相当于这个:

然后让你求操作若干次后所有可能的树中每个结点的出现次数。

题解

假设我们现在需要求解结点 x 的答案,将它的祖先链拎出来(我们令其不包括 x),考虑经过若干次操作后,这条链的形态一定形如(仅考虑包含 x 的):

此时我们只需要求出这条祖先链以外的方案数即可,具体来说,我们需要知道 f_x 表示 x 的子树中不断操作可以得到不同的树的方案数。转移考虑两部分,一部分是直接将儿子所得的树拎到 x,一部分是不拎到 x,那么转移形如:

f_x = \sum_{son}f_{son} + \prod_{son}f_{son}

我们定义 x 的祖先链上每个点的 g 为儿子中不包括祖先链中点的 f 的乘积,那么假设祖先链为 u_1 \to u_2 \to ... \to u_p,ans_x 即为:

g_x \times \sum_{S \subseteq \{1, 2, ..., p\}}\prod_{v \in S} g_{u_v}

显然前缀扫一遍这个东西是好求的。

同时发现在树的 DFN 序上前缀扫的时候这个东西是可以撤销的,于是我们在 O(n \log V) 的时间复杂度内求出了所有点的答案(O(\log V) 的逆元时间似乎是不可避免的)。

我们考虑这个做法的道理,其实是因为假设非 x 的祖先链的结点操作到了 x 的祖先链上,那么 x 必定没有贡献,因此祖先链和非祖先链上的结点可以分开计算方案数。也有一个合理的解释是这个操作保证了每个结点 x 的祖先链结点集合只会取子集,于是发现可以特殊考虑祖先链上的形态,其余点另外计算。

Code

Code:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define fir first
#define sec second
#define mkp make_pair 
#define pb push_back
#define lep( i, l, r ) for ( int i = ( l ); i <= ( r ); ++ i )
#define rep( i, r, l ) for ( int i = ( r ); i >= ( l ); -- i )

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair < int, int > pii;

namespace IO{
    const int SIZE=1<<21;
  static char ibuf[SIZE],obuf[SIZE],*iS,*iT,*oS=obuf,*oT=oS+SIZE-1;
  int qr;
  char qu[55],c;
  bool f;
    #define getchar() (IO::iS==IO::iT?(IO::iT=(IO::iS=IO::ibuf)+fread(IO::ibuf,1,IO::SIZE,stdin),(IO::iS==IO::iT?EOF:*IO::iS++)):*IO::iS++)
    #define putchar(x) *IO::oS++=x,IO::oS==IO::oT?flush():0
    #define flush() fwrite(IO::obuf,1,IO::oS-IO::obuf,stdout),IO::oS=IO::obuf
    #define puts(x) IO::Puts(x)
    template<typename T>
    inline void read(T&x){
        for(f=1,c=getchar();c<48||c>57;c=getchar())f^=c=='-';
        for(x=0;c<=57&&c>=48;c=getchar()) x=(x<<1)+(x<<3)+(c&15); 
        x=f?x:-x;
    }
    template<typename T>
    inline void write(T x){
      if(!x) putchar(48); if(x<0) putchar('-'),x=-x;
      while(x) qu[++qr]=x%10^48,x/=10;
      while(qr) putchar(qu[qr--]);
    }
    inline void Puts(const char*s){
        for(int i=0;s[i];i++)
            putchar(s[i]);
        putchar('\n');
    }
    struct Flusher_{~Flusher_(){flush();}}io_flusher_;
}
using IO::read;
using IO::write;

template < class type > inline void chkmin ( type &x, type y ) { x = ( x <= y ? x : y ); }
template < class type > inline void chkmax ( type &x, type y ) { x = ( x >= y ? x : y ); }

const int N = 2e6 + 5;
const int mod = 998244353;

int n, ans, shidui = 1, suf, sum = 1;
int g[N], mul[N], cnt[N];

int head[N], tot;

struct Node {
  int to, next;
} edges[N << 1];

void add ( int u, int v ) {
  tot ++;
  edges[tot].to = v;
  edges[tot].next = head[u];
  head[u] = tot;
}

int fast_pow ( int a, int b ) {
  int res = 1;
  while ( b ) {
    if ( b & 1 ) {
      res = res * a;
      res %= mod;
    }
    b >>= 1;
    a = a * a;
    a %= mod;
  }
  return res;
}

void dfs ( int x, int fa ) {
  int son = 0, res = 1;
  mul[x] = 1;
  for ( int i = head[x]; i; i = edges[i].next ) {
    if ( edges[i].to != fa ) {
      dfs ( edges[i].to, x );
      son ++;
      res *= g[edges[i].to], res %= mod;
      g[x] += g[edges[i].to], g[x] %= mod;
      mul[x] *= g[edges[i].to], mul[x] %= mod;
    }
  }
  if ( !son ) {
    g[x] = 1;
  }
  else {
    g[x] += res, g[x] %= mod;
  }
}

void dfs2 ( int x, int fa ) {
  for ( int i = head[x]; i; i = edges[i].next ) {
    if ( edges[i].to != fa ) {
      int tmp = mul[x] * fast_pow ( g[edges[i].to], mod - 2 ) % mod + 1, inv = fast_pow ( tmp, mod - 2 );
      sum *= tmp, sum %= mod;
      dfs2 ( edges[i].to, x );
      sum *= inv, sum %= mod;
    }
  }
  int res = 0;
  res += sum % mod, res %= mod;
  res *= mul[x], res %= mod;
  cnt[x] = res;
}

void Solve () {
  ios :: sync_with_stdio ( false );
  cin.tie ( 0 ), cout.tie ( 0 );
  cin >> n;
  for ( int i = 1; i < n; i ++ ) {
    int u, v;
    cin >> u >> v;
    add ( u, v ), add ( v, u );
  }
  dfs ( 1, 0 );
  dfs2 ( 1, 0 );
  for ( int i = 1; i <= n; i ++ ) {
    cout << ( cnt[i] % mod + mod ) % mod << " ";
  }
}

signed main () {
#ifdef judge
  freopen ( "Code.in", "r", stdin );
  freopen ( "Code.out", "w", stdout );
  freopen ( "Code.err", "w", stderr );
#endif
  Solve ();
    return 0;
}