CF1823F.Random Walk

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree consisting of nn vertices and n−1n - 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 998 244 353998\,244\,353.

你被给定一棵包含 nn 个顶点和 n−1n - 1 条边的树,且每个顶点 vv 都关联一个计数器 c(v)c(v)。

初始时,一枚棋子被放置在顶点 ss 上,除 c(s)c(s) 被设为 11 外,其余所有计数器均设为 00。

你的目标是将棋子移动至顶点 tt。你可以通过一系列操作实现该目标。假设当前棋子位于顶点 vv,则一次操作按如下步骤进行:

  1. 在顶点 vv 的所有邻居 toto 中均匀随机选择一个(即:当且仅当树中存在边 {v,to}\{v, to\} 时,toto 是 vv 的邻居);
  2. 将棋子移至顶点 toto,并将 c(to)c(to) 的值增加 11;

你将重复上述操作,直至棋子到达顶点 tt。

对每个顶点 vv,计算其计数器 c(v)c(v) 的期望值,并对 998 244 353998\,244\,353 取模。

输入格式

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

Next n−1n - 1 lines contain edges of the tree: one edge per line. The ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n; ui≠viu_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.

第一行包含三个整数 nn、ss 和 tt(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5;1≤s,t≤n1 \le s, t \le n;s≠ts \neq t),分别表示树中顶点的数量、起点和终点。

接下来的 n−1n - 1 行描述树的边:每行一条边。第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n;ui≠viu_i \neq v_i),表示节点 uiu_i 与 viv_i 之间存在一条边。

保证所给的边构成一棵树。

输出格式

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

Formally, let M=998 244 353M = 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 p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

输出 nn 个数:对每个 vv(从 11 到 nn),输出 c(v)c(v) 的期望值对 998 244 353998\,244\,353 取模的结果。

形式化地,令 M=998 244 353M = 998\,244\,353。可以证明答案可表示为既约分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(modM)q \not \equiv 0 \pmod{M}。请输出整数 p⋅q−1 mod Mp \cdot q^{-1} \bmod M。换言之,输出满足 0≤x<M0 \le x < M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的整数 xx。

输入输出样例

  • 输入#1

    3 1 3
    1 2
    2 3

    输出#1

    2 2 1
  • 输入#2

    4 1 3
    1 2
    2 3
    1 4

    输出#2

    4 2 1 2
  • 输入#3

    8 2 6
    6 4
    6 2
    5 4
    3 1
    2 3
    7 4
    8 2

    输出#3

    1 3 2 0 0 1 0 1

说明/提示

The tree from the first example is shown below:

Let's calculate expected value E[c(1)]E[c(1)]:

  • P(c(1)=0)=0P(c(1) = 0) = 0, since c(1)c(1) is set to 11 from the start.
  • P(c(1)=1)=12P(c(1) = 1) = \frac{1}{2}, since there is the only one series of moves that leads c(1)=1c(1) = 1. It's 1→2→31 \rightarrow 2 \rightarrow 3 with probability 1⋅121 \cdot \frac{1}{2}.
  • P(c(1)=2)=14P(c(1) = 2) = \frac{1}{4}: the only path is 1→12→0.51→12→0.531 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 3.
  • P(c(1)=3)=18P(c(1) = 3) = \frac{1}{8}: the only path is 1→12→0.51→12→0.51→12→0.531 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 3.
  • P(c(1)=i)=12iP(c(1) = i) = \frac{1}{2^i} in general case.

As a result, E[c(1)]=∑i=1∞i12i=2E[c(1)] = \sum\limits_{i=1}^{\infty}{i \frac{1}{2^i}} = 2.

Image of tree in second test

Image of tree in third test

第一个示例中的树如下所示:

我们来计算期望值 E[c(1)]E[c(1)]:

  • P(c(1)=0)=0P(c(1) = 0) = 0,因为 c(1)c(1) 从初始时刻起就被设为 11。
  • P(c(1)=1)=12P(c(1) = 1) = \frac{1}{2},因为仅存在一条导致 c(1)=1c(1) = 1 的操作序列:1→2→31 \rightarrow 2 \rightarrow 3,其概率为 1⋅121 \cdot \frac{1}{2}。
  • P(c(1)=2)=14P(c(1) = 2) = \frac{1}{4}:唯一路径为 1→12→0.51→12→0.531 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 3。
  • P(c(1)=3)=18P(c(1) = 3) = \frac{1}{8}:唯一路径为 1→12→0.51→12→0.51→12→0.531 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 1 \rightarrow_{1} 2 \rightarrow_{0.5} 3。
  • 一般情况下,P(c(1)=i)=12iP(c(1) = i) = \frac{1}{2^i}。

因此,E[c(1)]=∑i=1∞i12i=2E[c(1)] = \sum\limits_{i=1}^{\infty}{i \frac{1}{2^i}} = 2。

第二个测试用例中的树图:

第三个测试用例中的树图:

输入解题思路,AI测评打分。不知道怎么写?

首页