#C11574. Non-Consecutive Vertex Labeling
Non-Consecutive Vertex Labeling
Non-Consecutive Vertex Labeling
You are given an undirected graph with n vertices and m edges. Your task is to assign a permutation of numbers from 1 to n to the vertices such that for every edge connecting vertices u and v, the absolute difference between the numbers assigned to u and v is not equal to 1.
If such a labeling exists, print YES
on the first line and one valid labeling (the numbers assigned to vertices 1,2,...,n in order) on the second line. Otherwise, print NO
.
The problem can be formally stated as follows. Let \(f: \{1, 2, \dots, n\} \to \{1, 2, \dots, n\}\) be a permutation. For every edge \((u, v)\), it must hold that:
[ |f(u) - f(v)| \neq 1. ]
It is guaranteed that the input size is small so that a backtracking solution exploring all permutations is acceptable.
inputFormat
The first line of input contains two integers n and m separated by a space, where n is the number of vertices and m is the number of edges.
The following m lines each contain two integers u and v (1-based indices) representing an edge between vertices u and v.
outputFormat
If a valid labeling exists, output YES
on the first line, and on the second line output n integers—the labeling for vertices 1 through n—separated by spaces. If no valid labeling exists, output only NO
.
4 3
1 2
2 3
3 4
YES
2 4 1 3
</p>