#K92632. Consecutive Graph Labeling
Consecutive Graph Labeling
Consecutive Graph Labeling
You are given an undirected graph with N nodes (numbered from 1 to N) and a set of edges. Your task is to determine whether it is possible to assign a unique label from 1 to N to each node such that for every edge \((u, v)\), the labels of u and v are consecutive. In other words, if \(L(u)\) denotes the label of node u, then for every edge, the condition
$$|L(u)-L(v)|=1$$
must hold. If a valid labeling exists, print YES
on the first line and then print one valid assignment of labels for nodes 1 through N on the second line (labels separated by a single space). Otherwise, print NO
.
inputFormat
The input is provided via standard input (stdin) as a sequence of space-separated integers. The first integer is N, the number of nodes. The remaining integers form pairs where each pair represents an edge connecting two nodes.
outputFormat
Output the result via standard output (stdout). If a valid labeling exists, output two lines. The first line should be YES
and the second line should list N space-separated integers representing the labels of nodes 1 through N. If no valid labeling exists, output a single line containing NO
.
3 1 2 2 3
YES
1 2 3
</p>