Under construction

Still being built. Things may change

Time Travel

Interview

Time limit2sMemory limit512 MB

Summary
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 kk consecutive years. During these years the system of roads of Berland changed. There are nn cities in the country, numbered from 11 to nn. Each year these cities were connected by n−1n-1 bidirectional roads, so that for every pair of cities there was a unique path between them.

Every day the professor chooses two cities ss and ff, then travels through every year of the Great Road Transformation, one after another. In each year he makes a trip from ss to ff, visiting all roads on the path, including ss and ff. After that he writes down the number of cities that were visited on all of his kk 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 kk years of the Great Road Transformation.

Help the professor restore the numbers that he had written down for all possible pairs of cities ss and ff.

Input

The first line contains two integers nn and kk, the number of cities in Berland and the number of years of the Great Road Transformation (1≤n,k≤5001 \leq n, k \leq 500).

Then kk descriptions of the road systems follow, one for each year. Each description has n−1n-1 lines, and each line contains two integers aa and bb, the cities connected by a road (1≤a,b≤n1 \leq a, b \leq n, a≠ba \neq b). It is guaranteed that for each of the kk years, every pair of cities has a unique path between them.

Output

Output nn lines with nn integers in each. The jj-th number in the ii-th line must be the number the professor wrote down if s=is=i and f=jf=j.

Hint

There are 44 cities in Berland in the first sample test. There are 22 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 s=1s = 1 and f=2f = 2. In the first year, the professor visits cities 11, 22, 33, 44 on his trip. In the second year, he visits cities 11, 22, 44. Cities 11, 22, 44 are visited on all of his trips, so he writes down the number 33 for this pair.

Consider the pair s=3s = 3 and f=1f = 1. In the first year, the trip visits cities 11, 33. In the second year, it visits cities 11, 33, 44. Cities 11 and 33 are visited on all trips, so he writes down 22 for this pair.

Examples2

  1. Example 1

    Input
    4 2
    1 3
    4 2
    3 4
    1 4
    4 3
    2 4
    
    Expected output
    1 3 2 2
    3 1 3 2
    2 3 1 2
    2 2 2 1
    
  2. Example 2

    Input
    3 3
    1 2
    2 3
    2 3
    3 1
    3 1
    1 2
    
    Expected output
    1 2 2
    2 1 2
    2 2 1