CF1254D.Tree Queries

省选/NOI-

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Hanh is a famous biologist. He loves growing trees and doing experiments on his own garden.

One day, he got a tree consisting of nn vertices. Vertices are numbered from 11 to nn. A tree with nn vertices is an undirected connected graph with n−1n-1 edges. Initially, Hanh sets the value of every vertex to 00.

Now, Hanh performs qq operations, each is either of the following types:

  • Type 11: Hanh selects a vertex vv and an integer dd. Then he chooses some vertex rr uniformly at random, lists all vertices uu such that the path from rr to uu passes through vv. Hanh then increases the value of all such vertices uu by dd.
  • Type 22: Hanh selects a vertex vv and calculates the expected value of vv.

Since Hanh is good at biology but not math, he needs your help on these operations.

韩是一位著名的生物学家。他热爱种植树木,并在自己的花园里进行各种实验。

一天,他得到了一棵包含 nn 个顶点的树。顶点编号为 11 到 nn。一个含 nn 个顶点的树是一个具有 n−1n-1 条边的无向连通图。初始时,韩将每个顶点的值设为 00。

现在,韩执行 qq 次操作,每次操作为以下两种类型之一:

  • 类型 11:韩选择一个顶点 vv 和一个整数 dd。然后他均匀随机地选择某个顶点 rr,并列出所有满足“从 rr 到 uu 的路径经过 vv”的顶点 uu。接着,韩将所有这样的顶点 uu 的值增加 dd。
  • 类型 22:韩选择一个顶点 vv,并计算其期望值。

由于韩擅长生物学但不擅长数学,他需要你来协助完成这些操作。

输入格式

The first line contains two integers nn and qq (1≤n,q≤150 0001 \leq n, q \leq 150\,000) — the number of vertices on Hanh's tree and the number of operations he performs.

Each of the next n−1n - 1 lines contains two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n), denoting that there is an edge connecting two vertices uu and vv. It is guaranteed that these n−1n - 1 edges form a tree.

Each of the last qq lines describes an operation in either formats:

  • 11 vv dd (1≤v≤n,0≤d≤1071 \leq v \leq n, 0 \leq d \leq 10^7), representing a first-type operation.
  • 22 vv (1≤v≤n1 \leq v \leq n), representing a second-type operation.

It is guaranteed that there is at least one query of the second type.

第一行包含两个整数 nn 和 qq(1≤n,q≤150 0001 \leq n, q \leq 150\,000),分别表示韩的树上的顶点数以及他执行的操作次数。

接下来的 n−1n - 1 行,每行包含两个整数 uu 和 vv(1≤u,v≤n1 \leq u, v \leq n),表示顶点 uu 和 vv 之间存在一条边。保证这 n−1n - 1 条边构成一棵树。

最后的 qq 行,每行描述一个操作,格式为以下两种之一:

  • 11 vv dd(1≤v≤n, 0≤d≤1071 \leq v \leq n,\ 0 \leq d \leq 10^7),表示第一类操作;
  • 22 vv(1≤v≤n1 \leq v \leq n),表示第二类操作。

保证至少存在一个第二类查询。

输出格式

For each operation of the second type, write the expected value on a single line.

Let M=998244353M = 998244353, it can be shown that the expected value 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}.

对于每个第二类操作,在单独一行中输出其期望值。

令 M=998244353M = 998244353,可以证明该期望值可表示为既约分数 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

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

    输出#1

    1
    199648871
    399297742
    199648871
    199648871
    598946614
    199648873
    2
    2
    2

说明/提示

The image below shows the tree in the example:

For the first query, where v=1v = 1 and d=1d = 1:

  • If r=1r = 1, the values of all vertices get increased.
  • If r=2r = 2, the values of vertices 11 and 33 get increased.
  • If r=3r = 3, the values of vertices 11, 22, 44 and 55 get increased.
  • If r=4r = 4, the values of vertices 11 and 33 get increased.
  • If r=5r = 5, the values of vertices 11 and 33 get increased.

Hence, the expected values of all vertices after this query are (1,0.4,0.8,0.4,0.41, 0.4, 0.8, 0.4, 0.4).

For the second query, where v=2v = 2 and d=2d = 2:

  • If r=1r = 1, the values of vertices 22, 44 and 55 get increased.
  • If r=2r = 2, the values of all vertices get increased.
  • If r=3r = 3, the values of vertices 22, 44 and 55 get increased.
  • If r=4r = 4, the values of vertices 11, 22, 33 and 55 get increased.
  • If r=5r = 5, the values of vertices 11, 22, 33 and 44 get increased.

Hence, the expected values of all vertices after this query are (2.2,2.4,2,2,22.2, 2.4, 2, 2, 2).

下图展示了示例中的树:

对于第一个查询(v=1v = 1,d=1d = 1):

  • 若 r=1r = 1,所有顶点的值均增加;
  • 若 r=2r = 2,顶点 11 和 33 的值增加;
  • 若 r=3r = 3,顶点 11、22、44 和 55 的值增加;
  • 若 r=4r = 4,顶点 11 和 33 的值增加;
  • 若 r=5r = 5,顶点 11 和 33 的值增加。

因此,该查询后各顶点的期望值为 (1, 0.4, 0.8, 0.4, 0.4)(1,\ 0.4,\ 0.8,\ 0.4,\ 0.4)。

对于第二个查询(v=2v = 2,d=2d = 2):

  • 若 r=1r = 1,顶点 22、44 和 55 的值增加;
  • 若 r=2r = 2,所有顶点的值均增加;
  • 若 r=3r = 3,顶点 22、44 和 55 的值增加;
  • 若 r=4r = 4,顶点 11、22、33 和 55 的值增加;
  • 若 r=5r = 5,顶点 11、22、33 和 44 的值增加。

因此,该查询后各顶点的期望值为 (2.2, 2.4, 2, 2, 2)(2.2,\ 2.4,\ 2,\ 2,\ 2)。

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

首页