A11669 | Kuro and Walking Route
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Kuro is living in a country called Uberland, consisting of $n$ towns, numbered from $1$ to $n$ , and $n - 1$ bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns $a$ and $b$ . Kuro loves walking and he is planning to take a walking marathon, in which he will choose a pair of towns $(u, v)$ ( $u \neq v$ ) and walk from $u$ using the shortest path to $v$ (note that $(u, v)$ is considered to be different from $(v, u)$ ).
Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $x$ ) and Beetopia (denoted with the index $y$ ). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns $(u, v)$ if on the path from $u$ to $v$ , he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.
Kuro wants to know how many pair of city $(u, v)$ he can take as his route. Since he’s not really bright, he asked you to help him with this problem.
Oddly, there are 2 special towns in Uberland named Flowrisa (denoted with the index $x$ ) and Beetopia (denoted with the index $y$ ). Flowrisa is a town where there are many strong-scent flowers, and Beetopia is another town where many bees live. In particular, Kuro will avoid any pair of towns $(u, v)$ if on the path from $u$ to $v$ , he reaches Beetopia after he reached Flowrisa, since the bees will be attracted with the flower smell on Kuro’s body and sting him.
Kuro wants to know how many pair of city $(u, v)$ he can take as his route. Since he’s not really bright, he asked you to help him with this problem.
输入格式
The first line contains three integers $n$ , $x$ and $y$ ( $1 \leq n \leq 3 \cdot 10^5$ , $1 \leq x, y \leq n$ , $x \ne y$ ) - the number of towns, index of the town Flowrisa and index of the town Beetopia, respectively.
$n - 1$ lines follow, each line contains two integers $a$ and $b$ ( $1 \leq a, b \leq n$ , $a \ne b$ ), describes a road connecting two towns $a$ and $b$ .
It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.
$n - 1$ lines follow, each line contains two integers $a$ and $b$ ( $1 \leq a, b \leq n$ , $a \ne b$ ), describes a road connecting two towns $a$ and $b$ .
It is guaranteed that from each town, we can reach every other town in the city using the given roads. That is, the given map of towns and roads is a tree.
输出格式
A single integer resembles the number of pair of towns $(u, v)$ that Kuro can use as his walking route.
输入输出样例
输入 #1
3 1 3 1 2 2 3
输出 #1
5
输入 #2
3 1 3 1 2 1 3
输出 #2
4
On the first example, Kuro can choose these pairs:
- $(1, 2)$ : his route would be $1 \rightarrow 2$ ,
- $(2, 3)$ : his route would be $2 \rightarrow 3$ ,
- $(3, 2)$ : his route would be $3 \rightarrow 2$ ,
- $(2, 1)$ : his route would be $2 \rightarrow 1$ ,
- $(3, 1)$ : his route would be $3 \rightarrow 2 \rightarrow 1$ .
Kuro can't choose pair $(1, 3)$ since his walking route would be $1 \rightarrow 2 \rightarrow 3$ , in which Kuro visits town $1$ (Flowrisa) and then visits town $3$ (Beetopia), which is not allowed (note that pair $(3, 1)$ is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).
On the second example, Kuro can choose the following pairs:
- $(1, 2)$ : his route would be $1 \rightarrow 2$ ,
- $(2, 1)$ : his route would be $2 \rightarrow 1$ ,
- $(3, 2)$ : his route would be $3 \rightarrow 1 \rightarrow 2$ ,
- $(3, 1)$ : his route would be $3 \rightarrow 1$ .
- $(1, 2)$ : his route would be $1 \rightarrow 2$ ,
- $(2, 3)$ : his route would be $2 \rightarrow 3$ ,
- $(3, 2)$ : his route would be $3 \rightarrow 2$ ,
- $(2, 1)$ : his route would be $2 \rightarrow 1$ ,
- $(3, 1)$ : his route would be $3 \rightarrow 2 \rightarrow 1$ .
Kuro can't choose pair $(1, 3)$ since his walking route would be $1 \rightarrow 2 \rightarrow 3$ , in which Kuro visits town $1$ (Flowrisa) and then visits town $3$ (Beetopia), which is not allowed (note that pair $(3, 1)$ is still allowed because although Kuro visited Flowrisa and Beetopia, he did not visit them in that order).
On the second example, Kuro can choose the following pairs:
- $(1, 2)$ : his route would be $1 \rightarrow 2$ ,
- $(2, 1)$ : his route would be $2 \rightarrow 1$ ,
- $(3, 2)$ : his route would be $3 \rightarrow 1 \rightarrow 2$ ,
- $(3, 1)$ : his route would be $3 \rightarrow 1$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted