You are given a tree consisting of vertices and edges, and each vertex has a counter assigned to it.
Initially, there is a chip placed at vertex and all counters, except , are set to ; is set to .
Your goal is to place the chip at vertex . You can achieve it by a series of moves. Suppose right now the chip is placed at the vertex . In one move, you do the following:
- choose one of neighbors of vertex uniformly at random ( is neighbor of if and only if there is an edge in the tree);
- move the chip to vertex and increase by ;
You'll repeat the move above until you reach the vertex .
For each vertex calculate the expected value of modulo .
Input
The first line contains three integers , and (; ; ) — number of vertices in the tree and the starting and finishing vertices.
Next lines contain edges of the tree: one edge per line. The -th line contains two integers and (; ), denoting the edge between the nodes and .
It's guaranteed that the given edges form a tree.
Output
Print numbers: expected values of modulo for each from to .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output such an integer that and .
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;
}