Unique Cities
Time limit2sMemory limit512 MB
For each city, count the distinct specialties among the unique cities, those whose distance from it differs from every other city's distance to it.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, DFS
- Solved
- No attempts yet
Problem
JOI Country has cities, numbered from through . The cities are connected by roads. The -th road () connects cities and in both directions. From any city to any other city, you can travel using some roads.
JOI Country has some local specialties. Each kind of specialty is assigned an integer from to , inclusive. Some integers may not correspond to any specialty in JOI Country. Each city produces one kind of specialty. City () produces specialty . Multiple cities may produce the same kind of specialty.
The distance between two cities is the minimum number of roads needed to travel from one to the other. For city (), city (, ) is a unique city if, for every city (, , ), the distance between and differs from the distance between and .
Mr. K, the Minister of Transport in JOI Country, wants to know, for each (), the number of kinds of specialties produced in the unique cities for city . Write a program that calculates this value for every city.
Input
The first line contains and . Each of the next lines contains and . The last line contains integers , separated by spaces.
Output
Print lines. The -th line contains the number of kinds of specialties produced in the unique cities for city .
Constraints
- (), ()
- ()
- From any city to any other city, you can travel using some roads.
- ()