Commuter trains do not change tracks
Time limit1sMemory limit1024 MB
Given a tree and a set of simple paths, assign each vertex a value in 1..N so that values strictly increase along every path; report NO if impossible.
- Level
Hard8 of 10
- Topics
- Graph, DFS, Topological sort, Implementation
- Solved
- No attempts yet
Problem
A suburban railway has stations and segments connecting them. Any two stations are connected by at most one segment. The segment network is arranged so that, starting from any station, you can return to it only by traveling at least one segment twice. Commuter trains run on the railway. Each train travels in both directions along its own route between two terminal stations and stops at every intermediate station.
For the convenience of passengers, the railway management decided to introduce a new fare system. Under this system, each station is assigned an integer called its tariff number. The fare between two stations without transfers is determined by the absolute difference of the tariff numbers of those stations. The tariff numbers of stations along the route of each train must change monotonically, that is, strictly increase when moving in one direction and therefore strictly decrease when moving in the opposite direction. This ensures that the fare grows as the number of segments traveled increases.
Write a program that assigns a tariff number to each station.
Input
The first line of the input file contains two integers: , the number of stations , and , the number of segments between them . The following lines contain pairs of integers , meaning there is a segment between stations and . After them, on a separate line, a single positive integer is given, the number of train routes. The following lines contain descriptions of train routes, one per line. Each description is a sequence of integers, the numbers of all stations of the route in the order of one of the two possible directions of travel. A route description ends with the number 0.
All station numbers in a route description are distinct. The number of stations in each route is at least two. Any two consecutive stations in the route of each train are connected by a segment. The total number of stations in the descriptions of all routes does not exceed 200,000. There may be stations and segments that no train passes through.
Output
In the first line of the output file, print "NO" if the required assignment of tariff numbers does not exist. Otherwise, in the first line print "YES", and in the next line print positive integers, where the -th number is the tariff number of the -th station. The tariff number of each station must be in the range from 1 to .
If several solutions exist, print any one of them.

