F. Random Walk

202 阅读1分钟

You are given a tree consisting of nn vertices and n1n - 1 edges, and each vertex vv has a counter c(v)c(v) assigned to it.

Initially, there is a chip placed at vertex ss and all counters, except c(s)c(s), are set to 00; c(s)c(s) is set to 11.

Your goal is to place the chip at vertex tt. You can achieve it by a series of moves. Suppose right now the chip is placed at the vertex vv. In one move, you do the following:

  1. choose one of neighbors toto of vertex vv uniformly at random (toto is neighbor of vv if and only if there is an edge {v,to}\{v, to\} in the tree);
  2. move the chip to vertex toto and increase c(to)c(to) by 11;

You'll repeat the move above until you reach the vertex tt.

For each vertex vv calculate the expected value of c(v)c(v) modulo 998244353998\,244\,353.

Input

The first line contains three integers nn, ss and tt (2n21052 \le n \le 2 \cdot 10^5; 1s,tn1 \le s, t \le n; sts \neq t) — number of vertices in the tree and the starting and finishing vertices.

Next n1n - 1 lines contain edges of the tree: one edge per line. The ii-th line contains two integers uiu_i and viv_i (1ui,vin1 \le u_i, v_i \le n; uiviu_i \neq v_i), denoting the edge between the nodes uiu_i and viv_i.

It's guaranteed that the given edges form a tree.

Output

Print nn numbers: expected values of c(v)c(v) modulo 998244353998\,244\,353 for each vv from 11 to nn.

Formally, let M=998244353M = 998\,244\,353. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to pq1modMp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0x<M0 \le x < M and xqp(modM)x \cdot q \equiv p \pmod{M}.

Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef vector<vector<int>> VII;
typedef pair<int,int> PII;
ll MOD = 998244353;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// mt19937 mrand(random_device{}()); 
const int N = 2e5 + 10;
void solve()
{
    int n, s, t; cin >> n >> s >> t;
    VII e(n + 1);
    VI sz(n + 1), de(n + 1);
    rep (i, 1, n - 1) {
    	int u, v; cin >> u >> v;
    	e[u].pb(v);
    	e[v].pb(u);
    	sz[u] ++;
    	sz[v] ++;
    }
    VII p(n + 1, VI(23));
    function<void(int, int)> dfs = [&] (int u, int Fa) {
        de[u] = de[Fa] + 1;
        p[u][0] = Fa;
        for (auto v : e[u]) {
            if (v == Fa) continue;
            dfs(v, u);
        }
    };
    dfs(t, 0);
    for (int j = 1; j < 22; j++)
        for (int i = 1; i <= n; i++) {
            p[i][j] = p[p[i][j - 1]][j-1];
        }
    function<ll(int, int)> Lca = [&] (int x, int y) -> ll {
        if (de[x] < de[y]) swap(x, y);
        for (int i = 21; i >= 0; i--) {
            if (de[p[x][i]] >= de[y]) {
                x = p[x][i];
            }
        }
        if (x == y) return x;
        for (int i = 21; i >= 0; i--) {
            if (p[x][i] != p[y][i]) {
                x = p[x][i];
                y = p[y][i];
            }
        }
        return p[x][0];
    };
    rep (i, 1, n) {
    	if (i == t) cout << 1 << ' ';
    	else
    	cout << sz[i] * (de[Lca(s, i)] - 1) % MOD << ' ';
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // int T;cin >> T;
    // while ( T -- )
    solve();
    return 0;
}