#P9829. Endless Earning Path
Endless Earning Path
Endless Earning Path
Mr. Lawrence is a traveling merchant who starts his journey at city 0. Each city i has an initial price status \(c_i\) which is either \(\text{Low}\) or \(\text{High}\). It is guaranteed that \(c_0 = \text{Low}\). When Mr. Lawrence visits a city, he must take an action: if the city’s price is Low he buys products (a buy‐in) and if the price is High he sells products (a sell‐out). Moreover, upon departing a city, its price flips (i.e. \(\text{Low}\) becomes \(\text{High}\) and vice versa).
The rules of travel are as follows:
- He starts at city 0 and buys products there (so his first action is buying at a Low priced city).
- After buying at a city, he must travel along one of its roads to a neighboring city and sell his product there. Thus, when arriving at the next city, its price must be High.
- After selling at a city, he must travel to one of its neighbors and buy products there. Hence, upon arrival the city’s price must be Low.
In other words, his path is an infinite sequence of cities \(p_0, p_1, p_2, \dots\) satisfying:
[ p_0 = 0, \quad \text{and} \quad \text{for every } k \ge 0, \quad c_{p_{2k}} = \text{Low} \quad (\text{buy}) \quad \text{and} \quad c_{p_{2k+1}} = \text{High} \quad (\text{sell}). ]
Your task is to decide whether there exists an endless earning path following the rules above. Note that when a city is visited for the first time, its arrival price is its given initial status. However, after leaving a city, its price flips and all subsequent arrivals will observe the new price. Thus, while initially only the state matching the given \(c_i\) is available for city \(i\), after its first visit both "Low" and "High" states become accessible in the journey.
If Mr. Lawrence can continue his alternation of buying and selling forever, output Yes
; otherwise output No
.
inputFormat
The first line contains two integers \(n\) and \(m\), representing the number of cities and the number of roads, respectively.
The second line contains \(n\) strings. The \(i\)-th string is either Low
or High
and denotes the initial price status \(c_i\) of city \(i\) (with city \(0\) always being Low
).
Each of the following \(m\) lines contains two integers \(u\) and \(v\) (0-indexed) indicating that there is an undirected road between cities \(u\) and \(v\).
outputFormat
Output a single line containing Yes
if there exists an endless earning path; otherwise, output No
.
sample
3 3
Low High Low
0 1
1 2
2 0
Yes