Autosynthesis (Codeforces 1876C)
Tags: Greedy, dfs and similar

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):
n = int(input())
a = list(map(int, input().split()))
tr1 = []
tr2 = [0] * n
ans = []
for x in a:
    tr1.append(x - 1)
    tr2[x - 1] += 1
stack = []
for i in range(n):
    if tr2[i] == 0:
        stack.append(i)
while True:
    if len(stack) == 0:
        break
    ind = stack.pop()
    ans.append(ind)
    tr2[ind] = -1
    ku = tr1[ind]
    if tr2[ku] != -1:
        tr2[ku] = -1
        if tr2[tr1[ku]] != -1:
            tr2[tr1[ku]] -= 1
            if tr2[tr1[ku]] == 0:
                stack.append(tr1[ku])
for i in range(n):
    schot = 0
    if tr2[i] == 1:
        ind = tr1[i]
        while True:
            tr2[ind] = 2
            if schot % 2 == 1:
                ans.append(ind)
            if ind == i:
                break
            schot += 1
            ind = tr1[ind]
        if schot % 2 == 0:
            print(-1)
            break
else:
    print(len(ans))
    ans.sort()
    ans2 = []
    for x in ans:
        ans2.append(a[x])
    print(' '.join(map(str, ans2)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
tr1 = []
tr2 = [0] * n
ans = []
for x in a:
    tr1.append(x - 1)
    tr2[x - 1] += 1
stack = []
for i in range(n):
    if tr2[i] == 0:
        stack.append(i)
while True:
    if len(stack) == 0:
        break
    ind = stack.pop()
    ans.append(ind)
    tr2[ind] = -1
    ku = tr1[ind]
    if tr2[ku] != -1:
        tr2[ku] = -1
        if tr2[tr1[ku]] != -1:
            tr2[tr1[ku]] -= 1
            if tr2[tr1[ku]] == 0:
                stack.append(tr1[ku])
for i in range(n):
    schot = 0
    if tr2[i] == 1:
        ind = tr1[i]
        while True:
            tr2[ind] = 2
            if schot % 2 == 1:
                ans.append(ind)
            if ind == i:
                break
            schot += 1
            ind = tr1[ind]
        if schot % 2 == 0:
            print(-1)
            break
else:
    print(len(ans))
    ans.sort()
    ans2 = []
    for x in ans:
        ans2.append(a[x])
    print(' '.join(map(str, ans2)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
tr1 = []
tr2 = [0] * n
ans = []
for x in a:
    tr1.append(x - 1)
    tr2[x - 1] += 1
stack = []
for i in range(n):
    if tr2[i] == 0:
        stack.append(i)
while True:
    if len(stack) == 0:
        break
    ind = stack.pop()
    ans.append(ind)
    tr2[ind] = -1
    ku = tr1[ind]
    if tr2[ku] != -1:
        tr2[ku] = -1
        if tr2[tr1[ku]] != -1:
            tr2[tr1[ku]] -= 1
            if tr2[tr1[ku]] == 0:
                stack.append(tr1[ku])
for i in range(n):
    schot = 0
    if tr2[i] == 1:
        ind = tr1[i]
        while True:
            tr2[ind] = 2
            if schot % 2 == 1:
                ans.append(ind)
            if ind == i:
                break
            schot += 1
            ind = tr1[ind]
        if schot % 2 == 0:
            print(-1)
            break
else:
    print(len(ans))
    ans.sort()
    ans2 = []
    for x in ans:
        ans2.append(a[x])
    print(' '.join(map(str, ans2)))