CF500G.New Year Running
普及/提高-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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:
- The runner starts at city a.
- The runner runs from city a to b, following the shortest path from city a to city b.
- 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.
- 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.
- 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.
新年即将来到树岛!顾名思义,该岛上共有 $ n $ 座城市,由 $ n-1 $ 条道路连接;且任意两座不同城市之间,恰好存在唯一一条路径。对于树岛上的每个人而言,恰好花费一分钟通过恰好一条道路。
树岛有一种奇特的新年跑步传统,称为“极限跑”(extreme run)。其执行方式如下:
一名跑步者选择两座不同的城市 $ a $ 和 $ b $。为简化起见,记从城市 $ a $ 到城市 $ b $ 的最短路径为 $ p_1,,p_2,,\dots,,p_l $(其中满足 $ p_1 = a $ 且 $ p_l = b $)。随后发生以下过程:
- 跑步者从城市 $ a $ 出发;
- 跑步者沿从 $ a $ 到 $ b $ 的最短路径,从城市 $ a $ 跑向城市 $ b $;
- 当跑步者抵达城市 $ b $ 时,他立即掉头(不耗时间),并沿从 $ b $ 到 $ a $ 的最短路径,跑向城市 $ a $;
- 当跑步者抵达城市 $ a $ 时,他再次立即掉头(不耗时间),并沿从 $ a $ 到 $ b $ 的最短路径,跑向城市 $ b $;
- 不断重复步骤 3 和步骤 4。
简言之,该跑步者的运动轨迹可表示为:


两名跑步者 JH 和 JY 决定以“极限跑”的方式庆祝新年。JH 选择了两座城市 $ u $ 和 $ v $,JY 选择了两座城市 $ x $ 和 $ y $。他们决定同时起跑,并持续奔跑,直至首次在同一个城市相遇为止(在道路上相遇不计为有效相遇)。起跑前,他们希望知道此次奔跑将持续多长时间。
JH 和 JY 自己难以计算这一时间,因此请求你的帮助。
输入格式
The first line contains a single positive integer n (5 ≤ n ≤ 2 × 105) — 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 × 105) — 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.
第一行包含一个正整数 n(5≤n≤2×105)—— 表示树岛上的城市数量。
接下来的 n−1 行描述树岛上的道路。其中第 i 行(1≤i≤n−1)包含两个以空格分隔的整数 ai 和 bi(1≤ai,bi≤n,且 ai=bi)—— 表示树中一条道路所连接的两个顶点。
下一行包含一个整数 t(1≤t≤2×105)—— 表示测试用例的数量。
接下来的 t 行描述各个测试用例。其中第 j 行(1≤j≤t)包含四个以空格分隔的整数 uj,vj,xj,yj(1≤uj,vj,xj,yj≤n,且 uj=vj,xj=yj)。这表示在该测试用例中,JH 选择了两座城市 uj 和 vj,JY 选择了两座城市 xj 和 yj。JH 从城市 uj 出发开始奔跑,JY 从城市 xj 出发开始奔跑。
输出格式
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。如果他们在跑步开始时就相遇,则输出 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:

示例看起来如下:

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