102215: [AtCoder]ABC221 F - Diameter set

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

Given is a tree with $N$ vertices. The vertices are numbered $1$, $2$, $\ldots$, $N$, and for each $1\leq i\leq N-1$, the $i$-th edge connects Vertex $U_i$ and Vertex $V_i$. Let $D$ be the diameter of the tree. Find the number, modulo $998244353$, of ways to choose two or more of the vertices and paint them red so that all distances between two red vertices are $D$.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of the tree is the maximum distance between two vertices.

Constraints

  • $2 \leq N \leq 2\times 10^5$
  • $1 \leq U_i,V_i \leq N$
  • $U_i \neq V_i$
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

$N$
$U_1$ $V_1$
$U_2$ $V_2$
$\vdots$
$U_{N-1}$ $V_{N-1}$

Output

Print the answer.


Sample Input 1

5
1 2
1 3
1 4
4 5

Sample Output 1

2

The given tree has five vertices and a diameter of $3$.
There are just two pairs of vertices whose distance is $3$: $(2,5)$ and $(3,5)$, so there are two ways to paint the tree to satisfy the condition: $\lbrace 2,5\rbrace $ and $\lbrace 3,5\rbrace $.
Note that painting $2,3,5$ does not satisfy the condition since the distance between Vertex $2$ and Vertex $3$ is $2$.


Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

4

The diameter is $2$, and the four ways to paint the tree to satisfy the condition are: $\lbrace 2,3\rbrace $, $\lbrace 2,4\rbrace $, $\lbrace 3,4\rbrace $, $\lbrace 2,3,4\rbrace $.

Input

题意翻译

给你一个 $n$ 个顶点的无根树, 记 $d$ 为它的直径. 求使得顶点集合 $S$ 中任意两个顶点的距离为 $d$ 的集合个数, 对 $998244353$ 取模. $S$ 中至少要有两个数. $n \le 2 \times 10^5$.

Output

得分:500分 部分 问题描述 给定一个包含N个顶点的树。顶点编号为1、2、…、N,对于每个1≤i≤N-1,第i条边连接顶点Ui和顶点Vi。令D为树的直径。求按照以下条件将两个或更多顶点涂成红色的方法数(对998244353取模):所有两个红色顶点之间的距离都为D。 这里,两个顶点之间的距离是指从一个顶点到另一个顶点必须经过的最少边数,树的直径是指两个顶点之间的最大距离。 部分 约束条件 * 2≤N≤2×105 * 1≤Ui,Vi≤N * Ui≠Vi * 输入中的所有值都是整数。 * 给定的图是一棵树。 部分 输入 输入格式如下: N U1 V1 U2 V2 ⋯ UN-1 VN-1 部分 输出 输出答案。 部分 样例输入1 5 1 2 1 3 1 4 4 5 部分 样例输出1 2 给定的树有5个顶点和直径为3。距离为3的顶点对只有(2,5)和(3,5),所以有两种满足条件的涂色方法:{2,5}和{3,5}。注意,将2、3、5涂色并不满足条件,因为顶点2和顶点3之间的距离为2。 部分 样例输入2 4 1 2 1 3 1 4 部分 样例输出2 4 直径为2,满足条件的涂色方法有:{2,3}、{2,4}、{3,4}和{2,3,4}。

加入题单

上一题 下一题 算法标签: