CF500G.New Year Running

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

New Year is coming in Tree Island! In this island, as the name implies, there are nn cities connected by n1n-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 aa and bb . For simplicity, let's denote the shortest path from city aa to city bb as p1,p2,...,plp_{1},p_{2},...,p_{l} (here, p1=ap_{1}=a and pl=bp_{l}=b holds). Then following happens:

  1. The runner starts at city aa .
  2. The runner runs from city aa to bb , following the shortest path from city aa to city bb .
  3. When the runner arrives at city bb , he turns his direction immediately (it takes no time), and runs towards city aa , following the shortest path from city bb to city aa .
  4. When the runner arrives at city aa , he turns his direction immediately (it takes no time), and runs towards city bb , following the shortest path from city aa to city bb .
  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 uu and vv , and JY has chosen two cities xx and yy . 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 nn ( 5<=n<=2×1055<=n<=2×10^{5} ) — the number of cities in Tree Island.

Next n1n-1 lines describe the roads of Tree Island. The ii -th line ( 1<=i<=n11<=i<=n-1 ) of them contains two space-separated integers aia_{i} and bib_{i} ( 1<=ai,bi<=n,aibi1<=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 tt ( 1<=t<=2×1051<=t<=2×10^{5} ) — the number of test cases.

Next tt lines describes the test cases. The jj -th line ( 1<=j<=t1<=j<=t ) of them contains four space-separated integers uj,vj,xj,yju_{j},v_{j},x_{j},y_{j} ( 1<=uj,vj,xj,yj<=n,ujvj,xjyj1<=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 uju_{j} and vjv_{j} , JY has chosen two cities xjx_{j} and yjy_{j} . JH starts running at city uju_{j} , and JY starts running at city xjx_{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:

首页