AtCoder Regular Contest 215
E
CNOT 聚会
CNOT Party
AtCoder
⌀ 2418
原题
← DF →

CNOT 聚会AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given binary strings $A = A_1 A_2 \ldots A_N$ and $B = B_1 B_2 \ldots B_N$ of length $N$.

给定两个长度为 $N$ 的二进制字符串 $A = A_1 A_2 \ldots A_N$ 和 $B = B_1 B_2 \ldots B_N$。AI (CI翻译)

You can perform $M$ types of operations. The $i$-th type of operation is represented by a pair of integers $(x_i, y_i)$ with $1 \le x_i, y_i \le N$, and it flips the value of $A_{y_i}$ when $A_{x_i} = 1$. It is possible that $x_i = y_i$. Here, flipping means changing $0$ to $1$ or $1$ to $0$.

你可以对 $A$ 执行 $M$ 种操作。第 $i$ 种操作由一对整数 $(x_i, y_i)$ 表示,其中 $1 \le x_i, y_i \le N$。当 $A_{x_i} = 1$ 时,该操作会翻转 $A_{y_i}$ 的值。允许 $x_i = y_i$。这里,翻转是指将 $0$ 变为 $1$,或将 $1$ 变为 $0$。AI (CI翻译)

Determine whether it is possible to convert $A$ into a string equal to $B$ by performing at most $2N$ operations on $A$, and if so, construct one such way.

判断能否通过至多 $2N$ 次操作将 $A$ 转换成与 $B$ 相同的字符串;如果可以,请构造一种操作方案。AI (CI翻译)

You are given $T$ test cases; solve each one.

给定 $T$ 组测试用例,请分别求解。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \le T \le 2 \times 10^5$
  • $1 \le N \le 2 \times 10^5$
  • $1 \le M \le 2 \times 10^5$
  • $A$ and $B$ are binary strings of length $N$.
  • $A \neq B$
  • $1 \le x_i \le N$ ($1 \le i \le M$)
  • $1 \le y_i \le N$ ($1 \le i \le M$)
  • The sum of $N$ and $M$ over all test cases is at most $2 \times 10^5$.
  • $T$, $N$, $M$, $x_i$, $y_i$ are integers.
  • $1 \le T \le 2 \times 10^5$
  • $1 \le N \le 2 \times 10^5$
  • $1 \le M \le 2 \times 10^5$
  • $A$ 和 $B$ 是长度为 $N$ 的二进制字符串。
  • $A \neq B$
  • $1 \le x_i \le N$($1 \le i \le M$)
  • $1 \le y_i \le N$($1 \le i \le M$)
  • 所有测试用例的 $N$ 和 $M$ 之和不超过 $2 \times 10^5$。
  • $T$、$N$、$M$、$x_i$、$y_i$ 均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入读入,格式如下:AI (CI翻译)

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试用例的格式如下:AI (CI翻译)

N
A
B
M
x_1 y_1
x_2 y_2
\vdots
x_M y_M

N
A
B
M
x_1 y_1
x_2 y_2
\vdots
x_M y_M
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output for the $T$ test cases in order.

按顺序输出 $T$ 组测试用例的答案。AI (CI翻译)

For each test case, output as follows.

对于每组测试用例,按以下格式输出。AI (CI翻译)

If no sequence of operations satisfying the condition exists, output -1 on a single line.

如果不存在满足条件的操作序列,在单独一行输出 -1。AI (CI翻译)

If a sequence of operations satisfying the condition exists, let $K$ be the length of the sequence of operations and $C_i$ be the index of the $i$-th operation performed, and output as follows:

如果存在满足条件的操作序列,设操作序列的长度为 $K$,第 $i$ 次执行的操作编号为 $C_i$,按以下格式输出:AI (CI翻译)

K
C_1 C_2 \ldots C_K

K
C_1 C_2 \ldots C_K
AI (CI翻译)

Here, $K$ must be at most $2N$, and operation $i$ must not be performed when $A_{x_i} = 0$.

其中,$K$ 必须不超过 $2N$,并且执行第 $i$ 种操作时必须满足 $A_{x_i} = 1$。AI (CI翻译)

样例

Input #1
3
4
0100
1010
4
1 2
2 1
2 3
4 3
2
10
01
1
1 2
2
01
00
3
1 1
1 2
2 1
Output #1
3
2 3 1
-1
3
3 2 1
For the first test case, you can perform the operations as follows. - Initially, $A = 0100$. - Perform operation $2$. Now, $A = 1100$. - Perform operation $3$. Now, $A = 1110$. - Perform operation $1$. Now, $A = 1010$. For the second test case, no matter what operations are performed, it is impossible to make $A_1$ equal to $0$. Note that, as in the third test case, $x_i = y_i$ is possible.
题目信息
题号
arc215_e
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。