CF1907G.Lights

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In the end of the day, Anna needs to turn off the lights in the office. There are nn lights and nn light switches, but their operation scheme is really strange. The switch ii changes the state of light ii, but it also changes the state of some other light aia_i (change the state means that if the light was on, it goes off and vice versa).

Help Anna to turn all the lights off using minimal number of switches, or say it is impossible.

一天结束时,安娜需要关闭办公室的灯。一共有 nn 盏灯和 nn 个开关,但它们的工作方式非常奇特:开关 ii 不仅会改变灯 ii 的状态,还会改变另一盏灯 aia_i 的状态(“改变状态”指:如果灯原本是开着的,则变为关闭;如果原本是关闭的,则变为开启)。

请帮助安娜用最少数量的开关将所有灯都关闭;若不可能实现,请说明这一点。

输入格式

The first line of input contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. Descriptions of test cases follow.

The first line of each test case contains the integer nn (2≤n≤1052 \le n \le 10^5) — the number of lights.

The second line of each test case contains the string of nn characters, the initial state of the lights. Character "0" means that the corresponding light is off, and "1" means that it is on.

The third line of each test case contains nn integers aia_i (1≤ai≤n1 \le a_i \le n, ai≠ia_i \neq i) — the switch ii changes the states of light ii and light aia_i.

It is guaranteed that sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5),表示灯的数量。

每个测试用例的第二行包含一个长度为 nn 的字符串,表示灯的初始状态。字符 "0" 表示对应灯处于关闭状态,字符 "1" 表示对应灯处于开启状态。

每个测试用例的第三行包含 nn 个整数 aia_i(1≤ai≤n1 \le a_i \le n,且 ai≠ia_i \neq i),表示开关 ii 会改变灯 ii 和灯 aia_i 的状态。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case output the integer kk, the minimal number of switches to use, then in the separate line output the list of kk switches.

If it is impossible to turn off all the lights, output single integer −1-1.

对于每个测试用例,输出整数 kk(即所需开关的最小数量),然后在下一行输出这 kk 个开关的列表。

如果无法关闭所有灯,则输出单个整数 −1-1。

输入输出样例

  • 输入#1

    8
    5
    11101
    4 3 4 2 2
    2
    10
    2 1
    10
    0000000011
    9 10 10 7 10 9 9 9 10 2
    10
    1000111101
    9 3 8 9 2 1 3 7 2 7
    10
    0001101010
    5 7 6 10 8 3 6 6 2 2
    10
    0101100010
    8 7 7 9 9 4 1 4 2 7
    10
    1010111010
    7 9 10 7 7 2 8 6 10 4
    10
    1110000001
    3 10 10 1 10 8 6 3 2 1

    输出#1

    3
    1 5 3 
    -1
    1
    9 
    5
    5 6 10 2 3 
    6
    4 9 5 10 8 7 
    3
    5 4 9 
    6
    1 3 5 9 7 8 
    2
    2 1

输入解题思路,AI测评打分。不知道怎么写?

首页