Back From Summer '19 P5: ABC123E

View as PDF

Points: 17 (partial)
Time limit: 1.0s
Memory limit: 128M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

There is a tree containing nodes that is connected with edges. This tree is rooted at node . A path is defined as a sequence of at least two nodes where consecutive nodes are connected in the tree by edges, and every node occurs at most once. As such, there are exactly paths in the tree. The length of a path is defined as the number of edges in that path.

The heaviness of the tree is defined as the average length of all the paths in the tree. Recall that the average in this case is .

Evan is planning to modify the tree. He will choose two nodes, and , such that and are not in each other's subtree (not ancestors of one another), and ask you:

• If the subtree rooted at node is swapped with the subtree rooted at node (that is, and all of its descendants are moved to the location of , and vice versa), is the heaviness of the resultant tree lower, equal, or higher than the original tree's heaviness?

He will ask of these questions. Note that the questions are not persistent.

Input Specification

The first line will contain two integers, .

The next lines will each contain two integers, , the edges of the tree.

The next lines will each contain two integers, , a question as defined above. and will not be ancestors of one another.

Output Specification

For each question, output -1 if the heaviness in the resultant tree is less than in the original tree, 0 if it is equal, and 1 if it is greater, on its own line.

No further constraints.

Sample Input 1

6 3
1 2
2 3
1 4
4 5
1 6
2 4
3 5
2 5

Sample Output 1

0
0
1

Explanation For Sample 1

The original tree is shown:

The heaviness is .

For the third question, the tree looks like:

The new heaviness is , which is larger than .

Sample Input 2

13 10
1 12
12 13
1 2
2 3
2 4
2 5
2 6
3 7
7 8
8 9
9 10
10 11
7 12
8 12
9 12
10 12
11 12
8 4
7 5
3 6
3 12
3 13

Sample Output 2

0
-1
-1
0
1
-1
-1
0
1
1