Here is a complete Python solution. It
- Reads the group‐sizes,
- Builds the edge‐indexing function in O(1),
- For each required node‐cycle (full cycle and, if requested, the 2‐cycle),
• Builds the induced edge permutation,
• Extracts its cycle‐decomposition (omitting 1‐cycles),
• Prints it in the requested “smallest‐first, ordered” notation.
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
def edge_index(i, j, n):
"""
Given 1 <= i < j <= n, returns the lex-order index of edge {i,j}:
{1,2}->1, {1,3}->2, ..., {1,n}->n-1,
{2,3}->n, ...
Formula:
offset = sum_{k=1..i-1} (n - k)
= (i-1)*n - (i-1)*i//2
idx = offset + (j - i)
"""
# offset edges before the block starting with i
off = (i-1)*n - (i-1)*i//2
return off + (j - i)
def cycle_decomposition(perm):
"""
Given perm[1..m], a 1-based permutation array,
returns a list of nontrivial cycles, each cycle as a list of ints,
in the required order:
- Each cycle starts with its smallest element.
- Cycles ordered by these starters ascending.
"""
m = len(perm)-1
visited = [False]*(m+1)
cycles = []
for i in range(1, m+1):
if not visited[i] and perm[i] != i:
cyc = []
x = i
while not visited[x]:
visited[x] = True
cyc.append(x)
x = perm[x]
# rotate so the smallest element is first
mi = min(cyc)
k = cyc.index(mi)
cyc = cyc[k:] + cyc[:k]
cycles.append(cyc)
# sort cycles by their first (smallest) element
cycles.sort(key=lambda c:c[0])
return cycles
def print_cycles(cycles):
"""
Given list of cycles, each a list of ints, prints them in the form
(a,b,c)(d,e)... with no spaces and commas between elements.
If cycles is empty, prints an empty line.
"""
if not cycles:
print() # blank line
return
out = []
for cyc in cycles:
out.append("(" + ",".join(str(x) for x in cyc) + ")")
print("".join(out))
def build_edge_permutation(n, node_map):
"""
n : total number of nodes
node_map : list of length n+1, node_map[u] = s(u)
Returns perm[1..m] mapping each edge-index to its permuted index.
"""
m = n*(n-1)//2
perm = [0]*(m+1)
for i in range(1, n):
for j in range(i+1, n+1):
idx1 = edge_index(i, j, n)
u, v = node_map[i], node_map[j]
if u > v:
u, v = v, u
idx2 = edge_index(u, v, n)
perm[idx1] = idx2
return perm
def main():
data = sys.stdin.read().strip().split()
groups = list(map(int, data))
k = len(groups)
prefix = [0]*(k+1)
for i in range(1, k+1):
prefix[i] = prefix[i-1] + groups[i-1]
n = prefix[k]
# process each group
for i in range(1, k+1):
ni = groups[i-1]
if ni < 2:
continue
L = prefix[i-1] + 1
R = prefix[i]
# --- FULL CYCLE (L -> L+1 -> ... -> R -> L) ---
node_map = list(range(n+1))
cyc_nodes = list(range(L, R+1))
for a, b in zip(cyc_nodes, cyc_nodes[1:]):
node_map[a] = b
node_map[R] = L
perm = build_edge_permutation(n, node_map)
cycles = cycle_decomposition(perm)
print_cycles(cycles)
# --- 2-CYCLE (only if ni > 2): swap L <-> L+1 ---
if ni > 2:
node_map = list(range(n+1))
node_map[L], node_map[L+1] = node_map[L+1], node_map[L]
perm = build_edge_permutation(n, node_map)
cycles = cycle_decomposition(perm)
print_cycles(cycles)
if __name__ == "__main__":
main()