The Contest Contest 1 P6 - A Typical DMOPC Problem

View as PDF

Points: 30 (partial)
Time limit: 3.0s
Java 5.0s
Python 5.0s
Memory limit: 256M

Author:
Problem type

Alice challenges Bob to a puzzle! Alice is situated on an undirected graph consisting of nodes and distinctly-weighted edges, the of which connects node and . She starts walking from node and performs the following procedure:

Suppose Alice is at node . If , she stops. Otherwise, she considers the set of all edges connected to node , excluding the edge she just travelled on, if any. If is empty, she stops. Otherwise, she can choose to walk along the lowest or highest weight edge in . Note that all edge weights are distinct so there are no ties.

Alice claims that no matter how the edges are weighted, she always has a strategy to never reach node . That is, she can choose edges to walk along such that she either stops at some node or ends up in an infinite loop. Skeptical of her claim, Bob gets to work trying to find counterexamples. Can you help Bob find an assignment of edge weights that disproves her claim, or confirm that her claim is correct?

Constraints

The graph is a tree.

Every node has degree at least .

Input Specification

The first line contains two integers and .

The next lines each contain two integers and , indicating that the edge connects nodes and .

Output Specification

On the first line, output either YES or NO, indicating whether or not there exists an assignment of edge weights that forces Alice to always reach node .

If such an assignment exists, output lines, where the line is the weight you assign to the edge. All weights must be distinct integers between and (inclusive).

Scoring

For each test file:

If you do not correctly determine if an assignment of edge weights exists, you will receive a score of for that file and a Wrong Answer verdict.

Otherwise, on each test case where an assignment is possible, your score for the test file is determined as follows:

You will receive full points if you output an valid assignment of edge weights that is fully correct and of the points otherwise.

Your final score within a subtask will be the minimum score over all tests in that subtask.

Sample Input 1

5 5
1 2
1 3
1 4
2 5
3 5

Sample Output 1

YES
1
8
4
2
6

Explanation for Sample 1

We can weight the edges to obtain the following graph:

Starting from node , Alice could either travel to node (using the minimum edge) or node (using the maximum edge) . If she chooses to go to node , her only next option is to take the edge to node . The same thing would happen if she chooses to go to node . Therefore, Alice must always reach node .

Sample Input 2

5 7
1 2
2 3
3 4
4 2
4 2
2 5
5 1

Sample Output 2

YES
1
2
3
4
5
6
7

Explanation for Sample 2

We can weight the edges to obtain the following graph:

It can be shown that Alice must always reach node .

Sample Input 3

4 5
1 2
1 3
2 4
3 4
3 2

Sample Output 3

NO

Explanation for Sample 3

It can be proven that no matter how we weight the edges, there exists a strategy for Alice to never reach node .