#K41277. Landmark Portal Sequence
Landmark Portal Sequence
Landmark Portal Sequence
You are given n landmarks numbered from 1 to n and m bidirectional portals connecting these landmarks. A portal connects two landmarks and can be traversed in both directions. You are also given a sequence of k landmarks. Your task is to determine whether it is possible to visit these landmarks in the given order. For any two consecutive landmarks in the sequence, there must exist a path connecting them using the provided portals.
More formally, for two landmarks \(u\) and \(v\), there exists a path if there is a sequence \(u = v_0, v_1, \ldots, v_p = v\) where each consecutive pair \(v_i\) and \(v_{i+1}\) is directly connected by a portal. If every consecutive pair of landmarks in the sequence satisfies this condition, output YES; otherwise, output NO.
inputFormat
The input is given via standard input and has the following format:
- The first line contains three integers \(n\), \(m\), and \(k\) where \(n\) is the number of landmarks, \(m\) is the number of portals, and \(k\) is the length of the landmark sequence.
- The next \(m\) lines each contain two integers \(u\) and \(v\) representing a bidirectional portal connecting landmarks \(u\) and \(v\).
- The last line contains \(k\) integers representing the sequence of landmarks you need to visit in order.
outputFormat
Output a single line: YES if it is possible to traverse the sequence by moving from each landmark to the next using the available portals, and NO otherwise.
## sample5 4 5
1 2
2 3
3 4
4 5
1 2 3 4 5
YES