#P9829. [ICPC2020 Shanghai R] Traveling Merchant
[ICPC2020 Shanghai R] Traveling Merchant
题目描述
Mr. Lawrence is a traveling merchant who travels between cities and resells products. Basically, to earn from it, he needs to buy products at a very low price and sell them at a higher price. Your task is to tell him whether there exists an endless traveling path that can earn money all the time.
To make things simple, suppose there are cities named from to and undirected roads each of which connecting two cities. Mr. Lawrence can travel between cities along the roads. Initially he is located at city and each of the city has a starting price , either or . Due to the law of markets, the price status at city will change (i.e. price will become price, or vice versa) after he departs for a neighboring city from . (City is a neighboring city of city when one of the roads connects city and city .) For some reasons (e.g. product freshness, traveling fee, tax), he :
- Start at city and buy products at city . It is guaranteed that is .
- When he arrives some city, he either sells products or buys products. It is not allowed for him to do nothing before he leaves the city.
- After buying products at some city , he must travel to some neighboring city whose price is and sell the products at city .
- After selling products at some city , he must travel to some neighboring city whose price is and buy the products at city .
As a result, the path will look like an alternation between buy at low price'' and
sell at high price''.
An endless earning path is defined as a path consisting of an endless sequence of cities where city and city has a road, , and the price alternates, in other words (indicates a buy-in) and (indicates a sell-out) for . Please note here is the price when city and this value may be different when he arrives the second time.
Your task is to determine whether there exists any such path.
输入格式
There are several test cases. The first line contains a positive integer indicating the number of test cases. Each test case begins with two positive integers and indicating the number of cities and the number of roads.
The next line is a string of length containing H' or
L'. The -th () charactor of is if the starting price at city is . The -th () charactor of is if the starting price at city is .
The -th line () of the following lines contains two different cities and , indicating a road between and .
The sum of the values of over all test cases is no more than . The sum of the values of over all test cases is no more than . For each test case, holds for each . is always . and hold for each . No two roads connect the same pair of cities.
输出格式
For each test case, output a line of yes
or no
, indicating whether there exists an endless earning path.
题目大意
题目描述
劳伦斯先生是一位在不同城市转售商品的旅行商人。基本地,为了赚钱,他需要以低价买进商品,再以高价卖出。现在请你为他规划一条可以一直盈利的旅行路线。
简单地说,假设有 座城市,标号为 到 ,以及 条连接特定两座城市的路,劳伦斯先生可以通过这些路到访每座城市。最初劳伦斯先生位于第 座城市,并且对于城市 都有一个起始价格 。根据市场规律,当他从城市 来到相邻的城市 时(当且仅当城市 与城市 之间有路径相连时,才称 与 为相邻城市),城市 的价格状况会发生变化(高价会变成低价,低价也可能变成高价)。而因为一些原因(比如商品的新鲜程度,旅行费用,税务等),他必须:
- 从城市 出发并在城市 购买一些商品。保证城市 的起始价格很低。
- 每当他到达一座城市后,他必须售卖或购买一些商品。
- 若他在城市 购买了商品,他就必须去一座与 相邻且价格 高于 的城市 ,并在那里卖掉手中来自城市 的商品。
- 若他在城市 售卖了商品,他就必须去一座与 相邻且价格 低于 的城市 ,并在那里购买一些商品。
因此,最终路径会始终重复 低价购入
和 高价卖出
。
一条无尽的盈利路线由无尽的城市序列 组成。其中,城市 与城市 之间有路径相连,,且价格高低是交替循环的,也就是说当 时,城市 的价格 (要在这个城市购买商品) 而相邻城市 的价格 (要在这个城市卖出商品)。
注意: 是 到达 城市 时的价格,而当他第二次到达城市 时,这个价格可能会因为市场规律而变化。
你需要写一个程序,判断是否有这样一条永远盈利的路径存在。
输入格式
输入有多组数据。所有数据的第一行是一个整型 表示数据组数。每组数据的第一行是两个整型 和 ,表示城市的数量和道路的数量。
每组数据的第二行是一个长度为 ,由 或 组成的字符串 。字符串 的第 个字符若为 ,则表示城市 的起始价格 高,反之若为 则表示城市 的起始价格 低。
接下来 行,每行输入一组 和 ,表示一条连接城市 和城市 的双向路径。
所有数据中 的总和不超过 , 的总和也不超过 。对于每组数据, 对应每个 ,保证 总为 。保证对于每个 ,都有 且 。保证每两座城市之间只有一条路径相连。
输出格式
对于每组数据,输出一行 yes
或者 no
,表示是否存在一条无尽的盈利路径。
2
4 4
LHLH
0 1
1 2
1 3
2 3
3 3
LHH
0 1
0 2
1 2
yes
no
提示
In the first sample test case, the endless earning path is $0\rightarrow 1\rightarrow 2\rightarrow 3\rightarrow 1\rightarrow 2\rightarrow 3\rightarrow \dots$. In the illustration, cities with price are filled with stripe.
In the second sample test case, Mr. Lawrence can only make one move from city and after that all cities will have price. Thus, no further moves can be made.