CF1770E.Koxia and Tree

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Imi has an undirected tree with nn vertices where edges are numbered from 11 to n−1n-1. The ii-th edge connects vertices uiu_i and viv_i. There are also kk butterflies on the tree. Initially, the ii-th butterfly is on vertex aia_i. All values of aa are pairwise distinct.

Koxia plays a game as follows:

  • For i=1,2,…,n−1i = 1, 2, \dots, n - 1, Koxia set the direction of the ii-th edge as ui→viu_i \rightarrow v_i or vi→uiv_i \rightarrow u_i with equal probability.
  • For i=1,2,…,n−1i = 1, 2, \dots, n - 1, if a butterfly is on the initial vertex of ii-th edge and there is no butterfly on the terminal vertex, then this butterfly flies to the terminal vertex. Note that operations are sequentially in order of 1,2,…,n−11, 2, \dots, n - 1 instead of simultaneously.
  • Koxia chooses two butterflies from the kk butterflies with equal probability from all possible k(k−1)2\frac{k(k-1)}{2} ways to select two butterflies, then she takes the distance†^\dagger between the two chosen vertices as her score.

Now, Koxia wants you to find the expected value of her score, modulo 998 244 353‡998\,244\,353^\ddagger.

†^\dagger The distance between two vertices on a tree is the number of edges on the (unique) simple path between them.

‡^\ddagger 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 个顶点的无向树,其边编号为 11 到 n−1n-1。第 ii 条边连接顶点 uiu_i 和 viv_i。树上还有 kk 只蝴蝶。初始时,第 ii 只蝴蝶位于顶点 aia_i。所有 aia_i 的值两两不同。

小霞进行如下游戏:

  • 对于 i=1,2,…,n−1i = 1, 2, \dots, n - 1,小霞以相等概率将第 ii 条边定向为 ui→viu_i \rightarrow v_i 或 vi→uiv_i \rightarrow u_i;
  • 对于 i=1,2,…,n−1i = 1, 2, \dots, n - 1,若某只蝴蝶位于第 ii 条边的起点,且终点处没有蝴蝶,则该蝴蝶飞向终点。注意:这些操作按顺序依次执行(即先处理边 11,再边 22,……,最后边 n−1n-1),而非同时进行;
  • 小霞从 kk 只蝴蝶中等概率地随机选出两只(共 k(k−1)2\frac{k(k-1)}{2} 种选法),并将这两只蝴蝶所在顶点之间的距离†^\dagger 作为她的得分。

现在,小霞希望你计算她得分的期望值,并对 998 244 353‡998\,244\,353^\ddagger 取模。

†^\dagger 树上两个顶点之间的距离定义为它们之间(唯一)简单路径上的边数。

‡^\ddagger 形式化地,令 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 的整数。换言之,输出唯一整数 xx,使得 0≤x<M0 \le x < M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}。

输入格式

The first line contains two integers nn, kk (2≤k≤n≤3⋅1052 \leq k \leq n \leq 3 \cdot {10}^5) — the size of the tree and the number of butterflies.

The second line contains kk integers a1,a2,…,aka_1, a_2, \dots, a_k (1≤ai≤n1 \leq a_i \leq n) — the initial position of butterflies. It's guaranteed that all positions are distinct.

The ii-th line in following n−1n − 1 lines contains two integers uiu_i, viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i \neq v_i) — the vertices the ii-th edge connects.

It is guaranteed that the given edges form a tree.

第一行包含两个整数 nn、kk(2≤k≤n≤3⋅1052 \leq k \leq n \leq 3 \cdot {10}^5)—— 分别表示树的大小和蝴蝶的数量。

第二行包含 kk 个整数 a1,a2,…,aka_1, a_2, \dots, a_k(1≤ai≤n1 \leq a_i \leq n)—— 表示蝴蝶的初始位置。保证所有位置互不相同。

接下来的 n−1n - 1 行中,第 ii 行包含两个整数 uiu_i、viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,ui≠viu_i \neq v_i)—— 表示第 ii 条边所连接的两个顶点。

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

输出格式

Output a single integer — the expected value of Koxia's score, modulo 998 244 353998\,244\,353.

输出一个整数——Koxia 得分的期望值对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

    3 2
    1 3
    1 2
    2 3

    输出#1

    748683266
  • 输入#2

    5 3
    3 4 5
    1 2
    1 3
    2 4
    2 5

    输出#2

    831870296

说明/提示

In the first test case, the tree is shown below. Vertices containing butterflies are noted as bold.

There are only 22 butterflies so the choice of butterflies is fixed. Let's consider the following 44 cases:

  • Edges are 1→21 \rightarrow 2 and 2→32 \rightarrow 3: butterfly on vertex 11 moves to vertex 22, but butterfly on vertex 33 doesn't move. The distance between vertices 22 and 33 is 11.
  • Edges are 1→21 \rightarrow 2 and 3→23 \rightarrow 2: butterfly on vertex 11 moves to vertex 22, but butterfly on vertex 33 can't move to vertex 22 because it's occupied. The distance between vertices 22 and 33 is 11.
  • Edges are 2→12 \rightarrow 1 and 2→32 \rightarrow 3: butterflies on both vertex 11 and vertex 33 don't move. The distance between vertices 11 and 33 is 22.
  • Edges are 2→12 \rightarrow 1 and 3→23 \rightarrow 2: butterfly on vertex 11 doesn't move, but butterfly on vertex 33 move to vertex 22. The distance between vertices 11 and 22 is 11.

Therefore, the expected value of Koxia's score is 1+1+2+14=54\frac {1+1+2+1} {4} = \frac {5} {4}, which is 748 683 266748\,683\,266 after modulo 998 244 353998\,244\,353.

In the second test case, the tree is shown below. Vertices containing butterflies are noted as bold. The expected value of Koxia's score is 116\frac {11} {6}, which is 831 870 296831\,870\,296 after modulo 998 244 353998\,244\,353.

在第一个测试用例中,树的结构如下图所示。包含蝴蝶的顶点以粗体标出。

共有且仅有 22 只蝴蝶,因此蝴蝶的位置选择是固定的。我们考虑以下 44 种情况:

  • 边为 1→21 \rightarrow 2 和 2→32 \rightarrow 3:位于顶点 11 的蝴蝶移动到顶点 22,而位于顶点 33 的蝴蝶不移动。此时顶点 22 与顶点 33 之间的距离为 11。
  • 边为 1→21 \rightarrow 2 和 3→23 \rightarrow 2:位于顶点 11 的蝴蝶移动到顶点 22,但位于顶点 33 的蝴蝶无法移动到顶点 22(因为该顶点已被占据)。此时顶点 22 与顶点 33 之间的距离为 11。
  • 边为 2→12 \rightarrow 1 和 2→32 \rightarrow 3:位于顶点 11 和顶点 33 的蝴蝶均不移动。此时顶点 11 与顶点 33 之间的距离为 22。
  • 边为 2→12 \rightarrow 1 和 3→23 \rightarrow 2:位于顶点 11 的蝴蝶不移动,而位于顶点 33 的蝴蝶移动到顶点 22。此时顶点 11 与顶点 22 之间的距离为 11。

因此,Koxia 得分的期望值为 1+1+2+14=54\frac {1+1+2+1} {4} = \frac {5} {4},对 998 244 353998\,244\,353 取模后结果为 748 683 266748\,683\,266。

在第二个测试用例中,树的结构如下图所示。包含蝴蝶的顶点以粗体标出。Koxia 得分的期望值为 116\frac {11} {6},对 998 244 353998\,244\,353 取模后结果为 831 870 296831\,870\,296。

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

首页