QOJ.ac

QOJ

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

#23643. 项链宝石排列

Statistics

Boris 想要出售美丽的项链。他咨询了最优秀的设计师和珠宝商,以发明出最完美的项链。现在他需要你的帮助,来判断是否存在一条符合他期望的完美项链。

Boris 想要制作的项链由 $3k$ 颗宝石组成,其中 $k$ 是项链的长度。任意长度 $k \ge 2$ 都是允许的。这些宝石排成三行,每行有 $k$ 颗宝石。所有宝石都有各自的类型(用整数标识)。我们将第一行从左到右的宝石类型定义为 $a_1, a_2, \dots, a_k$,第二行定义为 $b_1, b_2, \dots, b_k$,第三行定义为 $c_1, c_2, \dots, c_k$。

problem_23643_54327d0708bcad57a6669571d88ccf82.png

$k = 4$ 时的项链示例

一条项链有 $k$ 个后向列(backward columns)和 $k$ 个前向列(forward columns)。每一列包含 $3$ 颗宝石。每一列中宝石的顺序非常重要。后向列依次为 $(a_1, b_1, c_2), (a_2, b_2, c_3), \dots, (a_i, b_i, c_{i+1}), \dots, (a_k, b_k, c_1)$。前向列依次为 $(a_2, b_1, c_1), (a_3, b_2, c_2), \dots, (a_{i+1}, b_i, c_i), \dots, (a_1, b_k, c_k)$。

problem_23643_b76bd44f7ac4f82e116a32039eacd0f1.png

后向列

problem_23643_b05c9a10efe61f01113da53469f2af5d.png

前向列

Boris 有 $n$ 种美丽的后向列备选方案 $\{(x_i, y_i, z_i)\}_{i=1}^n$,以及 $m$ 种美丽的前向列备选方案 $\{(u_i, v_i, w_i)\}_{i=1}^m$。他认为,如果一条项链的所有列都是美丽的(即每一列都存在于对应的美丽列集合中),那么这条项链就是美丽的。请判断是否存在长度 $\ge 2$ 的美丽项链。

此外,Boris 保证所有的 $y_i$ 互不相同。注意,这一性质仅对美丽后向列中间的宝石有保证,因此其他位置的数字可能相等。

输入格式

每个输入包含多组测试数据。第一行包含一个整数 $t$($1 \le t \le 10^5$)——测试数据的组数。接下来是各组测试数据的描述。

每组测试数据的第一行包含两个整数 $n$、$m$($1 \le n, m \le 10^5$)——美丽后向列的数量和美丽前向列的数量。

接下来的 $n$ 行,每行包含三个整数 $x_i, y_i, z_i$($1 \le x_i, y_i, z_i \le 10^9$),描述一个美丽的后向列。

接下来的 $m$ 行,每行包含三个整数 $u_i, v_i, w_i$($1 \le u_i, v_i, w_i \le 10^9$),描述一个美丽的前向列。

保证在每组测试数据中,所有的 $y_i$ 互不相同。

保证所有测试数据中 $n$ 的总和与 $m$ 的总和均不超过 $10^5$。

输出格式

对于每组测试数据,如果存在长度 $\ge 2$ 的美丽项链,输出一个单词“YES”,否则输出“NO”。

你可以以任意大小写形式输出每个字母。

样例

输入格式 1

3
3 5
1 1 2
2 3 1
3 2 1
1 3 1
2 1 2
1 1 1
2 2 2
3 1 2
4 4
2 2 2
1 3 1
2 1 1
4 4 4
1 2 1
1 3 2
1 1 1
2 1 2
1 1
1 1 1
1 1 1

输出格式 1

YES
NO
YES

说明

第一组测试数据的一个美丽项链示例(长度为 $4$):

problem_23643_2c5cf15c04aae33885bee3b1318e32b2.png

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.