Two Trees
Time limit8sMemory limit256 MB
Given two trees on the same n vertices, compute the sum over all vertex pairs of the squared sum of their distances in both trees, modulo 2^32.
- Level
Hard8 of 10
- Topics
- Tree, Divide and conquer, Math
- Solved
- No attempts yet
Problem
Trees and are given. Each tree has vertices numbered from through . Let be the number of edges on the path between vertices and in tree . Compute the following sum.
The answer may be large, so print it modulo .
Input
The first line contains one integer , the number of vertices in each tree (). Each of the next lines contains two integers and , an edge between and in (). Each of the last lines contains two integers and , an edge between and in ().
Output
Print the answer modulo .