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