题目
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n;
int a[maxn], ind[maxn];
void solve(){
int i, j;
cin >> n;
string s;
cin >> s;
s = ' ' + s;
for(i = 1; i <= n; i++){
ind[i] = 0;
}
for(i = 1; i <= n; i++){
cin >> a[i];
ind[a[i]]++;//i -> a[i]
}
queue<int> q;
for(i = 1; i <= n; i++){
if(ind[i] == 0){
q.push(i);
}
}
vector<int> ans;
while(!q.empty()){
int u = q.front();
q.pop();
int v = a[u];
if(s[u] == '1'){//入度为0(其他开关不会影响自己了)且亮着的肯定得按一次开关
s[u] = '0';
s[v] ^= 1;
ans.push_back(u);
}
ind[v]--;
if(ind[v] == 0) q.push(v);
}
for(i = 1; i <= n; i++){//处理环内的灯
if(ind[i]){//找到环内的灯
int len = 0;//环的长度
int j = i;//当前在哪个灯
int res = 0;//把环内的灯关了要几次操作
int t = 0;//当前灯是否需要操作
/*
环内有两种情况:(只有亮着的灯个数为偶数才能全部关)
1、亮着的灯成对相邻存在
1 1
0 0
0 0
1 1
2、不相邻(此时一次操作相当于可以让1移动一步,在下图中向下移动最优)
1 0 0 0 0
0 0
0 0
1 0 0 0 0
*/
while(ind[j]){
if(s[j] == '1'){
t ^= 1;
}
ind[j] = 0;
len++;
res += t;
j = a[j];
}
if(t == 1){//奇数个灯亮
cout << -1 << '\n';
return;
}
for(int k = 1; k <= len; k++){
if(s[j] == '1') t ^= 1;
if(t == (res <= len - res)){//如果res <= len - res,说明从j开始操作是最优的,否则从j的下一个亮着的灯开始操作
ans.push_back(j);
}
j = a[j];
}
}
}
cout << ans.size() << '\n';
for(auto x : ans){
cout << x << " \n"[x == ans.back()];
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}