f | class MroChecker: | f | class MroChecker: |
n | queue = {} | n | mro = {} |
| | | |
| def __init__(self): | | def __init__(self): |
n | while (s := input()): | n | while (line := input()): |
| if s.startswith('class'): | | if line.startswith('class'): |
| name = s[6:s.find(':')] | | cls = line[6:line.find(':')] |
| if '(' in name: | | if '(' in cls: |
| cur = name[:name.find('(')] | | cls_name = cls[:cls.find('(')] |
| prev = [x.strip() for x in name[name.find('(') + 1:-1].split(',')] | | cls_decl = [x.strip() for x in cls[cls.find('(') + 1:-1].split(',')] |
| try: | | try: |
n | n_q = self.mklst([self.queue[x].copy() for x in prev] + [prev]) | n | new_mro = self.build([self.mro[x].copy() for x in cls_decl] + [cls_decl]) |
| except KeyError: | | except KeyError: |
| print('No') | | print('No') |
| break | | break |
n | if n_q: | n | if new_mro: |
| self.queue[cur] = [cur] + n_q | | self.mro[cls_name] = [cls_name] + new_mro |
| else: | | else: |
| print('No') | | print('No') |
| break | | break |
| else: | | else: |
n | self.queue[name] = [name] | n | self.mro[cls] = [cls] |
| else: | | else: |
| print('Yes') | | print('Yes') |
| | | |
| @staticmethod | | @staticmethod |
n | def my_extend(lists): | n | def join(lsts): |
| | | ans = [] |
| | | for lst in lsts: |
| | | ans.extend(lst) |
| | | return ans |
| | | |
| | | def build(self, deps): |
| res = [] | | res = [] |
n | for i in lists: | n | |
| res.extend(i) | | |
| return res | | |
| | | |
| def mklst(self, deps): | | |
| res = [] | | |
| fl = False | | flag = False |
| while (pars := self.my_extend(deps)): | | while (pars := self.join(deps)): |
| fl = False | | flag = False |
| for i in pars: | | for cls in pars: |
| if fl: | | if flag: |
| break | | break |
n | for j in deps: | n | for lst in deps: |
| if i in j and i != j[0]: | | if cls in lst and cls != lst[0]: |
| break | | break |
| else: | | else: |
n | res.append(i) | n | res.append(cls) |
| fl = True | | flag = True |
| for j in deps: | | for tmp in deps: |
| if i in j: | | if cls in tmp: |
| j.pop(0) | | tmp.pop(0) |
| else: | | else: |
t | if not fl: | t | if not flag: |
| return None | | return None |
| return res | | return res |
| MroChecker() | | MroChecker() |