Symbolic systems of equations

A symbolic system of equations is a set of equations
x = f(y₁, …, y_(k)), where x, y₁, …, y_(k) are variables and f is a
symbol representing an arbitrary function with k arguments (we say that
f has arity k). A solution of a system with n variables x₁, …, x_(n) is
any assignment α of expressions to variables in such a way that for
every equation x = f(y₁, …, y_(k)) it holds that
α(x) = f(α(y₁), …, α(y_(k))).

Write a program that, given a system of symbolic equations, computes its
most general solution, or tells that it does not exist.

Input

Input consists of several cases, each with n, followed by n variables in
lexicographical order, followed by the number of equations m, followed
by m equations in the exact format of the examples. Variables and
functions are words made up of lowercase letters, all different. Every
variable appears at most once in the left side of an equation. Every
function can occur several times, but always with the same arity,
between 1 and n. All arguments of the same function are different
variables. You can assume 1 ≤ n ≤ 40.

Output

Print, in lexicographical order of the variables, the most general
solution of the system, following the format of the examples. Print an
empty line at the end of each case.

Hint

Take inspiration from a topological sort.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:56:45.754Z

© Jutge.org, 2006–2026.
https://jutge.org
