Permutation Weight (Easy Version) (Codeforces 1685D1)
Tags: Others, dsu

This is an easy version of the problem. The difference between the easy and hard versions is that in this version, you can output any permutation with the smallest weight.You are given a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$.Let's define the weight of the permutation $$$q_1, q_2, \ldots, q_n$$$ of integers from $$$1$$$ to $$$n$$$ as $$$$$$|q_1 - p_{q_{2}}| + |q_2 - p_{q_{3}}| + \ldots + |q_{n-1} - p_{q_{n}}| + |q_n - p_{q_{1}}|$$$$$$You want your permutation to be as lightweight as possible. Find any permutation $$$q$$$ with the smallest possible weight.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)  — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 200$$$)  — the size of the permutation.The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation.The sum of $$$n$$$ over all test cases doesn't exceed $$$400$$$.

Output: For each test case, output $$$n$$$ integers $$$q_1, q_2, \ldots, q_n$$$ ($$$1 \le q_i \le n$$$, all $$$q_i$$$ are distinct)  — one of the permutations with the smallest weight.

Note: In the first test case, there are two permutations of length $$$2$$$: $$$(1, 2)$$$ and $$$(2, 1)$$$. Permutation $$$(1, 2)$$$ has weight $$$|1 - p_2| + |2 - p_1| = 0$$$, and permutation $$$(2, 1)$$$ has the same weight: $$$|2 - p_1| + |1 - p_2| = 0$$$. You can output any of these permutations in this version.In the second test case, the weight of the permutation $$$(1, 3, 4, 2)$$$ is $$$|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_1| = |1 - 1| + |3 - 4| + |4 - 3| + |2 - 2| = 2$$$. There are no permutations with smaller weights.In the third test case, the weight of the permutation $$$(1, 4, 2, 3, 5)$$$ is $$$|1 - p_4| + |4 - p_2| + |2 - p_3| + |3 - p_5| + |5 - p_1| = |1 - 2| + |4 - 4| + |2 - 3| + |3 - 1| + |5 - 5| = 4$$$. There are no permutations with smaller weights.

Sample Inputs:
3
2
2 1
4
2 3 1 4
5
5 4 3 2 1

Sample Outputs:
1 2 
1 3 4 2 
1 4 2 3 5

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

class Node:

    def __init__(self, i):
        self.i = i
        self.nex = None
        self.bef = None

def solve():
    n = int(input())
    P = list(map(int, input().split()))
    node = [Node(i) for i in range(n)]
    for (i, p) in enumerate(P):
        p -= 1
        node[p].nex = node[i]
        node[i].bef = node[p]
    used = [False] * n
    V = node[0]
    while not used[V.i]:
        used[V.i] = True
        V = V.nex
    for i in range(n):
        if used[i]:
            continue
        V = node[i]
        while not used[V.i]:
            used[V.i] = True
            V = V.nex
        ''
        (node[i].nex, node[i - 1].nex, node[i].nex.bef, node[i - 1].nex.bef) = (node[i - 1].nex, node[i].nex, node[i - 1], node[i])
    ans = [1]
    V = node[0].nex
    while V.i != 0:
        ans.append(V.i + 1)
        V = V.nex
    print(*ans)
for _ in range(int(input())):
    solve()

Solution (Python):
import sys
I = lambda : [*map(int, sys.stdin.readline().split())]
(t,) = I()
for _ in range(t):
    (n,) = I()
    p = I()
    p = [guy - 1 for guy in p]
    cycles = []
    hit = [0] * n
    for i in range(n):
        if not hit[i]:
            cycle = {i}
            hit[i] = 1
            nex = p[i]
            while nex not in cycle:
                hit[nex] = 1
                cycle.add(nex)
                nex = p[nex]
            cycles.append(cycle)
    base = cycles[0]
    for i in range(n):
        if i not in base:
            for j in range(len(cycles)):
                if i in cycles[j]:
                    break
            base |= cycles[j]
            for j in range(n):
                if p[j] == i:
                    p[j] = i - 1
                elif p[j] == i - 1:
                    p[j] = i
    q = [0]
    for i in range(n - 1):
        for j in range(n):
            if p[j] == q[-1]:
                q.append(j)
                break
    print(*[guy + 1 for guy in q])

Solution (Python):
import sys
I = lambda : [*map(int, sys.stdin.readline().split())]
(t,) = I()
for _ in range(t):
    (n,) = I()
    p = I()
    p = [guy - 1 for guy in p]
    cycles = []
    hit = [0] * n
    for i in range(n):
        if not hit[i]:
            cycle = {i}
            hit[i] = 1
            nex = p[i]
            while nex not in cycle:
                hit[nex] = 1
                cycle.add(nex)
                nex = p[nex]
            cycles.append(cycle)
    base = cycles[0]
    for i in range(n):
        if i not in base:
            for j in range(len(cycles)):
                if i in cycles[j]:
                    break
            base |= cycles[j]
            for j in range(n):
                if p[j] == i:
                    p[j] = i - 1
                elif p[j] == i - 1:
                    p[j] = i
    q = [0]
    for i in range(n - 1):
        for j in range(n):
            if p[j] == q[-1]:
                q.append(j)
                break
    print(*[guy + 1 for guy in q])