#P9829. [ICPC 2020 Shanghai R] Traveling Merchant

    ID: 9112 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2020上海O2优化双连通分量最近公共祖先,LCAICPC

[ICPC 2020 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.

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.