A9689 | New Year Running
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
New Year is coming in Tree Island! In this island, as the name implies, there are $n$ cities connected by $n-1$ roads, and for any two distinct cities there always exists exactly one path between them. For every person in Tree Island, it takes exactly one minute to pass by exactly one road.
There is a weird New Year tradition for runnners in Tree Island, which is called "extreme run". This tradition can be done as follows.
A runner chooses two distinct cities $a$ and $b$ . For simplicity, let's denote the shortest path from city $a$ to city $b$ as $p_{1},p_{2},...,p_{l}$ (here, $p_{1}=a$ and $p_{l}=b$ holds). Then following happens:
1. The runner starts at city $a$ .
2. The runner runs from city $a$ to $b$ , following the shortest path from city $a$ to city $b$ .
3. When the runner arrives at city $b$ , he turns his direction immediately (it takes no time), and runs towards city $a$ , following the shortest path from city $b$ to city $a$ .
4. When the runner arrives at city $a$ , he turns his direction immediately (it takes no time), and runs towards city $b$ , following the shortest path from city $a$ to city $b$ .
5. Repeat step 3 and step 4 forever.
In short, the course of the runner can be denoted as: 
Two runners JH and JY decided to run "extremely" in order to celebrate the New Year. JH has chosen two cities $u$ and $v$ , and JY has chosen two cities $x$ and $y$ . They decided to start running at the same moment, and run until they meet at the same city for the first time. Meeting on a road doesn't matter for them. Before running, they want to know the amount of time they will run.
It is too hard for JH and JY to calculate this, so they ask you for help.
There is a weird New Year tradition for runnners in Tree Island, which is called "extreme run". This tradition can be done as follows.
A runner chooses two distinct cities $a$ and $b$ . For simplicity, let's denote the shortest path from city $a$ to city $b$ as $p_{1},p_{2},...,p_{l}$ (here, $p_{1}=a$ and $p_{l}=b$ holds). Then following happens:
1. The runner starts at city $a$ .
2. The runner runs from city $a$ to $b$ , following the shortest path from city $a$ to city $b$ .
3. When the runner arrives at city $b$ , he turns his direction immediately (it takes no time), and runs towards city $a$ , following the shortest path from city $b$ to city $a$ .
4. When the runner arrives at city $a$ , he turns his direction immediately (it takes no time), and runs towards city $b$ , following the shortest path from city $a$ to city $b$ .
5. Repeat step 3 and step 4 forever.
In short, the course of the runner can be denoted as: 
Two runners JH and JY decided to run "extremely" in order to celebrate the New Year. JH has chosen two cities $u$ and $v$ , and JY has chosen two cities $x$ and $y$ . They decided to start running at the same moment, and run until they meet at the same city for the first time. Meeting on a road doesn't matter for them. Before running, they want to know the amount of time they will run.
It is too hard for JH and JY to calculate this, so they ask you for help.
输入格式
The first line contains a single positive integer $n$ ( $5<=n<=2×10^{5}$ ) — the number of cities in Tree Island.
Next $n-1$ lines describe the roads of Tree Island. The $i$ -th line ( $1<=i<=n-1$ ) of them contains two space-separated integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n,a_{i}≠b_{i}$ ) — the vertices connected by a single road of the tree.
The next line contains an integer $t$ ( $1<=t<=2×10^{5}$ ) — the number of test cases.
Next $t$ lines describes the test cases. The $j$ -th line ( $1<=j<=t$ ) of them contains four space-separated integers $u_{j},v_{j},x_{j},y_{j}$ ( $1<=u_{j},v_{j},x_{j},y_{j}<=n,u_{j}≠v_{j},x_{j}≠y_{j}$ ). It means that in this test case, JH has chosen two cities $u_{j}$ and $v_{j}$ , JY has chosen two cities $x_{j}$ and $y_{j}$ . JH starts running at city $u_{j}$ , and JY starts running at city $x_{j}$ .
Next $n-1$ lines describe the roads of Tree Island. The $i$ -th line ( $1<=i<=n-1$ ) of them contains two space-separated integers $a_{i}$ and $b_{i}$ ( $1<=a_{i},b_{i}<=n,a_{i}≠b_{i}$ ) — the vertices connected by a single road of the tree.
The next line contains an integer $t$ ( $1<=t<=2×10^{5}$ ) — the number of test cases.
Next $t$ lines describes the test cases. The $j$ -th line ( $1<=j<=t$ ) of them contains four space-separated integers $u_{j},v_{j},x_{j},y_{j}$ ( $1<=u_{j},v_{j},x_{j},y_{j}<=n,u_{j}≠v_{j},x_{j}≠y_{j}$ ). It means that in this test case, JH has chosen two cities $u_{j}$ and $v_{j}$ , JY has chosen two cities $x_{j}$ and $y_{j}$ . JH starts running at city $u_{j}$ , and JY starts running at city $x_{j}$ .
输出格式
For each test case, print an integer describing the amount of time they should run in minutes. If they have to run for an infinitely long time (in other words, if they never meet at the same city), print -1 instead. If they meet at the beginning of their run, print 0.
输入输出样例
输入 #1
7 1 3 3 6 7 4 3 7 5 4 7 2 4 6 5 5 3 3 5 4 6 1 5 1 3 1 5 3 1
输出 #1
2 1 0 -1
The example looks like:


C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted