#P9829. [ICPC2020 Shanghai R] Traveling Merchant

    ID: 9193 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2020上海O2优化双连通分量最近公共祖先,LCAICPC

[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 nn cities named from 00 to n1n-1 and mm undirected roads each of which connecting two cities. Mr. Lawrence can travel between cities along the roads. Initially he is located at city 00 and each of the city ii has a starting price cic_i, either Low\text{Low} or High\text{High}. Due to the law of markets, the price status at city ii will change (i.e. High\text{High} price will become Low\text{Low} price, or vice versa) after he departs for a neighboring city jj from ii. (City jj is a neighboring city of city ii when one of the mm roads connects city ii and city jj.) For some reasons (e.g. product freshness, traveling fee, tax), he must\textbf{must}:

  • Start at city 00 and buy products at city 00. It is guaranteed that c0c_0 is Low\text{Low}.
  • 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 ii, he must travel to some neighboring city jj whose price cjc_j is High\text{High} and sell the products at city jj.
  • After selling products at some city ii, he must travel to some neighboring city jj whose price cjc_j is Low\text{Low} and buy the products at city jj.

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 p0,p1,p_0, p_1,\dots where city pip_i and city pi+1p_{i+1} has a road, p0=0p_0=0, and the price alternates, in other words cp2k=Lowc_{p_{2k}}=\text{Low} (indicates a buy-in) and cp2k+1=Highc_{p_{2k+1}}=\text{High} (indicates a sell-out) for k0k\geq0. Please note here cpic_{p_i} is the price when arriving\textbf{arriving} city pip_i 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 TT indicating the number of test cases. Each test case begins with two positive integers nn and mm indicating the number of cities and the number of roads.

The next line is a string cc of length nn containing H' or L'. The ii-th (0i<n0\le i<n) charactor of cc is HH if the starting price cic_i at city ii is High\text{High}. The ii-th (0i<n0\le i<n) charactor of cc is LL if the starting price cic_i at city ii is Low\text{Low}.

The ii-th line (1im1\le i\le m) of the following mm lines contains two different cities uiu_i and viv_i, indicating a road between uiu_i and viv_i.

The sum of the values of nn over all test cases is no more than 200,000200,000. The sum of the values of mm over all test cases is no more than 200,000200,000. For each test case, ci{H,L}c_i\in\{\text{H},\text{L}\} holds for each i{0,,n1}i\in \{0, \ldots, n-1\}. c0c_0 is always LL. 0ui,vi<n0\leq u_i,v_i<n and uiviu_i\neq v_i hold for each i{1,,m}i\in \{1,\ldots, m\}. 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.

题目大意

题目描述

劳伦斯先生是一位在不同城市转售商品的旅行商人。基本地,为了赚钱,他需要以低价买进商品,再以高价卖出。现在请你为他规划一条可以一直盈利的旅行路线。

简单地说,假设有 nn 座城市,标号为 00n1n-1 ,以及 mm 条连接特定两座城市的路,劳伦斯先生可以通过这些路到访每座城市。最初劳伦斯先生位于第 00 座城市,并且对于城市 ii 都有一个起始价格 cic_i 。根据市场规律,当他从城市 ii 来到相邻的城市 jj 时(当且仅当城市 ii 与城市 jj 之间有路径相连时,才称 iijj 为相邻城市),城市 ii 的价格状况会发生变化(高价会变成低价,低价也可能变成高价)。而因为一些原因(比如商品的新鲜程度,旅行费用,税务等),他必须

  • 从城市 00 出发并在城市 00 购买一些商品。保证城市 00 的起始价格很
  • 每当他到达一座城市后,他必须售卖购买一些商品。
  • 若他在城市 ii 购买了商品,他就必须去一座与 ii 相邻且价格 cjc_j 高于 cic_i 的城市 jj ,并在那里卖掉手中来自城市 ii 的商品。
  • 若他在城市 ii 售卖了商品,他就必须去一座与 ii 相邻且价格 cjc_j 低于 cic_i 的城市 jj,并在那里购买一些商品。

因此,最终路径会始终重复 低价购入高价卖出

一条无尽的盈利路线由无尽的城市序列 p0,p1p_0,p_1 \dots 组成。其中,城市 pip_i 与城市 pp+1p_{p+1} 之间有路径相连,p0=0p_0 = 0,且价格高低是交替循环的,也就是说当 k0k \ge 0 时,城市 p2kp_{2k} 的价格 cp2k=Lowc_{p_{2k}} = \text{Low} (要在这个城市购买商品) 而相邻城市 p2k+1p_{2k+1} 的价格 cp2k+1=Hignc_{p_{2k+1}} = \text{Hign} (要在这个城市卖出商品)。

注意cpic_{p_i}到达 城市 pip_i 时的价格,而当他第二次到达城市 pip_i 时,这个价格可能会因为市场规律而变化。

你需要写一个程序,判断是否有这样一条永远盈利的路径存在。

输入格式

输入有多组数据。所有数据的第一行是一个整型 TT 表示数据组数。每组数据的第一行是两个整型 nnmm,表示城市的数量和道路的数量。

每组数据的第二行是一个长度为 nn ,由 HHLL 组成的字符串 cc 。字符串 cc 的第 ii 个字符若为 HH,则表示城市 ii 的起始价格 cic_i ,反之若为 LL 则表示城市 ii 的起始价格 cic_i

接下来 mm 行,每行输入一组 uiu_iviv_i ,表示一条连接城市 uiu_i 和城市 viv_i 的双向路径。

所有数据中 nn 的总和不超过 200,000200,000mm 的总和也不超过 200,000200,000 。对于每组数据,ci{H,L}c_i\in\{\text{H}, \text{L}\} 对应每个 i{0,,n1}i\in\{0, \dots, n-1\} ,保证 c0c_0 总为 LL 。保证对于每个 i{1,,m}i\in\{1,\dots,m\} ,都有 0ui,vi<n0 \leq u_i,v_i < nuiviu_i \neq v_i 。保证每两座城市之间只有一条路径相连。

输出格式

对于每组数据,输出一行 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 Low\text{Low} price are filled with stripe.

In the second sample test case, Mr. Lawrence can only make one move from city 00 and after that all cities will have High\text{High} price. Thus, no further moves can be made.