Autosynthesis (Codeforces 1877E)
Tags: Implement, graphs

Chaneka writes down an array $$$a$$$ of $$$n$$$ positive integer elements. Initially, all elements are not circled. In one operation, Chaneka can circle an element. It is possible to circle the same element more than once.After doing all operations, Chaneka makes a sequence $$$r$$$ consisting of all uncircled elements of $$$a$$$ following the order of their indices.Chaneka also makes another sequence $$$p$$$ such that its length is equal to the number of operations performed and $$$p_i$$$ is the index of the element that is circled in the $$$i$$$-th operation.Chaneka wants to do several operations such that sequence $$$r$$$ is equal to sequence $$$p$$$. Help her achieve this, or report if it is impossible! Note that if there are multiple solutions, you can print any of them.

Input: The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the size of array $$$a$$$.The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,\ldots,a_n$$$ ($$$1\leq a_i\leq n$$$).

Output: A single line containing $$$-1$$$ if it is impossible.Otherwise, the output consists of two lines. The first line contains an integer $$$z$$$ representing the number of operations performed. The second line contains $$$z$$$ integers, the $$$i$$$-th integer represents the index of the element circled in the $$$i$$$-th operation. If there are multiple solutions, you can print any of them.

Note: In the first example, doing the operations like the example output gives the following results:   Element $$$a_2$$$ gets circled $$$1$$$ time.  Element $$$a_3$$$ gets circled $$$2$$$ times.  The uncircled elements (ordered by their indices) are $$$a_1$$$, $$$a_4$$$, and $$$a_5$$$. So $$$r=[3,2,3]$$$.  $$$p=[3,2,3]$$$ Therefore, $$$r=p$$$.In the second example, it is impossible.

Sample Inputs:
5
3 4 2 2 3

3
1 2 3

Sample Outputs:
3
3 2 3

-1

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr) = (readInt(), [0] + readList())
    deg = [0] * (n + 1)
    for i in range(1, n + 1):
        deg[arr[i]] += 1
    uc = [-1] * (n + 1)
    st = [i for i in range(1, n + 1) if deg[i] == 0]
    for idx in st:
        uc[idx] = 1
    while st:
        node = st.pop()
        deg[arr[node]] -= 1
        if uc[arr[node]] != -1:
            continue
        if deg[arr[node]] == 0:
            st.append(arr[node])
            uc[arr[node]] = uc[node] ^ 1
        elif uc[node] == 1:
            st.append(arr[node])
            uc[arr[node]] = uc[node] ^ 1
    for i in range(1, n + 1):
        if uc[i] != -1:
            continue
        node = i
        uc[i] = cnt = 1
        while arr[node] != i:
            uc[arr[node]] = uc[node] ^ 1
            node = arr[node]
            cnt += 1
        if cnt % 2:
            print(-1)
            return
    ans = [arr[i] for i in range(1, n + 1) if uc[i] == 1]
    print(len(ans))
    print(*ans)
    return
solve()

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, arr) = (readInt(), [0] + readList())
    deg = [0] * (n + 1)
    for i in range(1, n + 1):
        deg[arr[i]] += 1
    uc = [-1] * (n + 1)
    qu = deque([i for i in range(1, n + 1) if deg[i] == 0])
    while qu:
        node = qu.popleft()
        uc[node] = 1
        if uc[arr[node]] != -1:
            continue
        uc[arr[node]] = 0
        deg[arr[arr[node]]] -= 1
        if deg[arr[arr[node]]] == 0:
            qu.append(arr[arr[node]])
    for i in range(1, n + 1):
        if uc[i] != -1:
            continue
        node = i
        uc[i] = cnt = 1
        while arr[node] != i:
            uc[arr[node]] = uc[node] ^ 1
            node = arr[node]
            cnt += 1
        if cnt % 2:
            print(-1)
            return
    ans = [arr[i] for i in range(1, n + 1) if uc[i] == 1]
    print(len(ans))
    print(*ans)
    return
solve()

Solution (Python):
def main():
    n = int(input())
    a = [-1] + readIntArr()
    element_cnts = [0] * (n + 1)
    for i in range(1, n + 1):
        element_cnts[a[i]] += 1
    ans_arr = [-1] * (n + 1)
    st = []
    vi = [0] * (n + 1)
    for i in range(1, n + 1):
        if element_cnts[i] == 0:
            ans_arr[i] = a[i]
            st.append(i)
            vi[i] = 1
    while st:
        i = st.pop()
        j = a[i]
        if vi[j] == 0:
            vi[j] = 1
            element_cnts[a[j]] -= 1
            if element_cnts[a[j]] == 0:
                ans_arr[a[j]] = a[a[j]]
                st.append(a[j])
                vi[a[j]] = 1
    nxt = [-1] * (n + 1)
    for i in range(1, n + 1):
        if vi[i] == 0:
            nxt[i] = a[i]
    vi = [0] * (n + 1)
    for i in range(1, n + 1):
        if nxt[i] != -1 and vi[i] == 0:
            cycle = [i]
            j = nxt[i]
            while j != i:
                cycle.append(j)
                j = nxt[j]
            if len(cycle) % 2 == 1:
                print(-1)
                return
            for j in range(0, len(cycle), 2):
                ans_arr[cycle[j]] = a[cycle[j]]
            for j in cycle:
                vi[j] = 1
    ans = []
    for i in range(1, n + 1):
        if ans_arr[i] != -1:
            ans.append(ans_arr[i])
    print(len(ans))
    oneLineArrayPrint(ans)
    return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
    return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
    dv = defaultValFactory
    da = dimensionArr
    if len(da) == 1:
        return [dv() for _ in range(da[0])]
    else:
        return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())

def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
    main()