Under construction

Still being built. Things may change

Rigged Roads

Time limit2sMemory limit512 MB

Summary
Given a connected graph and a spanning tree R, assign distinct weights 1 to E so that R is the unique MST, with the weight list lexicographically smallest.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree, Union-find, Greedy
Solved
No attempts yet

Problem

Silvermill is on a tight budget lately, and Peanut, its mayor, plans to demolish some roads to save on maintenance costs. Silvermill has NN road intersections and EE roads running between them. The ii-th road connects intersections AiA_i and BiB_i. Intersections are labelled 1,…,N1, \ldots, N and roads are labelled 1,…,E1, \ldots, E. Any two intersections can reach each other directly or indirectly, and no two roads share the same endpoints.

Peanut has hired you to assess the maintenance cost of the roads. Your task is to report a list W=(W1,…,WE)W = (W_1, \ldots, W_E) where WW is a permutation of 1,…,E1, \ldots, E and WiW_i is the cost to keep road ii.

Peanut then keeps a subset of roads such that all intersections remain connected and the total cost of the kept roads is minimal. In other words, Peanut keeps the minimum spanning tree under the weights you choose. The costs are distinct, so the minimum spanning tree is unique.

You have a hidden agenda. You want Peanut to keep a set RR of roads that forms a spanning tree, and you can make that happen by choosing WW carefully. Find the lexicographically smallest permutation WW for which the minimum spanning tree of the city is RR.

(W1,…,WE)(W_1, \ldots, W_E) is smaller than (W1′,…,WE′)(W'_1, \ldots, W'_E) if there is a pp with 1≤p≤E1 \le p \le E such that Wp<Wp′W_p < W'_p and Wi=Wi′W_i = W'_i for all i<pi < p.

Input

Read from standard input. The first line contains two integers NN and EE. The next EE lines contain the two integers AiA_i and BiB_i describing road ii. The last line contains N−1N - 1 integers, the labels of the roads in RR.

Output

Print to standard output. Print EE integers on a single line, the lexicographically smallest permutation WW that makes RR the minimum spanning tree.

Constraints

1≤N,E≤3×1051 \le N, E \le 3 \times 10^5

1≤Ai≠Bi≤N1 \le A_i \ne B_i \le N

1≤Ri≤N1 \le R_i \le N

It is possible to travel between any two road intersections using only roads in RR.

Examples2

  1. Example 1

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

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