Evidence_Board — OOI final
Task type: batch | Time limit: 2 s | Memory limit: 512 MB | Difficulty: 22
Algorithms: queue, greedy, graph theory, union-find

# Evidence Board

| Input file:   | standard input  |
|---------------|-----------------|
| Output file:  | standard output |
| Time limit:   | 2 seconds       |
| Memory limit: | 512 megabytes   |

Volodya dreams of becoming a detective. Therefore, Volodya often reads books that tell incredible stories of solving crimes. Studying the next case, Volodya came across interesting details of the investigation.

There were a total of *n* suspected persons in the case. The evidence board contains all *n* persons. Initially, there were no connections between them.

During the investigation, new connections between suspected persons emerged one after another. Each connection linked two persons that previously had no connection with each other, even indirectly through several other persons.

Let’s consider what happened when a connection between persons A and B emerged. In addition to the names of the persons, each connection had three parameters: *c<sub>A</sub>* — the strength of the evidence against A, *c<sub>B</sub>* — the strength of the evidence against B, and *w<sub>AB</sub>* — the total strength of the evidence of connection. For natural reasons, the strength of the evidence of connection could not exceed the sum of strengths of the evidence against A and B. That means that for each connection, it was necessarily that *w<sub>AB</sub> ≤ c<sub>A</sub> + c<sub>B</sub>*. Upon receiving such connection, the detectives drew a line on the board between the images of persons A and B, assigning the *w<sub>AB</sub>* to this line. Also, a sticker with the number *c<sub>A</sub>* was placed on the image of person A, and a sticker with the number *c<sub>B</sub>* was placed on B. If there were already other stickers on the image, the new sticker was placed on top of the old ones.

The case was solved exactly at the moment when all the suspected persons were linked through *n* − 1 connections. After solving the crime, the board was placed in the museum in its original form.

Inspired by this approach, Volodya visited that museum and studied the evidence board in detail. Volodya noticed that the image of person *v* contained stickers with numbers *c<sub>v,1</sub>, ..., c<sub>v,deg<sub>v</sub></sub>* numbered from top to bottom. Here, *deg<sub>v</sub>* denotes the number of connections associated with person *v*. Also, Volodya remembered that the *i*-th connection was between persons *a<sub>i</sub>* and *b<sub>i</sub>* and had evidence strength *w<sub>i</sub>*. Unfortunately the connections were arbitrarily numbered, and their numbers did not necessarily correspond to the order in which they appeared during the investigation.

Due to the confusion with the numbers of connections, the information on the board did not help to restore the process of the investigation. Now Volodya needs to restore any possible chronological order in which the connections could have emerged for the detectives. This task is too difficult for him, so he asks your help. It is also possible that the museum falsified information, and a suitable order does not exist.

#### Input

The first line of the input contains two integers *n* and *g* (2 ≤ *n* ≤ 200 000, 0 ≤ *g* ≤ 9) — the number of suspected persons in the case and the test group number.

The next *n* − 1 lines describe the connections. The *i*-th line contains three integers *a<sub>i</sub>*, *b<sub>i</sub>*, and *w<sub>i</sub>* (1 ≤ *a<sub>i</sub>*, *b<sub>i</sub>* ≤ *n*, 1 ≤ *w<sub>i</sub>* ≤ 10<sup>9</sup>, *a<sub>i</sub>* ≠ *b<sub>i</sub>*) — the persons connected by the *i*-th connection and the total strength of the *i*-th connection. It is guaranteed that connections link all persons together.

The next *n* lines describe the numbers written on the stickers. The *i*-th line contains *deg<sub>i</sub>* integers *c<sub>i,1</sub>, ..., c<sub>i,deg<sub>i</sub></sub>* (0 ≤ *c<sub>i,j</sub>* ≤ 10<sup>9</sup>) — the numbers written on the stickers on the image of the *i*-th person from top to bottom. *deg<sub>i</sub>* equals the number of connections associated with person *i*.

## Output

If there is no suitable chronological order for the restoration of connections according to the conditions of the problem, output “No” (without quotes) on a single line.

Otherwise, on the first line output “Yes” (without quotes). On the second line, output *n* − 1 numbers — a suitable chronological order of connections to emerge. The connections are numbered from 1 to *n* − 1 in the same order as they are given in the input. If there are multiple possible orders, output any of them.

## Examples

| standard input | standard output |
|---|---|
| 5 0  1 2 3  1 3 1  3 4 12  3 5 6  0 4  2  6 1 3  8  3 | Yes  1 4 2 3 |
| 7 0  1 2 4  2 3 4  3 4 4  4 5 4  5 6 4  6 7 4  2  1 2  2 3  1 2  3 2  1 2  179 | Yes  5 1 2 3 6 4 |
| 4 0  1 2 7  1 3 6  1 4 5  3 2 1  5  4  3 | No |

#### Note

In the first example, one of the possible orders is [1, 4, 2, 3]. In chronological order, the first connection links A = 1 and B = 2, *c<sub>A</sub>* = 4, *c<sub>B</sub>* = 2, *w<sub>AB</sub>* = 3, 3 ≤ 2 + 4 — the evidence is correct. The second connection links A = 3 and B = 5, *c<sub>A</sub>* = 3, *c<sub>B</sub>* = 3, *w<sub>AB</sub>* = 6, 6 ≤ 3 + 3 — the evidence is correct. The third connection links A = 1 and B = 3, *c<sub>A</sub>* = 0, *c<sub>B</sub>* = 1, *w<sub>AB</sub>* = 1, 1 ≤ 0 + 1 — the evidence is correct. The fourth connection links A = 3 and B = 4, *c<sub>A</sub>* = 6, *c<sub>B</sub>* = 8, *w<sub>AB</sub>* = 12, 12 ≤ 6 + 8 – the evidence is correct. For a better understanding, refer to the illustration.

![](_page_2_Figure_0.jpeg)

## Scoring

The tests for this problem consist of nine groups. Points for each group are given only if all tests of the group and all tests of the required groups are passed. Please note that passing the example tests is not required for some groups. Offline-evaluation means that the results of testing your solution on this group will only be available after the end of the competition.

| Group | Points | Additional constraints |   | Required Groups | Comment |
|---|---|---|---|---|---|
|   |   | *n* | *a<sub>i</sub>*, *b<sub>i</sub>*, *c<sub>i</sub>*, *w<sub>i</sub>* |   |   |
| 0 | 0 | – | – | – | Examples. |
| 1 | 10 | *n* ≤ 10 | – | 0 | – |
| 2 | 15 | – | *a<sub>i</sub>* = *i*, *b<sub>i</sub>* = *i* + 1 for all *i* | – | – |
| 3 | 8 | – | *a<sub>i</sub>* = 1, *b<sub>i</sub>* = *i* + 1 for all *i* | – | – |
| 4 | 9 | – | *a<sub>i</sub>* ≤ 2, *b<sub>i</sub>* = *i* + 1 for all *i* | 3 | – |
| 5 | 7 | *n* ≤ 1000 | *c<sub>i,1</sub>* ≤ *c<sub>i,2</sub>* ≤ ... ≤ *c<sub>i,deg<sub>i</sub></sub>* for all *i* | – | – |
| 6 | 7 | *n* ≤ 1000 | *c<sub>i,j</sub>* = 0 for all 1 ≤ *i* ≤ *n* and *j* ≥ 2 | – | – |
| 7 | 17 | – | ∑<sub>*v*=1</sub><sup>*n*</sup> ∑<sub>*i*=1</sub><sup>deg<sub>*v*</sub></sup> *c<sub>v,i</sub>* = ∑<sub>*i*=1</sub><sup>*n*-1</sup> *w<sub>i</sub>* | – | – |
| 8 | 16 | *n* ≤ 1000 | – | 0, 1, 5, 6 | – |
| 9 | 11 | – | – | 0 – 8 | Offline-evaluation |

Starter code: