This page is still under construction.

Parts of this page are still being built. What you see may change.

Commuter trains do not change tracks

Time limit1sMemory limit1024 MB

Summary
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 NN stations and MM 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.

4 stations, 3 segments: 1-4, 2-4, 3-4Routes: 1-4-2, 2-4-3, 3-4-1.Answer: no solution
5 stations, 4 segments: 1-5, 2-5, 3-5, 4-5Routes: 1-5-2, 2-5-3, 3-5-4, 4-5-1.Answer: a solution exists; for example, the following one:station number: 1 2 3 4 5tariff number: 1 4 1 5 3Note: tariff numbers of different stations may coincide.

Input

The first line of the input file contains two integers: NN, the number of stations (2≤N≤100 000)(2 \le N \le 100\,000), and MM, the number of segments between them (1≤M≤N−1)(1 \le M \le N - 1). The following MM lines contain pairs of integers a,ba, b (a≠b,1≤a≤N,1≤b≤N)(a \ne b, 1 \le a \le N, 1 \le b \le N), meaning there is a segment between stations aa and bb. After them, on a separate line, a single positive integer KK is given, the number of train routes. The following KK 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 NN positive integers, where the ii-th number is the tariff number of the ii-th station. The tariff number of each station must be in the range from 1 to NN.

If several solutions exist, print any one of them.

Examples2

  1. Example 1

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

    Input
    5 4
    1 5
    2 5
    3 5
    4 5
    4
    1 5 2 0
    2 5 3 0
    3 5 4 0
    4 5 1 0
    
    Expected output
    YES
    1 4 1 5 3