QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 256 MB Puntuación total: 100

#5631. 机器人

Estadísticas

在你没注意的时候,你的 $N$ 个机器人已经有了自己的生活,并散布在你的家乡。你家乡的 $N$ 个路口(编号为 $0, \dots, N-1$)中,每个路口恰好有一个机器人。在每个路口 $i$,都有一个指向某个路口的红色路标 $r_i \neq i$,以及一个指向某个路口的绿色路标 $g_i \neq i$。当你按下遥控器上的红色按钮时,每个机器人都会移动到红色路标所指的路口(位于路口 $i$ 的机器人移动到 $r_i$)。当你按下绿色按钮时,每个机器人都会移动到绿色路标所指的路口(位于路口 $i$ 的机器人移动到 $g_i$)。请编写一个程序,判断是否可以通过某种遥控器指令序列,使所有机器人在同一时间汇聚在同一个路口。

输入

输入的第一行包含一个十进制整数 $P$ ($1 \le P \le 500$),表示随后数据组的数量。每组数据应被独立处理。

每组数据包含三行输入:

  • 第一行包含数据组编号 $K$,后跟一个整数 $N$,表示路口的数量。
  • 第二行包含 $N$ 个空格分隔的整数 $r_0, \dots, r_{N-1}$ ($0 \le r_i \le N-1$ 且 $r_i \neq i$)。
  • 第三行包含 $N$ 个空格分隔的整数 $g_0, \dots, g_{N-1}$ ($0 \le g_i \le N-1$ 且 $g_i \neq i$)。

在某些路口,两个路标可能指向同一个方向(即 $r_i = g_i$)。

输出

对于每组数据,输出一行。如果能使所有机器人汇聚,输出字符串 “YES”,否则输出 “NO”。

样例

输入格式 1

2
1 4
1 2 3 0
3 0 1 0
2 4
1 2 3 0
2 2 1 2

输出格式 1

1 NO
2 YES

说明

对于第二个样例,按键序列 GREEN, RED, RED, GREEN 可以使所有机器人在路口 2 汇聚。

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.