Time Travel
InterviewTime limit2sMemory limit512 MB
Given k trees on n cities, count for each ordered pair (s, f) how many cities lie on the s-to-f path in every tree.
- Level
Medium5 of 10
- Topics
- Tree, Bit manipulation, BFS
- Solved
- No attempts yet
Problem
In the future, after the time machine was invented, it became easier to learn history. Now you can just go to the corresponding year and watch the events yourself. A professor uses this device to study the system of Berland roads during the Great Road Transformation.
The transformation took consecutive years. During these years the system of roads of Berland changed. There are cities in the country, numbered from to . Each year these cities were connected by bidirectional roads, so that for every pair of cities there was a unique path between them.
Every day the professor chooses two cities and , then travels through every year of the Great Road Transformation, one after another. In each year he makes a trip from to , visiting all roads on the path, including and . After that he writes down the number of cities that were visited on all of his trips.
Unfortunately, after he finished his work and studied all pairs of cities, he lost all of his records. He only has maps of the Berland road system for all years of the Great Road Transformation.
Help the professor restore the numbers that he had written down for all possible pairs of cities and .
Input
The first line contains two integers and , the number of cities in Berland and the number of years of the Great Road Transformation ().
Then descriptions of the road systems follow, one for each year. Each description has lines, and each line contains two integers and , the cities connected by a road (, ). It is guaranteed that for each of the years, every pair of cities has a unique path between them.
Output
Output lines with integers in each. The -th number in the -th line must be the number the professor wrote down if and .
Hint
There are cities in Berland in the first sample test. There are years studied by the professor. The map of the road system for the first year is shown on the left, and the map for the second year is shown on the right.

Consider the pair and . In the first year, the professor visits cities , , , on his trip. In the second year, he visits cities , , . Cities , , are visited on all of his trips, so he writes down the number for this pair.
Consider the pair and . In the first year, the trip visits cities , . In the second year, it visits cities , , . Cities and are visited on all trips, so he writes down for this pair.