A9824 | A and B and Lecture Rooms
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
A and B are preparing themselves for programming contests.
The University where A and B study is a set of rooms connected by corridors. Overall, the University has $n$ rooms connected by $n-1$ corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from $1$ to $n$ .
Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.
As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following $m$ days.
The University where A and B study is a set of rooms connected by corridors. Overall, the University has $n$ rooms connected by $n-1$ corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from $1$ to $n$ .
Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.
As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following $m$ days.
输入格式
The first line contains integer $n$ ( $1<=n<=10^{5}$ ) — the number of rooms in the University.
The next $n-1$ lines describe the corridors. The $i$ -th of these lines ( $1<=i<=n-1$ ) contains two integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n$ ), showing that the $i$ -th corridor connects rooms $a_{i}$ and $b_{i}$ .
The next line contains integer $m$ ( $1<=m<=10^{5}$ ) — the number of queries.
Next $m$ lines describe the queries. The $j$ -th of these lines ( $1<=j<=m$ ) contains two integers $x_{j}$ and $y_{j}$ ( $1<=x_{j},y_{j}<=n$ ) that means that on the $j$ -th day A will write the contest in the room $x_{j}$ , B will write in the room $y_{j}$ .
The next $n-1$ lines describe the corridors. The $i$ -th of these lines ( $1<=i<=n-1$ ) contains two integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n$ ), showing that the $i$ -th corridor connects rooms $a_{i}$ and $b_{i}$ .
The next line contains integer $m$ ( $1<=m<=10^{5}$ ) — the number of queries.
Next $m$ lines describe the queries. The $j$ -th of these lines ( $1<=j<=m$ ) contains two integers $x_{j}$ and $y_{j}$ ( $1<=x_{j},y_{j}<=n$ ) that means that on the $j$ -th day A will write the contest in the room $x_{j}$ , B will write in the room $y_{j}$ .
输出格式
In the $i$ -th ( $1<=i<=m$ ) line print the number of rooms that are equidistant from the rooms where A and B write contest on the $i$ -th day.
输入输出样例
输入 #1
4 1 2 1 3 2 4 1 2 3
输出 #1
1
输入 #2
4 1 2 2 3 2 4 2 1 2 1 3
输出 #2
0 2
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted