QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 1024 MB Total points: 100

#8029. 旅行商人

Statistics

Lawrence 是一位旅行商人,他在各个城市之间往返并转售商品。简单来说,为了从中获利,他需要以极低的价格买入商品,并以更高的价格卖出。你的任务是告诉他是否存在一条可以持续获利的无限旅行路径。

为了简化问题,假设有 $n$ 个城市,编号从 $0$ 到 $n-1$,以及 $m$ 条连接两个城市的无向道路。Lawrence 可以沿着道路在城市间旅行。他最初位于城市 $0$,每个城市 $i$ 都有一个初始价格 $c_i$,即“低”(Low)或“高”(High)。根据市场规律,当他从城市 $i$ 出发前往相邻城市 $j$ 后,城市 $i$ 的价格状态会发生改变(即高价变为低价,反之亦然)。(当 $m$ 条道路中的某一条连接城市 $i$ 和城市 $j$ 时,称城市 $j$ 为城市 $i$ 的相邻城市。)出于某些原因(例如产品保鲜度、差旅费、税收等),他必须遵守以下规则:

  1. 从城市 $0$ 出发并在城市 $0$ 买入商品。保证 $c_0$ 为“低”。
  2. 当他到达某个城市时,他要么卖出商品,要么买入商品。在离开城市之前,他不能什么都不做。
  3. 在某个城市 $i$ 买入商品后,他必须前往某个价格 $c_j$ 为“高”的相邻城市 $j$,并在城市 $j$ 卖出商品。
  4. 在某个城市 $i$ 卖出商品后,他必须前往某个价格 $c_j$ 为“低”的相邻城市 $j$,并在城市 $j$ 买入商品。

因此,路径看起来就像是在“低价买入”和“高价卖出”之间交替。

无限获利路径定义为由无限城市序列 $p_0, p_1, \dots$ 组成的路径,其中城市 $p_i$ 和城市 $p_{i+1}$ 之间有道路,$p_0 = 0$,且价格交替,换句话说,对于 $k \ge 0$,有 $c_{p_{2k}} = \text{Low}$(表示买入)且 $c_{p_{2k+1}} = \text{High}$(表示卖出)。请注意,这里的 $c_{p_i}$ 是到达城市 $p_i$ 时的价格,当他第二次到达该城市时,这个值可能会有所不同。

你的任务是确定是否存在任何这样的路径。

输入格式

输入包含多组测试数据。第一行包含一个正整数 $T$,表示测试数据的组数。每组测试数据的第一行包含两个正整数 $n$ 和 $m$,分别表示城市数量和道路数量。

下一行是一个长度为 $n$ 的字符串 $c$,包含字符 'H' 或 'L'。如果 $c$ 的第 $i$ 个字符($0 \le i < n$)为 'H',则表示城市 $i$ 的初始价格 $c_i$ 为高。如果为 'L',则表示初始价格 $c_i$ 为低。

接下来的 $m$ 行中,第 $i$ 行($1 \le i \le m$)包含两个不同的城市 $u_i$ 和 $v_i$,表示 $u_i$ 和 $v_i$ 之间有一条道路。

所有测试数据的 $n$ 之和不超过 $200,000$。所有测试数据的 $m$ 之和不超过 $200,000$。对于每组测试数据,每个 $i \in \{0, \dots, n-1\}$ 都有 $c_i \in \{H, L\}$。$c_0$ 始终为 $L$。对于每个 $i \in \{1, \dots, m\}$,满足 $0 \le u_i, v_i < n$ 且 $u_i \neq v_i$。没有两条道路连接同一对城市。

输出格式

对于每组测试数据,输出一行 "yes" 或 "no",表示是否存在无限获利路径。

样例

输入 1

2
4 4
LHLH
0 1
1 2
1 3
2 3
3 3
LHH
0 1
0 2
1 2

输出 1

yes
no

说明

在第一个样例测试中,无限获利路径为 $0 \to 1 \to 2 \to 3 \to 1 \to 2 \to 3 \to \dots$。在下图中,价格为 Low 的城市用条纹填充。

city=0 price=L, city=1 price=H, city=2 price=L, city=3 price=H, city=1 price=L, city=2 price=H

在第二个样例测试中,Lawrence 从城市 $0$ 出发只能走一步,之后所有城市的价格都将变为 High。因此,无法进行后续移动。

city=0 price=L, city=2 price=H

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.