class A:
    def __init__(self, txt: str):
        self.u: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: A = None
        self.s: A = None
        self.txt = txt

a = A("a")
d = B("d")
h = B("h")
e = A("e")
c = B("c")

a.u = c
d.p = a
d.s = a
h.p = a
h.s = e
e.u = h
c.p = a
c.s = a
assert d.p.u.s.txt == "
class A:
    def __init__(self, txt: str):
        self.u: B = None
        self.txt = txt

class B:
    def __init__(self, txt: str):
        self.p: A = None
        self.s: A = None
        self.txt = txt

a = A("a")
d = B("d")
h = B("h")
e = A("e")
c = B("c")

a.u = c
d.p = a
d.s = a
h.p = a
h.s = e
e.u = h
c.p = a
c.s = a
assert d.p.u.s.txt == "a"