#C6880. Palindromic Path in Castles
Palindromic Path in Castles
Palindromic Path in Castles
You are given n castles and m bidirectional roads connecting them. Each road between two castles has an integer length. Your task is to determine if there exists a path such that the sequence of road lengths along the path forms a palindrome. More formally, if the sequence of road lengths along the path is represented as \(a_1, a_2, \ldots, a_k\), then it must satisfy \(a_i = a_{k-i+1}\) for all valid indices, and the path must consist of at least two roads.
The input is given in the following format:
- The first line contains two integers \(n\) and \(m\), the number of castles and roads respectively.
- Each of the next \(m\) lines contains three integers \(u\), \(v\), and \(l\) representing a bidirectional road between castles \(u\) and \(v\) with length \(l\).
Output Yes
if any palindromic path exists; otherwise, output No
.
inputFormat
The first line contains two integers \(n\) (number of castles) and \(m\) (number of roads).
Each of the following \(m\) lines contains three integers: \(u\), \(v\) and \(l\), where \(u\) and \(v\) are the castle indices (1-indexed) connected by the road, and \(l\) is the length of the road.
outputFormat
Output a single line containing either Yes
if there exists a palindromic path (with at least two roads) in the graph, or No
otherwise.
5 5
1 2 3
2 3 2
3 4 5
4 5 3
5 1 2
No