#K13191. Valid City Visit Path

    ID: 23858 Type: Default 1000ms 256MiB

Valid City Visit Path

Valid City Visit Path

You are given an undirected graph representing a network of cities. The graph is defined by n cities and m roads. Each road connects two distinct cities. Additionally, you are provided with an order (a permutation of the cities from 1 to n) that represents the sequence in which a traveler visits the cities.

Your task is to determine whether the provided order forms a valid path through the network. A valid path is defined such that for every pair of consecutive cities in the given order, there exists a direct road connecting them.

If every consecutive pair is connected by a road, output YES; otherwise, output NO.

Note: The roads are bidirectional. There may be cases with only one city where the order is trivially valid.

inputFormat

The input is read from standard input and has the following format:

The first line contains two integers n and m, where n is the number of cities, and m is the number of roads.

The next m lines each contain two integers a and b representing a bidirectional road between cities a and b.

The last line contains n integers representing the order in which the cities are visited.

outputFormat

Output a single line to standard output containing either YES or NO depending on whether the order is a valid path according to the rules.## sample

4 3
1 2
2 3
3 4
1 2 3 4
YES